python刷新显示_Python ncurses:直到第一次刷新才显示屏幕

The code below lets you walk around a small grid on the screen using the arrow keys putting "." where you've explored or been next to. Even though I have my refresh before the first getch (to get a key-stroke) the screen doesn't first display anything until you've moved off your starting position. Shouldn't the addstr followed by refresh immediately show and then the getch waits after that? I even tried adding a stdscr.refresh(), but that didn't help either. How do I get the screen to refresh immediately before waiting for the first key-stroke?

import curses

def start(stdscr):

curses.curs_set(0)

movement = curses.newpad(10, 10)

cur_x, cur_y = 5, 5

while True:

movement.addstr(cur_y, cur_x, '@')

for (x_off, y_off) in [(-1,0),(1,0),(0,-1),(0,1)]:

movement.addstr(cur_y + y_off, cur_x + x_off, '.')

movement.refresh(1, 1, 0, 0, 7, 7) #Nothing is displayed until after the first key-stroke

key_stroke = stdscr.getch()

move_attempt = False

if 0 < key_stroke < 256:

key_stroke = chr(key_stroke)

elif key_stroke == curses.KEY_UP and cur_y > 1:

cur_y -= 1

elif key_stroke == curses.KEY_DOWN and cur_y < 8:

cur_y += 1

elif key_stroke == curses.KEY_LEFT and cur_x > 1:

cur_x -= 1

elif key_stroke == curses.KEY_RIGHT and cur_x < 8:

cur_x += 1

else:

pass

if __name__ == '__main__':

curses.wrapper(start)

解决方案

The docs are broken. I'd used curses back in the day, but libncurses is new to me.

My first hint came from ncurses(3):

The ncurses library permits manipulation of data structures, called windows, which can be thought of as two-dimensional arrays of characters representing all or part of a CRT screen. A default window called stdscr, which is the size of the terminal screen, is supplied. Others may be created with newwin.

Special windows called pads may also be manipulated. These are windows which are not constrained to the size of the screen and whose contents need not be completely displayed.

But then refresh(3) got decidedly evasive:

The routine wrefresh works by first calling wnoutrefresh, which copies the named window to the virtual screen, and then calling doupdate, which compares the virtual screen to the physical screen and does the actual update. … The phrase "copies the named window to the virtual screen" above is ambiguous. What actually happens is that all touched (changed) lines in the window are copied to the virtual screen. This affects programs that use overlapping windows; it means that if two windows overlap, you can refresh them in either order and the overlap region will be modified only when it is explicitly changed. [emphasis mine]

which prompted me to try adding

stdscr.refresh()

after your pad.refresh() which worked. And then I moved it further up start() to see if it was really needed on every pad modification. I moved it all the way up to the first point there is a stdscr to work with yielding:

def start(stdscr):

stdscr.refresh()

curses.curs_set(0)

which smacks of voodoo programming, but I'm not going to look at the innards of a 20-year old library made to cope with glass ttys to try to grok it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值