I am working on an assignment with some requirements such as keep stdin at the bottom of the terminal window in curse, hence I tried this: Keep stdin line at top or bottom of terminal screen, and the answer to that question works on me perfectly in a test (without class). But when I merge that method in the answer into my class, some problems happened.
part of my code is like this (runnable):
import curses
import curses.ascii
from string import printable
class Node:
def main(self):
curses.wrapper(self.init) # start running the program
def init(self, screen):
global PRINTABLE
PRINTABLE = map(ord, printable)
Y, X = screen.getmaxyx()
height = Y - 1
type = 0
command = self.stdin_fixed(screen, height, type)
"""From: https://stackoverflow.com/questions/30258999/keep-stdin-line-at-top-or-bottom-of-terminal-screen/30259422#30259422
"""
def stdin_fixed(self, screen, height, type):
global PRINTABLE
screen.move(height, 0)
screen.clrtoeol()
if (type == 0):
screen.addstr("[Command Line Interface]> ")
elif (type == 1):
screen.addstr(height, 0, "[RMP node " + str(self.HOST) + "]# ")
ERASE = self.stdin_fixed.ERASE = getattr(self.stdin_fixed, "erasechar", ord(curses.erasechar()))
Y, X = screen.getyx()
s = []
while True:
c = screen.getch()
if c in (curses.ascii.LF, curses.ascii.CR, curses.KEY_ENTER):
break
elif c == ERASE or c == curses.KEY_BACKSPACE:
y, x = screen.getyx()
if x > X:
del s[-1]
screen.move(y, (x - 1))
screen.clrtoeol()
screen.refresh()
elif c in PRINTABLE:
s.append(chr(c))
screen.addch(c)
else:
pass
return "".join(s)
if __name__ == '__main__':
node = Node()
node.main() # run the instance
In this case (when test in Class), the program runs with this error:
...
(some traceback)
...
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/curses/wrapper.py", line 43, in wrapper
return func(stdscr, *args, **kwds)
File "RMP_noGPIO.py", line 341, in init
command = self.stdin_fixed(screen, height, type)
File "RMP_noGPIO.py", line 518, in stdin_fixed
ERASE = self.stdin_fixed.ERASE = getattr(self.stdin_fixed, "erasechar", ord(curses.erasechar()))
AttributeError: 'instancemethod' object has no attribute 'ERASE'
It says that the instance has no attribute named 'ERASE', and I cannot find the solution to this, cause I ran it perfectly in the test without using class, and this only occurs when I use it in class.