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

gh-38464: Make tkinter nametowidget() work with cloned menus by serhiy-storchaka · Pull Request #152336 · 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
26 changes: 26 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 @@ -390,6 +390,32 @@ def test_nametowidget(self):
self.assertIs(self.root.nametowidget(str(b)), b)
self.assertRaises(KeyError, self.root.nametowidget, '.nonexistent')

def test_nametowidget_menu_clone(self):
# A menu used as a menubar or cascade is cloned by Tk under an
# auto-generated name (each path component is the original name
# prefixed with one or more '#' clone markers). nametowidget()
# maps such a name back to the original widget (gh-38464).
menubar = tkinter.Menu(self.root)
filemenu = tkinter.Menu(menubar, tearoff=0)
menubar.add_cascade(label='File', menu=filemenu)
submenu = tkinter.Menu(filemenu, tearoff=0)
filemenu.add_cascade(label='More', menu=submenu)
self.root['menu'] = menubar
self.root.update_idletasks()

originals = {menubar, filemenu, submenu}
clones = []
def collect(parent):
for name in self.root.tk.splitlist(
self.root.tk.call('winfo', 'children', parent)):
clones.append(name)
collect(name)
collect('.')
# Every menu (originals and clones) resolves to an original widget.
self.assertTrue(any('#' in name for name in clones))
for name in clones:
self.assertIn(self.root.nametowidget(name), originals)

def test_focus_methods(self):
f = tkinter.Frame(self.root, width=150, height=100)
f.pack()
Expand Down
11 changes: 10 additions & 1 deletion 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 @@ -1788,7 +1788,16 @@ def nametowidget(self, name):
for n in name:
if not n:
break
w = w.children[n]
try:
w = w.children[n]
except KeyError:
# Menu clones (a menu used as a menubar or a cascade) get
# auto-generated names where each path component is the
# original name prefixed with one or more '#' clone markers.
# Map such a name back to the original widget.
if not n.startswith('#'):
raise
w = w.children[n.rsplit('#', 1)[-1]]

return w

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,3 @@
:meth:`!tkinter.Misc.nametowidget` now resolves the auto-generated names of
cloned menus (a menu used as a menubar or a cascade) back to the original
widget.
Loading

Back | FazBrowse Home | New Git URL