[Tkinter-discuss] Proposal for thread-safe Tkinter

Guilherme Polo ggpolo at gmail.com
Mon Oct 27 20:57:45 CET 2008

On 10/27/08, C. Allen <callen at gemini.edu> wrote:
>  so if you do compile that way, Tk is thread safe, I can have separate
> mainloops in different threads and they can all post to each others windows,
> etc?

No, it doesn't make tk thread safe, it makes tkinter protect access
from multiple threads to data in tcl/tk.

Also, having multiple mainloops is another story. To have multiple
mainloops you would be creating multiple tcl interpreters, which is
allowed by tkinter but it will cause all sort of weird errors.

Now what you can do when tcl/tk is compiled with --enable-threads and
python has thread support too is creating threads in python and
changing tk widgets in another thread. tkinter will schedule these
calls from other threads to run in the main thread (with a probability
to fail).

Other toolkits, like qt, require explicit scheduling from the user in
these situations, but tkinter tries to do it automatically.

>  On Fri, 2008-10-24 at 17:45 -0200, Guilherme Polo wrote:
>  On Fri, Oct 24, 2008 at 5:31 PM, Allen Taylor
> <Allen.Taylor at mdacorporation.com> wrote:
> > Hi Guilherme,
> > The versions of Python and Tk used are the default packages installed on
> > Linux (Ubuntu 6.06 LTS). I don't know whether the default install is
> > compiled with --enable-threads or not. So, mtTkinter seems to be handy if
> > you want to use the default installed packages?
> > Allen
> >
> To check for --enable-threads try this:
> import Tkinter
> Tkinter.Tk().getvar("tcl_platform(threaded)")
> You will either get 1, or a TclError.
> Also, I though you said latest tcl/tk versions but clearly tcl/tk
> packages in Ubuntu 6.06 are not the latest.
> But they also won't make a difference if you didn't recompile python
> with these packages, I'm just telling this because right now Ubuntu
> includes tcl/tk 8.5 but distributes python-tk compiled against tcl/tk
> 8.4.
> Maybe you could include one example where this new module makes a
> difference ? We could be talking about different things. For instance,
> this:
> import Tkinter
> def test(text_widget):
>  text_widget.config(text='abcdef')
> def try_it(text_widget):
>  threading.Thread(target=test, args=(text_widget, )).start()
> lbl = Tkinter.Label()
> lbl.pack()
> lbl.after(10, lambda: try_it(lbl))
> lbl.mainloop()
> Would crash if tcl/tk were not compiled with --threads-enabled, but
> then using your module it will run just fine.

-- Guilherme H. Polo Goncalves

More information about the Tkinter-discuss mailing list