[Tutor] Stopping a loop with user input in curses

Alex from Caustic Creations alex@caustic-creations.com
Wed Jul 23 16:13:01 2003


Thanks a lot for the replies everyone.

Someone on the Python newsgroup suggested I use the select module to
accomplish what I'm trying to do. Here is the code he modified to
illustrate his solution:

[Code]

# Import curses module

import curses, time
stdscr = curses.initscr()
from select import select

def theClock():

     # Define global colour scheme
     curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)

     # Get the screen size
     max_y, max_x = stdscr.getmaxyx()

     # Calculate the clock position relative to the screen size
     clock_x = max_x - 28

     # Draw the clock
     clockWindow = curses.newwin(3, 26, 1, clock_x)
     clockWindow.bkgd(' ', curses.color_pair(1))
     clockWindow.box()
     clockWindow.refresh()

     # If 'q' is pressed, exit
     finished = 0
     while not finished:    # finished = 0 until the 'q' key is pressed
         if select([0], [], [], 1)[0]:
             c = stdscr.getch()
             if c == ord('q'):
                 curses.beep()
                 finished = 1
                 break

         t = time.asctime()
         clockWindow.addstr(1, 1, t)
         clockWindow.refresh()


def main(stdscr):

     # Bring up the clock function

     theClock()

if __name__ == '__main__':
     curses.wrapper(main)

[/Code]

This seems like a very efficient way of accomplishing the task at hand.
If anyone has any suggestions or comments, please do let me know. I'm
trying to learn Python the right way.


Alex