python / python/cpython

tkinter.simpledialog: KeyError when a dialog is opened while a window not created by tkinter holds the grab

Open
#157,676 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-tkinter type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

simpledialog.SimpleDialog.go() and Dialog use _temp_grab_focus(), which calls grab_current() and focus_get() to restore the previous grab and focus afterwards. Both convert the Tcl window name with _nametowidget(), which raises KeyError for windows which were not created by tkinter, such as the native message box (.__tk__messagebox) or Tk's file dialogs on X11.

import tkinter
from tkinter import messagebox, simpledialog
root = tkinter.Tk()
root.after(300, lambda: simpledialog.SimpleDialog(root, text='hi', buttons=['OK']).go())
messagebox.showinfo('t', 'm', parent=root)
KeyError: '__tk__messagebox'

This happens in IDLE when a dialog is opened from an event delivered while a message box is shown (#153481). Regression from #151848 (3.16).

Linked PRs
  • gh-157677

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.

Research direction

Start by reading tkinter.simpledialog.SimpleDialog.go(), Dialog, and _temp_grab_focus(), then run the supplied messagebox and simpledialog reproduction. The fix is complete when opening a dialog while a non-tk window holds the grab no longer raises KeyError and the previous grab and focus are restored.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.