Tkinter Q: Interrupting an update loop?

Cheong Siew Ann cheongsa at
Sun Jun 3 00:04:46 EDT 2001


I have been trying to adapt Guido's example for my own use.

Rather than moving items on a canvas, I have a class whose Update()
function modifies the fill option of a static grid of squares on the canvas
every time step.

Whereas creates a canvas and a fixed number of circles and
run for 500 time steps before quiting, I would like to have some minimal
control of the updating.  Here's my class definition:

class Ising:
   def __init__(self, ...):
      # following Guido's and letting the Ising class owns
      # a Tk() object = tk = Tk()
      self.canvas = canvas = Canvas(tk, width=..., height=...)
      quit_button = Button(tk, text="Quit", command=self.Quit)
      start_button = Button(tk, text="Start", command=self.Update)
   def Update(self, ...)
      # modifies the fill option of squares tiling the canvas

   def RunMainLoop(self):

   def Quit(self):

# main program

def main():

   ising = Ising(...)


The observed behaviour is as follows:

(1) when the script is executed, the Tk() window is launched, and the
initial state of the squares (initialized by another function in Ising) is

(2) if the "Start" button is clicked, the state of the squares is updated
by Update(), until the pre-specified number of time steps has elapsed, and
the final state of the squares is displayed.

(3) if the "Quit" button is clicked before the start of the simulation, or
after the termination of the simulation, then the Tk() window closes.

However, if I try to click the "Quit" button while the updating is in
progress, then nothing happens.  Is this because control has been passed
from the to self.Update(), and no events are serviced
until self.Update() returns control to

Is it possible to interrupt self.Update(), and pause the simulation?  I
notice that the destroy button at the top right corner of the Tk() window
still works...

Thanks in advance.

Cheong Siew Ann

More information about the Python-list mailing list