为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

元芳怎么了 2021-03-09 17:17:08
下面的代码使您可以使用箭头键“。”在屏幕上的小网格上四处走动。您曾经探索过的地方或附近的地方。即使我在第一次尝试获取按键之前已刷新一下,屏幕也不会首先显示任何内容,直到您离开起始位置为止。难道不应该先显示addstr之后再进行刷新,然后getch在那之后等待吗?我什至尝试添加一个stdscr.refresh(),但这也无济于事。如何在等待第一次按键之前立即刷新屏幕?import cursesdef 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:            passif __name__ == '__main__':    curses.wrapper(start)
查看完整描述

3 回答

?
www说

TA贡献1775条经验 获得超8个赞

stdscr.refresh()movement.refresh()解决问题之前,先添加一些时间。

通过time.sleep(1)在refresh语句之后添加,它确实写入屏幕,但随后在stdscr.getch()调用时消失,但仅在第一次时消失。可能与stdscr的某种延迟初始化有关。

stdscr.refresh()之后调用movement.refresh()会产生相同的效果:第一次通过循环会stdscr.refresh()清除屏幕,但不会在随后的几次通过循环。通过stdscr.refresh()在程序中尽早调用,可以使这种奇怪的第一次刷新不受影响。


查看完整回答
反对 回复 2021-03-27
?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

使用平板电脑时,出于某种原因(我不知道为什么),您必须curses.doupdate在调用平板电脑的之后打电话给refresh


查看完整回答
反对 回复 2021-03-27
  • 3 回答
  • 0 关注
  • 237 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号