tk: how to invoke it just to display something, and return to the main program?
Posted
by
max
on Stack Overflow
See other posts from Stack Overflow
or by max
Published on 2011-01-27T08:00:44Z
Indexed on
2011/01/30
23:25 UTC
Read the original article
Hit count: 216
Sorry for the noob question but I really don't understand this.
I'm using python / tkinter and I want to display something (say, a canvas with a few shapes on it), and keep it displayed until the program quits. I understand that no widgets would be displayed until I call tkinter.tk.mainloop()
. However, if I call tkinter.tk.mainloop()
, I won't be able to do anything else until the user closes the main window.
I don't need to monitor any user input events, just display some stuff. What's a good way to do this without giving up control to mainloop
?
EDIT:
Is this sample code reasonable:
class App(tk.Tk):
def __init__(self, sim):
self.sim = sim # link to the simulation instance
self.loop()
def loop():
self.redraw() # update all the GUI to reflect new simulation state
sim.next_step() # advance simulation another step
self.after(0, self.loop)
def redraw():
# get whatever we need from self.sim, and put it on the screen
EDIT2 (added after_idle):
class App(tk.Tk):
def __init__(self, sim):
self.sim = sim # link to the simulation instance
self.after_idle(self.preloop)
def preloop():
self.after(0, self.loop)
def loop():
self.redraw() # update all the GUI to reflect new simulation state
sim.next_step() # advance simulation another step
self.after_idle(self.preloop)
def redraw():
# get whatever we need from self.sim, and put it on the screen
© Stack Overflow or respective owner