Message397199
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 |
|
Date |
User |
Action |
Args |
2021-07-09 15:14:18 | Akuli | set | recipients:
+ Akuli |
2021-07-09 15:14:18 | Akuli | set | messageid: <1625843658.56.0.054020011522.issue44592@roundup.psfhosted.org> |
2021-07-09 15:14:18 | Akuli | link | issue44592 messages |
2021-07-09 15:14:18 | Akuli | create | |
|