Python Curses - 模块 'curses' 没有属性'LINES'



我在看一本书中的一些源代码,注意到有些代码似乎不在当前的Python2.7 API中。根据这段代码,模块curses应该有一个称为LINES的常量变量和另一个名为COLS的常量变量。我打开了一个Python交互终端,发现没有COLSLINES变量或方法。

我的问题是:这个代码是如何工作的

def draw_loglines(self):
        self.screen.clear()
        status_col = 4
        bytes_col = 6 
        remote_host_col = 20
        status_start = 0 
        bytes_start = 4 
        remote_host_start = 10
        line_start = 26 
        logline_cols = curses.COLS - status_col - bytes_col - remote_host_col - 1
        for i in range(curses.LINES):
            c = self.curr_topline
            try:
                curr_line = self.loglines[c]
            except IndexError:
                break
            self.screen.addstr(i, status_start, str(curr_line[2]))
            self.screen.addstr(i, bytes_start, str(curr_line[3]))
            self.screen.addstr(i, remote_host_start, str(curr_line[1]))
            #self.screen.addstr(i, line_start, str(curr_line[4])[logline_cols])
            self.screen.addstr(i, line_start, str(curr_line[4]), logline_cols)
            self.curr_topline += 1 
        self.screen.refresh()

我发现curses.LINES存在于Python2&Python3,但在使用它之前必须调用curses.initscr,否则会得到AttributeError。

您也可以使用window.getmaxyx

[1]https://docs.python.org/2/library/curses.html#curses.window.getmaxyx

该代码是为Python 3编写的。您可以看到curses.LINES现在在API中,尽管它不在Python 2.7:中

https://docs.python.org/3/howto/curses.html

如果您需要在Python 2中获得终端宽度和高度,请参阅此处:如何在Python 中获得Linux控制台窗口宽度

最新更新