This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Tkinter wait_visibility hanging when used in thread
Type: behavior Stage:
Components: Tkinter Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: epaine, serhiy.storchaka, spcmicro, spcmicro2, terry.reedy
Priority: normal Keywords:

Created on 2020-11-28 10:27 by spcmicro, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
threadTest.zip spcmicro, 2020-11-28 10:27 Contains source files and screen captures
Messages (6)
msg381990 - (view) Author: Steve Carll (spcmicro) Date: 2020-11-28 10:27
I'm having a problem with an application that uses threads and TKinter. Sometimes everything works fine but there are 2 errors that I can't seem to figure out why they are happening. The first error is the GUI completely locking up with no text cursor in the textbox. The second problem is a crash after the input has been entered.
I have created a bare minimum test application that demonstrates the problem. The problems are easily reproducible.
I've attached a simpleteTKinter test application that I created using Page. I've also attached 3 screen captures. 
1. threadTest-OK.jpg is proper behavior
2. threadTest-Hung.jpg shows the unresponsive application
3. threadTest-Exception.jpg shows the exception thrown when input window is closed
It looks like a race condition to me. I've tried putting in delays and using after but I can't seem to work around it. 
Am I doing something wrong here?
msg382001 - (view) Author: E. Paine (epaine) * Date: 2020-11-28 14:16
I agree this is a race condition, and have narrowed the issue down to wait_visibility with the following also hanging indefinitely (tested against the master branch):

import threading
import tkinter as tk
def create_tp():
    t = tk.Toplevel()
    t.wait_visibility()
root = tk.Tk()
tk.Button(root, text="Create", command=lambda:
          threading.Thread(target=create_tp).start()).pack()
root.mainloop()

I agree this is a race condition (presumably between wait_visibility and window render) and am pretty sure there is nothing either us or the Tk team can do (IMO, this issue should be closed - though thank you for reporting it).
Similar behaviour to issue-42142, though I don't believe threading is used there.

> Am I doing something wrong here?
Using threads?! tkinter is known to not like threads and there are several bugs which can cause the Python interpreter to crash.
msg382006 - (view) Author: Steve Carll (spcmicro) Date: 2020-11-28 14:59
Thank you to epaine for the quick reply.
IMHO this should be elevated and addressed.
TKinter not working well with threads appears to be a well known problem that a lot of developers have encountered and have had to work around.
The use of threads is not something that should be avoided, but rather used when needed in the appropriate manner. They've been around for a long time and work well in many development environments on a multitude of platforms.
I think it's time to make TKinter thread safe. Just my opinion...
msg382011 - (view) Author: E. Paine (epaine) * Date: 2020-11-28 16:34
You make very good points but sadly it is not that simple. The issue is described in detail in the comment found in Modules/_tkinter.c:178. A good summary is probably the comment for Tkapp_Call (Modules/_tkinter.c:1522).
msg399873 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-08-18 23:32
tcl/tk can be compiled without or with thread support.  For 8.5, the default was without.  For 8.6, the default is with.  The Windows and macOS installers include 8.6 compiled with the default.  I have not previously heard of problems with thread when it is so compiled.  Both of you, what tcl versions?  how compiled?
msg399914 - (view) Author: Steve Carll (spcmicro2) Date: 2021-08-19 12:59
This is a Tkinter problem and not a TK problem.
I ended up working around the problem by forcing garbage collection before opening and after closing any Tkinter windows that are created from other Tkinter windows. Since adding this code I have not had the problem.
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86657
2021-08-19 12:59:09spcmicro2setnosy: + spcmicro2
messages: + msg399914
2021-08-18 23:32:44terry.reedysetnosy: + terry.reedy
messages: + msg399873
2020-11-28 16:34:30epainesetmessages: + msg382011
2020-11-28 14:59:44spcmicrosetmessages: + msg382006
2020-11-28 14:16:26epainesetversions: + Python 3.8, Python 3.9, Python 3.10, - Python 3.7
nosy: + serhiy.storchaka, epaine
title: TKinter hanging intermittently - sometimes throwing exception -> Tkinter wait_visibility hanging when used in thread
messages: + msg382001

2020-11-28 10:27:46spcmicrocreate