From: Alex T. <al...@tw...> - 2004-07-09 15:34:23
|
At 14:01 09/07/2004 +0200, pie...@ba... wrote: >Hello, > >I have the following problem: > >I'm writing a PythonCard application that has 2 buttons (a start button and >a stop button). > >When I press the start button, the 'on_cmdStart_mouseClick(self, event):' >function is executed. >Now, I want to be able to stop that function at any time by pressing the >STOP button. > >My problem is now that 'mouseClick'-events are not seen as long as I'm in >the 'on_cmdStart_...' function. > >Can anyone help me? It's not just mouse-Click events that aren't seen - while your code is running, no events will be processed. You should read (or re-read to remind yourself) about http://pythoncard.sourceforge.net/timers-threads.html Note in particular the introductory paragraph that says: >Most of the top-level code you add to the front ends - your PythonCard >user interfaces - is event handlers. As with programming with any GUI, >event handlers should always complete their job fast, and return promptly, >so they don't 'clog up the works'. PythonCard is no exception. While an event handler is running, no further events can happen. The cleanest way to deal with processing that takes a long time is using threads - and is well explained in that tutorial. If you have a really simple case, you can use the wxApp function Yield - but read the cautions about its dangers - see the wx documentation for wxApp::Yield Here's a simple bit of code that shows what this does Notes. 1. the work this does to keep it busy is calculate factorials - hey, I've always wanted a chance to write a recursive factorial function for a good purpose :-) 2. each time round the loop, it calls yield to allow other events to be processed - without this the Stop button click wouldn't happen, and in fact the writing and updating of the TextArea would happen. (try commenting out the Yield line to see it fail ....) 3. the "app" in "app.Yield()" is the global from the main program (not included here, but the standard name used in the templates). def on_openBackground(self, event): self.results = [] def on_Start_mouseClick(self, event): def fact(i): if i<=1: return 1 return i * fact(i-1) self.stopping = False for i in range(1000): j = fact(i) s = "%d! is %d" % (i, j) ## self.results.append(s) ## self.components.TextArea1.text = "\n".join(self.results) self.components.TextArea1.text = str(i) app.Yield() if self.stopping: break def on_Stop_mouseClick(self, event): self.stopping = True -- Alex. |