[Tkinter-discuss] Tkinter GUI freezing

Michael Lange klappnase at web.de
Thu Apr 19 20:36:09 CEST 2012


Thus spoketh "Bharath Reddy A." <bharath270392 at gmail.com> 
unto us on Thu, 19 Apr 2012 21:02:58 +0530:

> Hi all,
> I have created a GUI using Tkinter.
> I have a button "GO", which when clicked calls a function (mainFunc).
> That function takes around 60 seconds to execute and in the mean time
> outputs many numbers which have to be displayed in a list in the GUI.
> I have attempted the following:
>    1.  I created a thread whose target = mainFunc, when the button is
>    clicked. I can see the function printing stuff in terminal. But the
> GUI still freezes.
>    2.  When I tried to create a  thread whose target = root.mainloop(),
> the GUI doesn't display at all, (Python.app launches but the GUI is not
>    displayed),

One thing you must *never* do is to mix the Tkinter gui thread, which in
fact should always be the main program thread, with any child thread. If
the gui needs to reflect progress of the callbacks in the child thread,
you must never call some Tkinter methods directly from within the child
thread, but update some variable values and query them (typically
periodically) from the gui thread.

> *Can you please help me how to create threads exactly???*
> I am attaching my code along with the mail
> main. mainFunction() is the function that takes approx 60 seconds to
> execute and that should be running in parallel with GUI/ in background.
> Thanks in advance...

The problem in your code is aparently the following line:

   thread1 = threading.Thread(target = main.mainFunction(host, uname,
                 passwd, networkPart, startHost, endHost, diction))

This way the mainFunction() call is done just when the interpreter
evaluates this line. See the library reference about Thread objects:

"class Thread(group=None, target=None, name=None, args=(), kwargs={})
  This constructor should always be called with keyword arguments.
  Arguments are: 
  group should be None; reserved for future extension when a ThreadGroup
  class is implemented. 
  target is the callable object to be invoked by the run() method.
  Defaults to None, meaning nothing is called. 
  name is the thread name. By default, a unique name is constructed of the
  form ``Thread-N'' where N is a small decimal number. 
  args is the argument tuple for the target invocation. Defaults to (). 
  kwargs is a dictionary of keyword arguments for the target invocation.
  Defaults to {}."

I hope this helps


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

You go slow, be gentle.  It's no one-way street -- you know how you
feel and that's all.  It's how the girl feels too.  Don't press.  If
the girl feels anything for you at all, you'll know.
		-- Kirk, "Charlie X", stardate 1535.8

More information about the Tkinter-discuss mailing list