为什么可以';是否将addstr()添加到python诅咒窗口中的最后一行/列?
使用Python,我试图使用addstr()将光标位置写入我的curses窗口的右下角,但我得到了一个错误<代码>屏幕H-2工作正常,但打印在winddow底部向上的第二行<代码>屏幕H-1根本不起作用。我做错了什么为什么可以';是否将addstr()添加到python诅咒窗口中的最后一行/列?,python,curses,Python,Curses,使用Python,我试图使用addstr()将光标位置写入我的curses窗口的右下角,但我得到了一个错误屏幕H-2工作正常,但打印在winddow底部向上的第二行屏幕H-1根本不起作用。我做错了什么 import curses ScreenH = 0 ScreenW = 0 CursorX = 1 CursorY = 1 def repaint(screen): global ScreenH global ScreenW global CursorX glob
import curses
ScreenH = 0
ScreenW = 0
CursorX = 1
CursorY = 1
def repaint(screen):
global ScreenH
global ScreenW
global CursorX
global CursorY
ScreenH, ScreenW = screen.getmaxyx()
cloc = ' ' + str(CursorX) + ':' + str(CursorY) + ' '
cloclen = len (cloc)
screen.addstr (ScreenH - 1, ScreenW - cloclen, cloc, curses.color_pair(1));
def Main(screen):
curses.init_pair (1, curses.COLOR_WHITE, curses.COLOR_BLUE)
repaint (screen)
while True:
ch = screen.getch()
if ch == ord('q'):
break
repaint (screen)
curses.wrapper(Main)
File "test.py", line 17, in repaint
screen.addstr (ScreenH - 1, ScreenW - cloclen, cloc, curses.color_pair(1));
_curses.error: addstr() returned ERR
您需要从宽度中减去1,就像在高度中减去1一样。否则字符串将超过屏幕的宽度
screen.addstr(ScreenH - 1, ScreenW - 1 - cloclen, cloc, curses.color_pair(1))
^^^
您也可以使用
insstr
而不是addstr
:
screen.insstr(ScreenH - 1, ScreenW - 1 - cloclen, cloc, curses.color_pair(1))
这将阻止滚动,从而允许您打印到最后一行的最后一个字符。但是如果我将addstr()行简化为screen.addstr(ScreenH-1,ScreenW-1,'*',curses.color_pair(1)),为什么它仍然会出错?我也从宽度和高度中减去1。就像我不能打印左下角的字符一样。@wufoo,因为这会导致滚动被禁用,除非您调用
screen.scrollok(1)
。啊,是的。谢谢你的澄清!