Python ncurses:直到第一次刷新才显示屏幕 [英] Python ncurses: Doesn't show screen until first key-press, even though refresh is first

查看:153
本文介绍了Python ncurses:直到第一次刷新才显示屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码使您可以使用箭头键."在屏幕上的小网格中四处走动.您曾经探索过的地方或附近的地方.即使我在第一次尝试获取按键之前已刷新一下,屏幕也不会首先显示任何内容,直到您离开起始位置为止.难道不应该先显示addstr之后再进行刷新,然后getch在那之后等待吗?我什至尝试添加一个stdscr.refresh(),但这也无济于事.如何在等待第一次按键之前立即刷新屏幕?

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)

推荐答案

文档已损坏.我曾经用过诅咒,但是libncurses对我来说是新的.

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

我的第一个提示来自 ncurses(3):

ncurses库允许操纵称为窗口的数据结构,可以将其视为代表CRT屏幕的全部或一部分的二维字符数组.提供了一个名为stdscr的默认窗口,它是终端屏幕的大小.其他人可能是用newwin创建的. … 也可以操纵称为垫的特殊窗口.这些是不受屏幕大小限制的窗口,其内容不需要完全显示.

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.

但是随后 refresh(3)显然是回避行为:

But then refresh(3) got decidedly evasive:

例程wrefresh的工作方式如下:首先调用wnoutrefresh,它将命名窗口复制到虚拟屏幕,然后调用doupdate,后者将虚拟屏幕与物理屏幕进行比较并进行实际更新. …上面的短语将命名窗口复制到虚拟屏幕"是模棱两可的.实际发生的是将窗口中所有触摸(更改)的行都复制到虚拟屏幕.这会影响使用重叠窗口的程序;这意味着如果两个窗口重叠,则可以按任意顺序刷新它们,并且只有在明确更改重叠区域时,重叠区域才会被修改. [强调我的]

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]

这促使我尝试添加

stdscr.refresh()

在您的pad.refresh()起作用之后.然后我将其进一步移到start()上,以查看每次修改填充时是否确实需要它.我将其一直移动到第一点,并有一个stdscr可以用来产生收益:

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)
    …

这有点巫术编程的问题,但是我不打算看一个有20年历史的图书馆的内部结构,该图书馆是为应付玻璃tty尝试改写它而设计的.

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.

这篇关于Python ncurses:直到第一次刷新才显示屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆