我可以在没有getch()函数的情况下在ncurses屏幕上显示字符串吗? [英] Can I show strings on ncurses screen without getch() function?

查看:97
本文介绍了我可以在没有getch()函数的情况下在ncurses屏幕上显示字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我只知道一种使用ncurses库显示字符串的方法,如下所示:

Currently, I know only one way to show strings using ncurses library, like as below:

import curses

stdscr = curses.initscr()
stdscr.addch(0,0,'x')
stdscr.getch()

但是当我想使用下降的字符串功能时遇到了一个问题.

But I've met a problem when I want to make a falling function of string.

import curses
import time

stdscr = curses.initscr()
y=1
def fall():
    global y
    stdscr.addstr(y,0,'x')
    stdscr.move(y-1,0)
    stdscr.clrtoeol()
    y += 1 
    stdscr.getch()

while True:
    time.sleep(0.2)
    fall()

如果删除此getch()功能,则看不到 ncurses 屏幕.但是,如果我把它放进去.我必须触摸键盘上的某个键,否则字符串可能掉下来.

If I remove this getch() function, I can't see the ncurses screen. But if I put it in. I have to touch some key on my keyboard then the string could fall.

有没有一种方法可以使字符串自动落下而不会击中键盘或鼠标?

Is there a way I can make the string automatically falling without hit keyboard or mouse?

推荐答案

您必须通过调用

You have to explicitly update the screen, either by calling the refresh() method on the window (stdscr in your example) or by calling curses.doupdate().

这是由于curses是几年前编写的,当时终端运行非常缓慢,有效进行修改非常重要.通过显式更新,您可以首先更改屏幕的方式,然后在单个操作中对其进行更新,而不是对每个单个操作都进行更新.

This is due to the fact that curses was written years ago, when terminal where pretty slow and it was really important to make modifications efficiently. With an explicit update you can first change the screen how you want and then update it in a single operation, instead of doing an update for every single operation.

这篇关于我可以在没有getch()函数的情况下在ncurses屏幕上显示字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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