FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-88758: Handle non-tkinter widgets in tkinter focus methods (GH-152337) by miss-islington · Pull Request #152346 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 16 additions & 0 deletions Lib/test/test_tkinter/test_misc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,22 @@ def test_focus_methods(self):
self.root.update()
self.assertIs(self.root.focus_get(), b)

def test_focus_methods_unresolvable(self):
# The focus may be on a widget that tkinter did not create and so
# cannot map to an instance (e.g. a torn-off menu). The focus
# methods return None instead of raising KeyError (gh-88758).
menu = tkinter.Menu(self.root, tearoff=1)
menu.add_command(label='Hello')
tearoff = self.root.tk.call('tk::TearOffMenu', str(menu), 0, 0)
self.addCleanup(self.root.tk.call, 'destroy', tearoff)
self.root.update()
self.assertRaises(KeyError, self.root.nametowidget, tearoff)

self.root.tk.call('focus', '-force', tearoff)
self.root.update()
self.assertIsNone(self.root.focus_get())
self.assertIsNone(self.root.focus_displayof())

def test_grab(self):
f = tkinter.Frame(self.root)
f.pack()
Expand Down
20 changes: 16 additions & 4 deletions Lib/tkinter/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,10 @@ def focus_get(self):
the focus."""
name = self.tk.call('focus')
if name == 'none' or not name: return None
return self._nametowidget(name)
try:
return self._nametowidget(name)
except KeyError:
return None

def focus_displayof(self):
"""Return the widget which has currently the focus on the
Expand All @@ -836,14 +839,20 @@ def focus_displayof(self):
Return None if the application does not have the focus."""
name = self.tk.call('focus', '-displayof', self._w)
if name == 'none' or not name: return None
return self._nametowidget(name)
try:
return self._nametowidget(name)
except KeyError:
return None

def focus_lastfor(self):
"""Return the widget which would have the focus if top level
for this widget gets the focus from the window manager."""
name = self.tk.call('focus', '-lastfor', self._w)
if name == 'none' or not name: return None
return self._nametowidget(name)
try:
return self._nametowidget(name)
except KeyError:
return None

def tk_focusFollowsMouse(self):
"""The widget under mouse will get automatically focus. Can not
Expand Down Expand Up @@ -1246,7 +1255,10 @@ def winfo_containing(self, rootX, rootY, displayof=0):
+ self._displayof(displayof) + (rootX, rootY)
name = self.tk.call(args)
if not name: return None
return self._nametowidget(name)
try:
return self._nametowidget(name)
except KeyError:
return None

def winfo_depth(self):
"""Return the number of bits per pixel."""
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:meth:`!tkinter.Misc.focus_get`, :meth:`!focus_displayof`,
:meth:`!focus_lastfor` and :meth:`!winfo_containing` now return ``None``
instead of raising :exc:`KeyError` when the widget was not created by
:mod:`tkinter` (for example a torn-off menu).
Loading

Back | FazBrowse Home | New Git URL