1

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:

  1. 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.
  2. 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()
Community
  • 1
  • 1
jti107
  • 149
  • 2
  • 13
  • You should read the [Python docs Curses Tutorial](https://docs.python.org/2/howto/curses.html) and the [window.nodelay](https://docs.python.org/2/library/curses.html#curses.window.nodelay) method in particular. – msw Mar 02 '15 at 18:45
  • thanks that helped. i looked through the tutorial before and it didnt have alot of example. just a description of the functions. – jti107 Mar 02 '15 at 19:46

1 Answers1

2

based on some comments i added screen.nodelay(True) right after the screen=curses.initscr() and was able to get it to work.

jti107
  • 149
  • 2
  • 13