r = 0
mypad.addstr(0, 0, "Test...")
while 1:
mypad.refresh(r, 0, 1, 1, 10, 10)
c = mypad.getch()
if c == curses.KEY_UP:
r -= 1
elif c == curses.KEY_DOWN:
r += 1
elif c == ord('q'): break
显示 Test...
,但当我向上/向下按时它就消失了。
更新
实际上当添加更多行时,我发现它会滚动,但只能向上,而不是低于初始位置。无论有多大r,文本都将保留在第一行。我错过了什么?
答案 0 :(得分:2)
检查上/下箭头的值,并将其与curses.KEY_UP/DOWN
进行比较。请参阅My cursor keys do not work。
例如,键盘在我的终端上返回3个字符:
import curses
with curses_screen() as stdscr:
pad = curses.newpad(100, 100)
pad.addstr(0,0, curses.longname())
for i in range(1, 10):
pad.addstr(i,0, str(i))
coord = 5, 5, 20, 75
pad.refresh(0, 0, *coord)
KEY_UP, KEY_DOWN = 'AB'
y = 0
for c in iter(pad.getkey, 'q'):
if c in '\x1b\x5b': continue # skip escape seq
y -= (c == KEY_UP)
y += (c == KEY_DOWN)
y = min(max(y, 0), 9)
pad.refresh(y, 0, *coord)