Tkinter "wait_window()" will hang and not spawn window from root with "after_idle()" on MacOS

Open
#100,617 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python
Domain
desktop

Research direction

Start by running the provided minimal reproducer on macOS, focusing on the interaction between Tkinter's after_idle(), Toplevel.show(), and wait_window(). Compare the behavior with the documented after() workaround and Python 3.9. Done means the waited-for window appears before the root window without hanging, while preserving the returned value.

Written by the indexing model from the issue text.

Description

OS-mac topic-tkinter type-bug

Bug report

Using Toplevel wait_window() from Tkinter on MacOS via after_idle() before any root windows will cause the process to hang and the windows are never spawned.

Expected behavior

The waited for window (Toplevel) will display before the main window (Root). Once some action is taken on the waited for window, in this case "Press me" button, the root window will be displayed. The user cannot access or see the root window until the "Press me" button is pressed.

Actual behavior
  • Using after_idle() - neither window spawns, application hangs
  • Using a button to call the waited-for-window from root, the waited-for-window spawns and will return a value to root
  • Using after() and withdraw/deiconify - expected behavior via workaround

MREs follows:

This will not work due to the waited window being called before the root window is displayed using after_idle.

import tkinter as tk

class MainWindow(tk.Tk):
  def __init__(self):
    super().__init__()
    self.title("Hello")
    tk.Label(self, text='Hi there').pack()

    self.after_idle(self.waitforit)
    self.mainloop()
  
  def waitforit(self):
    x = WaitedForWindow(self).show()
    print(x)

class WaitedForWindow(tk.Toplevel):
  def __init__(self, parent: tk.Tk):
    tk.Toplevel.__init__(self, parent)

    tk.Button(self, text='Press me', command=self.destroy).pack()

  def show(self):
    self.deiconify()
    self.wait_window()
    return True

if __name__ == "__main__":
  MainWindow()

Spawning a window (without calling the .show() above) will work fine, but a value cannot be returned of course, and both windows will spawn. Running the same MRE in 3.9.0 works fine.

However, if the main window's class is changed to spawn the waited window via button-press, everything works fine, but the behavior is not what we want:

# self.after_idle(self.waitforit)
tk.Button(self, text="Wait for window", command=self.waitforit).pack()

or by using after() [current workaround] we get the correct behavior via some extra steps.

class MainWindow(tk.Tk):
  def __init__(self):
    super().__init__()
    self.title("Hello")
    tk.Label(self, text='Hi there').pack()

    self.after(0, self.waitforit)
    self.after(0, self.withdraw)
    self.mainloop()
  
  def waitforit(self):
    x = WaitedForWindow(self).show()
    print(x)
    self.deiconify()

Your environment

  • CPython versions tested on (using MRE 1):
    • 3.9.0 (working) [^+]
    • 3.10.9 (non-functional) [^]
    • 3.11.1 (non-functional) [^+]
    • 3.12.0a3 (non-functional) [^]
    • ... all 64-bit
  • Operating system and architecture: MacOS Mojave 10.14.6 x64 (^), MacOS Monterey 12.6.2 (+)
  • Tkinter version: 8.6.12 [^+]
  • 3.9.0, 3.11.1 tested in and out of bare virtualenv [^+]
  • additional tests from colleagues:
    • W11, 3.10.5 (working)
Linked PRs
  • gh-152620
Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.