简体   繁体   中英

Python Curses - module 'curses' has no attribute 'LINES'

I am looking at some source-code from a book and noticed that some of the code does not seem to be in the current Python2.7 API. The module curses is, according to this code, supposed to have a constant variable called LINES and another called COLS . I opened a Python interactive terminal and saw that there is no COLS or LINES variable or method.

My question is: How does this code even work?

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()

I found that curses.LINES exists in Python2 & Python3, but you have to call curses.initscr before using it, or you will get AttributeError.

you can also use window.getmaxyx

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

That code is written for Python 3. You can see curses.LINES is now in that API, though it was not in Python 2.7:

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

If you need to get the terminal width and height in Python 2, see here: How to get Linux console window width in Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM