Note that there are some explanatory texts on larger screens.

plurals
  1. POCanceling events after the .after() method has been called on them?
    text
    copied!<p>I'm writing a little game with python tkinter module using a canvas widget. There is the person, who is just a ball, and bombs that spawn every 10 seconds. 3 seconds after a bomb spawns, it begins a process of "exploding" which I call 4 more methods on the self.bomb object to "animate" it exploding.</p> <p>The object of the game is to try to "disarm" the bombs. I've come up with some fancy collision detection which is working. Once a collision is detected, the bomb is cleared off of the canvas using: </p> <pre><code>self.canvas.delete(self.bomb) </code></pre> <p>However, the following animation of the bomb getting bigger and changing color (exploding) still happens, even if I've "disarmed" the bomb. This is because my original bomb calls:</p> <pre><code>self.parent.after(3000, self.explode_first) </code></pre> <p>... which I have chained 3 more "explode" functions, all bound and called with the .after() method.</p> <p>I tried to circumvent the problem by inserting conditionals into each of the methods called in the .after(), like booleans;</p> <pre><code>if self.bomb # if there's a bomb on the canvas, self.parent.after(125, self.explode_second) # and explode third and so on </code></pre> <p>which is not working. I want to be able to "cancel" events the tkinter queue that have been created with the after method, but here is the kicker. I don't(cant) kill ALL the events, just the ones on the canvas. I have other events going that I want to leave intact, if possible.</p> <p>Is there a way to do this? If not, feedback and/or suggestions on how to do this without canceling after events are welcomed, and Thanks in advance!</p> <p>Heres the relevant code, for reference:</p> <pre><code>def start_game(self): self.Bombs += 1 self.bombsLabel.configure(text = "Total Bombs: %d" % self.Bombs) self.b1 = random.randint(1, int(self.canvas.winfo_width())) self.b2 = random.randint(1, int(self.canvas.winfo_height())) self.b3 = self.b1 + 20 self.b4 = self.b2 + 20 self.create_bomb() self.parent.after(10000, self.start_game) </code></pre> <p>and :</p> <pre><code># continuously make and explode bombs def create_bomb(self): self.bomb = self.canvas.create_oval(self.b1, self.b2, self.b3, self.b4, fill = "red") if self.bomb: self.parent.after(3000, self.explode_first) def explode_first(self): if self.bomb: self.b1 -= 5 self.b2 -= 5 self.b3 += 5 self.b4 += 5 self.canvas.delete(self.bomb) self.bomb = self.canvas.create_oval(self.b1, self.b2, self.b3, self.b4, fill = "orange") self.parent.after(125, self.explode_second) def explode_second(self): if self.bomb: self.b1 -= 5 self.b2 -= 5 self.b3 += 5 self.b4 += 5 self.canvas.delete(self.bomb) self.bomb = self.canvas.create_oval(self.b1, self.b2, self.b3, self.b4, fill = "yellow") self.parent.after(125, self.explode_third) def explode_third(self): self.b1 -= 5 self.b2 -= 5 self.b3 += 5 self.b4 += 5 self.canvas.delete(self.bomb) self.bomb = self.canvas.create_oval(self.b1, self.b2, self.b3, self.b4, fill = "white") self.parent.after(125, self.explode_fourth) def explode_fourth(self): self.canvas.delete(self.bomb) def bomb_disarmed(self): print "disarmed the bomb!" self.canvas.delete(self.bomb) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload