'instancemethod'对象没有属性'ERASE'(Class中的方法)

时间:2016-01-23 00:26:05

标签: python

我正在处理一个带有一些要求的赋值,例如将stdin放在终端窗口底部的诅咒中,因此我尝试了这个:Keep stdin line at top or bottom of terminal screen,这个问题的答案对我来说非常适合测试(没有上课)。但是当我把答案中的方法合并到我的班级时,就会出现一些问题。

我的部分代码就像这样(runnable):

­

在这种情况下(在Class中测试时),程序运行时出现此错误:

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

它说实例没有名为'ERASE'的属性,我找不到解决方案,因为我在测试中完全运行它而不使用类,这只有在我在课堂上使用时才会发生。

0 个答案:

没有答案