Here’s the test case…
import Tkinter as tk
import thread
from time import sleep
if __name__ == '__main__':
t = tk.Tk()
thread.start_new_thread(t.mainloop, ())
# t.iconbitmap('icon.ico')
b = tk.Button(text='test', command=exit)
b.grid(row=0)
while 1:
sleep(1)
This code works. Uncomment the t.iconbitmap line and it locks. Re-arrange it any way you like; it will lock.
How do I prevent tk.mainloop locking the GIL when there is an icon present?
The target is win32 and Python 2.6.2.
I believe you should not execute the main loop on a different thread. AFAIK, the main loop should be executed on the same thread that created the widget.
The GUI toolkits that I am familiar with (Tkinter, .NET Windows Forms) are that way: You can manipulate the GUI from one thread only.
On Linux, your code raises an exception:
One of the following will work (no extra threads):
With extra thread:
If you need to do communicate with tkinter from outside the tkinter thread, I suggest you set up a timer that checks a queue for work.
Here is an example: