Pmw Error: "TclError: can't unset "unknown_pending(": no suchvariable"

Chad Netzer cnetzer at mail.arc.nasa.gov
Tue Feb 18 16:53:28 EST 2003


On Sun, 2003-02-16 at 14:10, Marc wrote:

> Which brings me to the proposed solution, the queue. How does a queue get
> around this problem when you are still calling the function from somewhere
> else? If you place a function call inside of a queue, doesn't it still
> originate from the second thread, even though it's popped out in the main
> thread?

I'm really not the one to ask, since I avoid threads like the plague. 
But, my (naive) answer is that the Python threading model interacts with
Tkinter.mainloop() in certain ways, so that only the thread that is
executing mainloop() should be making TK calls..  Let's say that thread
A is the one running Tkinter's mainloop().  Now when thread B tries to
call a thread A function to do a GUI operation, then thread A is NOT
executing.  Thread B is executing (even if it is calling thread A code;
after all, it is exactly the same as if thread B called the same code
belonging to itself, or of thread C, etc.)  But, when you update the
GUI, thread A MUST be the currently executing thread, or else problems
like you have encountered may and will occur.

I hope that makes sense.  Just remember that only ONE thread is
executing at a time (in python, threads are strictly serial; they do NOT
execute in parallel if you have multiple CPUs).  If thread A is running
mainloop(), and thread B calls a thread A function, Python is still
executing in the thread B context.  It doesn't automatically switch into
thread A to make the call.

So, people typically use a Queue for this.  Remember that thread A
(after any initial set-up) will always be executing mainloop() when it
is it's turn to run.  If you have no event handler set up, nothing will
happen.  So, typically, you will setup a callback to be executed (using
after_idle() I suppose) that will check the Queue for events, and do the
work of calling GUI controls based on those Queued request.  Yes, it is
more cumbersome than calling GUI functions from other threads.  But, it
has the advantage of working. :)  Look at F. Lundh's example below (the
first link) for an idea of how to proceed.

Also, I would suggest you repost your question with a more on-topic
subject header if you have followup questions (the current one may not
catch those you can help with Tkinter and threading issues.)

Also, here are some links I found which may be of some small help.

http://effbot.org/zone/tkinter-threads.htm
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965
http://mail.python.org/pipermail/thread-sig/1998-February/000144.html

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)







More information about the Python-list mailing list