Tkinter Q: Interrupting an update loop?

Cheong Siew Ann cheongsa at limhk.dyn.cheapnet.net
Sun Jun 3 00:04:46 EDT 2001


Hi,

I have been trying to adapt Guido's electrons.py 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 electrons.py 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 electrons.py and letting the Ising class owns
      # a Tk() object
      self.tk = tk = Tk()
      self.canvas = canvas = Canvas(tk, width=..., height=...)
      canvas.pack()
      quit_button = Button(tk, text="Quit", command=self.Quit)
      quit_button.pack(...)
      start_button = Button(tk, text="Start", command=self.Update)
      start_button.pack(...)
   ...
   ...
   def Update(self, ...)
      # modifies the fill option of squares tiling the canvas

   def RunMainLoop(self):
      self.tk.mainloop()

   def Quit(self):
      self.tk.quit()

# main program

def main():

   ising = Ising(...)

   ising.RunMainLoop()

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
displayed.

(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 self.tk.mainloop() to self.Update(), and no events are serviced
until self.Update() returns control to self.tk.mainloop()?

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