[Tutor] Stopping a loop with user input in curses

Michael Barrett mike@daboyz.org
Mon Jul 21 17:57:09 2003


It looks to me like the problem is that you call 'theClock' which has a while loop inside it that never ends.  I tried throwing your check for the button press of q in the while loop inside theClock but it looks like stdscr.getch() blocks until a button is pressed.  This is because you don't have it set in no_delay mode.  To set it in no_delay mode, use the nodelay method.

Here's my edit to your code that seems to do what you want.  No guarantee this is the best way to do it (i'm sure it's not) but it works for me.  Hope this helps:


#! /usr/bin/python

# Import curses module

import curses, time
stdscr = curses.initscr()

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()

    while 1:
        t = time.asctime()
        clockWindow.addstr(1, 1, t)
        clockWindow.refresh()
        stdscr.nodelay(1)
        c = stdscr.getch()
        if c == ord('q'):
            curses.beep()
            break
        time.sleep(1)


def main(stdscr):

    # Bring up the clock function

    theClock()

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


====
On Sat, Jul 19, 2003 at 06:04:10PM -0400, Alex from Caustic Creations wrote:
> Hello all,
> 
> I've written a small clock program in curses that I would
> like to have end when someone presses 'q'. I've included the
> code below. I'm *very* new to python so I apologize in advance if
> this seems like a horribly obvious problem. I've spent all day
> trying various structures but none of them have worked out.
> 
> Any help would be greatly appreciated.
> 
> Thanks,
> 
> Alex
> 
> 
> #! /usr/bin/python
> 
> # Import curses module
> 
> import curses, time
> stdscr = curses.initscr()
> 
> 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()
> 
>     while 1:
>         t = time.asctime()
>         clockWindow.addstr(1, 1, t)
>         clockWindow.refresh()
>         time.sleep(1)
> 
> 
> def main(stdscr):
> 
>     # Bring up the clock function
> 
>     theClock()
> 
>     # If 'q' is pressed, exit
>     while 2:
>     c = stdscr.getch()
>     if c == ord('q'):
>             curses.beep()
>             break
> 
> 
> if __name__ == '__main__':
>     curses.wrapper(main)
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
     ________________________________________________________________________
                Mike Barrett | "We have veggie bacon, why don't we have
             mike@daboyz.org |  meat fruit?"
              www.daboyz.org |    -- My co-worker, Ben
     ------------------------+-----------------------------------------------