Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Stopping a loop with user input. in curses

Reply
Thread Tools

Re: Stopping a loop with user input. in curses

 
 
Raymond Arthur St. Marie II of III
Guest
Posts: n/a
 
      07-20-2003
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--
 
Reply With Quote
 
 
 
 
Alex
Guest
Posts: n/a
 
      07-21-2003
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

 
Reply With Quote
 
 
 
 
Alex
Guest
Posts: n/a
 
      07-22-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: Replacing curses (Was: Re: Problem with curses and UTF-8) Jean-Paul Calderone Python 2 02-09-2006 08:29 AM
Stopping a while loop with user input ? ern C Programming 23 10-29-2005 10:12 PM
Re: Stopping a loop with user input. in curses Raymond Arthur St. Marie II of III Python 1 07-23-2003 02:20 AM
Re: Stopping a loop with user input. in curses Chatralias Python 0 07-20-2003 03:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57