Thanks for the reply Alan. I'm unfamiliar on both methods you suggested. Can you give me examples with code of them?<br>I saw this piece of code on your webpage:<br><pre>import msvcrt<br><br>def doKeyEvent(key):<br> if key == '\x00' or key == '\xe0': <span class="comment"># non ASCII</span><br>
key = msvcrt.getch() <span class="comment"># fetch second character</span><br> print ord(key),<br><br>def doQuitEvent(key):<br> raise SystemExit<br><br><span class="comment"><br># First, clear the screen of clutter then warn the user <br>
# of what to do to quit</span><br>lines = 25 <span class="comment"># set to number of lines in console</span><br>for line in range(lines): print<br><br>print "Hit space to end..."<br>print<br><br><span class="comment"># Now mainloop runs "forever"</span><br>
while True:<br> ky = msvcrt.getch()<br> length = len(ky)<br> if length != 0:<br> <span class="comment"># send events to event handling functions</span><br> if ky == " ": <span class="comment"># check for quit event</span><br>
doQuitEvent(ky)<br> else: <br> doKeyEvent(ky)<br><br><br><br></pre>Is this similar to the try except clause method? <br>How will breaking code into timed events help me? I wanted to stop the function on an event (like pushing a button). The part I don't really understand is how the program can be screening or "recognizing" a command or event while it hasn't finished this other process. How does the timer help in this? From my experiences with python, python doesn't let anything get in its way until its done with the current process unless you end it immediately with ctrl c or pressing X. It won't even let you type text into the console while it's "in process."<br>
<br><div class="gmail_quote"><br>On Mon, Jun 8, 2009 at 4:23 AM, Alan Gauld <span dir="ltr"><<a href="mailto:alan.gauld@btinternet.com">alan.gauld@btinternet.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
"xbmuncher" <<a href="mailto:xboxmuncher@gmail.com" target="_blank">xboxmuncher@gmail.com</a>> wrote<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">
def longLoop():<br>
x = 0<br>
for n in range(100000):<br>
time.sleep(5)<br>
x = x + n<br>
<br></div><div class="im">
It will run in the console for a long time. How can I send a command and<br>
cause it to stop and pursue hello() function?<br>
</div></blockquote>
<br>
You can use Ctrl-C and put the loop insiode a try/except clause<br>
to catch the Interrupt. Or....<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
in the console.. and really I'd like to create a GUI that will run the<br>
longLoop() function after I push a button and then press another button to<br>
stop the longLoop() function and run the hello() function.<br>
</blockquote>
<br></div>
There are two normal ways to do this in a GUI:<br>
1) break the loop into small chunks and run them in response<br>
to timer events. The function then calls the timer at the end of<br>
each chunk until it completes its task. This frees up the GUI to<br>
detect user events between timers.<br>
<br>
2) Use threads. This has the advantage of working in a GUI<br>
or a commandline. But is a little bit more complex and you<br>
need to make sure your loop isn't messing with (or being<br>
messed with) the rest of the priogram via shared data/resources.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
in the wxPython, which is gui of choice for python. I'll settle for a way to<br>
do it via console as well. The overall idea is, is that I want to cut short<br>
the process of a piece of code in order to start another piece of code.<br>
</blockquote>
<br></div>
You can put a terminating flag in the timer code or you can<br>
send a message to stop a thread, either way will work.<br>
<br>
HTH,<br><font color="#888888">
<br>
<br>
-- <br>
Alan Gauld<br>
Author of the Learn to Program web site<br>
<a href="http://www.alan-g.me.uk/" target="_blank">http://www.alan-g.me.uk/</a> <br>
<br>
_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</font></blockquote></div><br>