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.

Title: Tkinter focus_get on menu results in KeyError
Type: behavior Stage: resolved
Components: Tkinter Versions: Python 2.7
Status: closed Resolution: duplicate
Dependencies: Superseder: Make's nametowidget work with cloned menu widgets
View: 734176
Assigned To: Nosy List: jgoeders, r.david.murray, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2013-08-08 16:55 by jgoeders, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg194693 - (view) Author: Jeffrey Goeders (jgoeders) Date: 2013-08-08 16:55
I am experiencing a crash when calling focus_get() when a Tk Menu has focus.  Here is sample code, just click the menu and it will crash.

import Tkinter as tk

class App(tk.Tk):
    def __init__(self, *args, **kw):
        tk.Tk.__init__(self, *args, **kw)
        self.entry = tk.Entry(self)
        self.entry.grid(padx=20, pady=20)
        self.entry.bind("<FocusOut>", self.entry_focus_lost)
        self.menubar = tk.Menu(self)
        self["menu"] = self.menubar
        self.menufile = tk.Menu(self.menubar, tearoff=False)
        self.menubar.add_cascade(menu=self.menufile, label="File")

    def entry_focus_lost(self, event):
        widget_with_focus = self.focus_get()

app = App()

Here is the error stack:
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib64/python2.7/lib-tk/", line 1410, in __call__
    return self.func(*args)
  File "/home/jgoeders/workspace/tester/", line 19, in entry_focus_lost
    widget_with_focus = self.focus_get()
  File "/usr/lib64/python2.7/lib-tk/", line 446, in focus_get
    return self._nametowidget(name)
  File "/usr/lib64/python2.7/lib-tk/", line 1079, in nametowidget
    w = w.children[n]
KeyError: '#140030874747536'
msg194702 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-08-08 17:58
We use the 'crash' type for interpreter crashes (segfaults).  So goes under the 'behavior' type.
msg194849 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-08-10 23:24
I am not able to reproduce any problem on Win 7 with either recent 2.7.6a0 (repository) or 3.3.2. Nothing happens if I click on [File] or anywhere else outside of the entry box or [_]...[X] window buttons. Please give the signin info such as "Python 2.7.5+ (default, Jul  1 2013, 14:50:57) [MSC v.1500 32 bit (Intel)] on win32" and update if you do not have 2.7.5.
msg195927 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-08-22 22:02
This is a duplicate of issue734176.

Thank you for your example Jeffrey.
Date User Action Args
2022-04-11 14:57:49adminsetgithub: 62886
2013-08-22 22:02:47serhiy.storchakasetstatus: open -> closed

superseder: Make's nametowidget work with cloned menu widgets

nosy: + serhiy.storchaka
messages: + msg195927
resolution: duplicate
stage: resolved
2013-08-10 23:24:09terry.reedysetnosy: + terry.reedy
messages: + msg194849
2013-08-08 17:59:22r.david.murraysettitle: Tkinter focus_get on menu causes crash -> Tkinter focus_get on menu results in KeyError
2013-08-08 17:58:38r.david.murraysettype: crash -> behavior

messages: + msg194702
nosy: + r.david.murray
2013-08-08 16:55:42jgoederscreate