1

I am trying to build an application with a main window, and a background thread that checks a certain condition in a loop, and when the condition is met, brings the main window to the front. To bring the window to the front i use the lift() method, but it doesn't work. So I did the following experiment from the python command line:

from Tkinter import *
root = Tk()

A tk window appears. Minimize the window and then:

root.lift()

Nothing happens. I also tried start the window's mainloop before lifting:

import thread
# start mainloop in separate thread so we can continue typing
thread.start_new_thread(root.mainloop, ())
root.lift()

Again, nothing happens. In my actual code it is even worse - once I call lift(), the window is stuck and stops responding.

What am I doing wrong?

(I'm using Python 2.7.2 on Windows 7.)

roel
  • 109
  • 3
  • 11

1 Answers1

1

You cannot run the mainloop in a thread different from the one where you create the widgets. In fact, you can't interact with widgets at all from another thread.

To revert the effects of minimizing the window you need to use the deiconify method.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Ok. I tried deiconify in the above experiment and it works. Thanks. Now how do I use a thread to update the window? Is there some window messaging mechanism? – roel Jun 30 '13 at 14:14
  • 1
    You need to use a Queue. There are a few examples on stackoverflow. For example, http://stackoverflow.com/a/1198288/7432 – Bryan Oakley Jun 30 '13 at 14:27
  • Thanks for the tip. I used the `after` method from the example in the link and it solved my problem. – roel Jun 30 '13 at 14:49