简体   繁体   English

python curses动态值+用户输入

[英]python curses dynamic value + user input

I'm a bit new to python and curses, so had a few questions. 我对python和curses有点陌生,所以有几个问题。 I'm making a terminal application that uses curses to generate the ui. 我正在制作一个使用curses生成ui的终端应用程序。 I am trying to make something similar to the linux program top except it fetches data from an xml file. 我正在尝试使它类似于linux程序顶部,但它会从xml文件中获取数据。 I have this code that i am modifying from a previous stackoverflow question. 我有从先前的stackoverflow问题修改的这段代码。 To simplify things the dynamic value i'm updating here is just the time. 为简化起见,我在这里更新的动态值只是时间。 My two questions are as follows: 我的两个问题如下:

  1. I have x = screen.getch(7,2) to check if the user wants to quit. 我有x = screen.getch(7,2)检查用户是否要退出。 Similar to top, if the user can type "q" to quit. 与top相似,如果用户可以键入“ q”退出。 However because of this the time values arent being updated. 但是,由于这个原因,时间值无法更新。 How can I have user input as well the dynamic updating of the values. 如何获得用户输入以及值的动态更新。 When I comment out the screen.getch the values start updating. 当我注释掉screen.getch值开始更新。 Ive read that top is using the C version of curses so this should be possible, just need a help in the right direction. 我已经读到top使用的是curses的C版本,因此这应该可行,只需要在正确方向上提供帮助即可。
  2. My second question is about the refresh rate. 我的第二个问题是关于刷新率。 Is there a way to control it using the curses api? 有没有办法使用curses api来控制它? The easy way I thought to do it was using the time.sleep() command but I wasnt sure if this was the right way to go about it? 我想做的简单方法是使用time.sleep()命令,但是我不确定这是否是正确的方法? Ideally I'd like to update this with new xml data every 60 seconds or so. 理想情况下,我想每60秒左右用新的xml数据更新一次。

I did see this Python/curses user input while updating screen in stackoverflow and i think question is similar but i was a bit overwhelmed by the code. 我确实在更新 stackoverflow的屏幕时看到了这个Python / curses用户输入 ,我认为问题是相似的,但是我对代码有点不知所措。 i dont really understand classes yet and couldnt follow the logic. 我还不太了解课程,也无法遵循逻辑。 i was looking for something simple. 我在寻找简单的东西。 I believe the guy ended up using line = sys.stdin.read(1) to get his code running. 我相信这个家伙最终使用line = sys.stdin.read(1)来运行他的代码。 I didnt have any luck when using this instead of getch. 当我用这个代替getch时,我没有任何运气。 I got some error about x not being able to use strip(). 我收到一些关于x无法使用strip()的错误。

from os import system
import curses
import time

x = 0

while x != ord('q'):
    screen = curses.initscr()
    curses.curs_set(False) 
    screen.clear()
    screen.border(0)
    screen.addstr(1, 2, "BLAH BLAH ",
              curses.A_REVERSE)

    ltime = time.asctime(time.localtime(time.time()))

    screen.addstr(3, 2,'BLAH1: ' + ltime)    
    screen.addstr(4, 2, "BLAH2")

    screen.refresh()

    x = screen.getch(7,2)


curses.endwin()

基于一些评论,我在screen=curses.initscr()之后立即添加了screen.nodelay(True) screen=curses.initscr()并能够使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM