New submission from Akuli <akuviljane...@gmail.com>:
The purpose of focus_get() is to return the widget that currently has the focus. It tries to convert its result to a tkinter widget, which can fail, because not all Tk widgets are known to tkinter. Consider this, for example: import tkinter def print_focused_widget(): print(repr(root.focus_get())) root.after(1000, print_focused_widget) root = tkinter.Tk() menu = root["menu"] = tkinter.Menu() menu.add_cascade(label="Click here", menu=tkinter.Menu()) print_focused_widget() tkinter.mainloop() Output, with menu clicked after a couple seconds (on Linux): None <tkinter.Tk object .> <tkinter.Tk object .> Exception in Tkinter callback Traceback (most recent call last): File "/home/akuli/.local/lib/python3.10/tkinter/__init__.py", line 1916, in __call__ return self.func(*args) File "/home/akuli/.local/lib/python3.10/tkinter/__init__.py", line 838, in callit func(*args) File "/home/akuli/porcu/foo.py", line 4, in print_focused_widget print(repr(root.focus_get())) File "/home/akuli/.local/lib/python3.10/tkinter/__init__.py", line 782, in focus_get return self._nametowidget(name) File "/home/akuli/.local/lib/python3.10/tkinter/__init__.py", line 1531, in nametowidget w = w.children[n] KeyError: '#!menu' Some nametowidget() calls in tkinter/__init__.py already handle this correctly. Consider winfo_children(), for example: try: # Tcl sometimes returns extra windows, e.g. for # menus; those need to be skipped result.append(self._nametowidget(child)) except KeyError: pass ---------- components: Tkinter messages: 397199 nosy: Akuli priority: normal severity: normal status: open title: tkinter focus_get() with non-tkinter Tk widget type: behavior versions: Python 3.10 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue44592> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com