[Tutor] Tkinter, widgets not displaying...

John Fouhy john at fouhy.net
Fri Feb 10 08:32:53 CET 2006


On 10/02/06, Hugo González Monteverde <hugonz-lists at h-lab.net> wrote:
> Sorry for replying to myself, but I found a page that says that it can
> be done without threads... so it hit me: use threads!!! (duh)

Yes, this will be what you need.  Your GUI won't do anything until you
run mainloop, and mainloop won't exit until you close your GUI.  So if
you want to programmatically feed data in, you need to do it from a
different thread.

One caveat, though ---

> if __name__ == "__main__":
>      start_new_thread(Tkinter.Tk.mainloop, (top,))
>      for i in range(10):
>          myturns.insert(str(i))
>          sleep(0.2)

By doing this, you are changing the GUI from a different thread from
the one running mainloop.  This is a BAD IDEA.  It can easily lead to
weird bugs that you will struggle to understand.

If you want to do multithreaded programming with a GUI, one good way
is to use .after_idle.

ie, instead of myturns.insert(str(i)), do
top.after_idle(myturns.insert, str(i))  (I think this is the right
syntax).  This will cause the mainloop thread to run the code instead.

HTH!

--
John.


More information about the Tutor mailing list