strange Tkinter problem

Syver Enstad syver-en+usenet at online.no
Wed Dec 26 10:56:59 EST 2001


tjiit <tjiit at yahoo.com> writes:
> On Wed, 26 Dec 2001 08:38:09 +0100, Luke wrote:
> 
> > This is behavior common to pretty much all GUI's.  Your callback
> > function triggered by a button click should run in a new thread, or
> > spawn a new thread in which the networking code runs.  
> Does this mean that the GUI should do this or me?

You have to do it. The GUI has no way of knowing that you want to
start a thread. 

It's easy to create threads in python. Use the
threading module. 

import threading

Derive a new class from threading.Thread:

class MyTask(threading.Thread):

Define a run method that does the work you want to have done:

    def run(self):
        # code written here will run in it's own thread

Call you threads start method:

task = MyTask()
task.start()


Be aware that you should NOT call tkinter methods from the run method
of the thread object, because tkinter doesn't support this.

Check out the after method in tkinter and the Queue.Queue class for
ways to get your data when the thread finishes. See also the reference
on the join method and isAlive for the threading.Thread class.

Mail or post if you need further help.
-- 

Vennlig hilsen 

Syver Enstad



More information about the Python-list mailing list