From: Alex T. <al...@tw...> - 2005-07-16 13:39:06
|
Rohit wrote: >Hi, >I have started using Pythoncard recently, so this may seem trivial. I >was going through the samples which come with it and I found the >"gravity" sample, which animates a number of balls. >I also tried to do something similar to that, but just only for one >ball with one color. For this I used one button to start the >animation, another button to stop it and a canvas to draw the whole >sequence. In the animation sequence the program keeps on checking >whether a flag variable has been altered to stop the animation or not. >This flag variable was supposed to be altered by the stop animation >button, when a mouse click occured. But when I click the stop button >the animation keeps on running. I am including the code of my program. > > The problem is that your code runs continuously, so there is no opportunity for the click on the "stop" button to be processed, and so self.stopAnimation can't be set. This also means that you can't, for instance, move your window while the animation is running. Easiest fix is to add # give the user a chance to click Stop wx.SafeYield(self, True) inside your loop. This gives an opportunity for other things to happen. A better fix, IMO, is to use timers instead of a loop. You create a time in your initialization code, then when the start button is pressed, you start the timer. Each iteration of drawing is done within the timer handler, and the stop button simply stops the timer. See the 'flock' sample for an example of doing this. There are two reasons I say that this is better is: 1. You can more easily extend it to do more animations, perhaps at different time scales, by having multiple independent timers; the loop-with-yield mechanism is more restrictive. 2. You can do other things in the program while the timers continue to operate - if you do something else within the app during a loop+yield, the animation stops. For instance - I tested this out by creating a simple app with File/Help menus, and adding the buttons and canvas to it. I checked that, indeed, the stop button didn't work; then added the SafeYield() line, and now the stop button works immediately. Now, if I hit start and then the select File menu, the ball moves briefly then disappears while I keep the menu selected; once I release the menu, the ball continues its movement. But in the flock sample, the animation continues while the menu items are selected. -- Alex Tweedly http://www.tweedly.net -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.323 / Virus Database: 267.8.16/50 - Release Date: 15/07/2005 |