Re: Stopping a loop with user input. in curses

Discussion in 'Python' started by Raymond Arthur St. Marie II of III, Jul 20, 2003.

  1. Hey Alex,
    I wrote a detailed explanation earlier today and it seams to have disappeared.
    That's okay, because I had really beat up your code.

    You know, it didn't occure to me till hours later that all you really had to do
    is move the second while loop into the "theClock()" definition like this.

    I move the second while loop into the "theClock()" function so that it handles
    the key press. Then consolodated the while loops into one loop.
    If the finished variable is 0(zero) then the loop continues else it breaks the
    loop and the function has no where else to go but out.

    The problem with your original code is that the call to "theClock()" function
    never sees the 'q' key press because the second while loop never gets executed
    while the first while loop is forever executing.

    I don't have curses installed so I can't test this for you, but it should
    work.

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

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

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


    def main(stdscr):

    # Bring up the clock function

    theClock()

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

    Ray--
    Raymond Arthur St. Marie II of III, Jul 20, 2003
    #1
    1. Advertising

  2. Raymond Arthur St. Marie II of III

    Alex Guest

    Re: Stopping a loop with user input in curses

    Hello Ray,

    Nope. That didn't do it. I got a properly drawn curses box
    with nothing inside ... until I hit the 'q' key. Then the clock
    popped up and the program quit.

    I have a theory on curses. I could be totally wrong here (and it
    would be great if I was, actually).

    Curses cannot multi-task.

    It can't redraw a clock every second and check for a particular
    keystroke at the same time -- there is only one cursor after all.

    So I guess I'll forgoe the funky clock in my curses app. However,
    if you can completely flush this theory down the toilet, it would
    be greatly appreciated... :) It would make my life easier in other
    aspects of the program I'm trying to write.


    Thanks,

    Alex
    Alex, Jul 21, 2003
    #2
    1. Advertising

  3. Raymond Arthur St. Marie II of III

    Alex Guest

    Re: Stopping a loop with user input in curses

    Thank you so much! That worked fantastically!

    I'll have to read up on the select module. It seems like I'll be
    using it a lot for the application I'm writing.

    Once again, thank you.


    Alex
    Alex, Jul 22, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chatralias
    Replies:
    0
    Views:
    382
    Chatralias
    Jul 20, 2003
  2. Raymond Arthur St. Marie II of III

    Re: Stopping a loop with user input. in curses

    Raymond Arthur St. Marie II of III, Jul 22, 2003, in forum: Python
    Replies:
    1
    Views:
    321
  3. Jean-Paul Calderone
    Replies:
    2
    Views:
    429
    Joel Hedlund
    Feb 9, 2006
  4. ern
    Replies:
    23
    Views:
    761
    Keith Thompson
    Oct 29, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page