![](/img/trans.png)
[英]AttributeError: module 'curses' has no attribute 'A_ITALIC'
[英]Python Curses - module 'curses' has no attribute 'LINES'
我正在查看一本書中的一些源代碼,並注意到一些代碼似乎不在當前的Python2.7 API中。 根據這段代碼,模塊curses
應該有一個名為LINES
的常量變量和另一個名為COLS
變量。 我打開了一個Python交互式終端,發現沒有COLS
或LINES
變量或方法。
我的問題是:這個代碼甚至可以工作嗎?
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控制台窗口寬度
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.