I'm a bit new to python and curses, so had a few questions. I'm making a terminal application that uses curses to generate the ui. I am trying to make something similar to the linux program top except it fetches data from an xml file. I have this code that i am modifying from a previous stackoverflow question. To simplify things the dynamic value i'm updating here is just the time. My two questions are as follows:
- I have
x = screen.getch(7,2)
to check if the user wants to quit. Similar to top, if the user can type "q" to quit. 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. Ive read that top is using the C version of curses so this should be possible, just need a help in the right direction. - My second question is about the refresh rate. Is there a way to control it using the 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? Ideally I'd like to update this with new xml data every 60 seconds or so.
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. 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. I didnt have any luck when using this instead of getch. I got some error about x not being able to use 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()