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: Make's nametowidget work with cloned menu widgets
Type: behavior Stage: patch review
Components: Tkinter Versions: Python 3.6, Python 3.5, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, gpolo, gregcouch, jepler, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2003-05-07 20:11 by gregcouch, last changed 2022-04-10 16:08 by admin.

File name Uploaded Description Edit gregcouch, 2003-05-07 20:11 nametowidget patch for cloned widgets
nametowidget_clonedmenus.diff gpolo, 2008-12-31 17:04 review
issue734176_py3.patch BreamoreBoy, 2013-05-20 16:40 Python 3 patch review
nametowidget_clonedmenus_2.diff serhiy.storchaka, 2013-08-22 22:02 review
nametowidget_clonedmenus_3.patch serhiy.storchaka, 2016-07-24 05:32 review
Messages (13)
msg43671 - (view) Author: Greg Couch (gregcouch) Date: 2003-05-07 20:11
Tk will clone menu widgets when they are torn off and
it gives them names at are similar to the original
widget's names.  The patch extends the nametowidget so
it will return the original widget when given the name
of a cloned widget.  We used this patch to provide
balloon help in cloned menus.  This patch isn't
perfect, it will fail on clones of clones, but it is a
big help.

This patch was made against the latest CVS version
1.173 of but works against older's too.

     - Greg Couch
msg43672 - (view) Author: Jeff Epler (jepler) Date: 2003-05-11 14:30
Logged In: YES 

I don't believe that the naming convention of cloned menus is actually documented anywhere (it's certainly not documented in menu(n) of tk-8.3.5 as packaged by RedHat.  The only promise that the documentation makes is that the clone "is a child of the original". (but that can be violated if "menu clone" is invoked directly:

% menu .m
% .m clone .n

That said, the convention for automatically generated clones hasn't varied in the versions of Tk I'm aware of (at least 8.2 through 8.4) so it might be reasonable to depend on this.

Does anybody in Python development also keep up with Tcl/Tk development?  It might be reasonable to ask them to document the format of automatically generated clone names.
msg78634 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-12-31 17:04
Eh.. old.
Anyway, I have made a patch against trunk now and it should work with
any nested level of cloned menus according to how tk names cloned menus.
msg114239 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-08-18 16:11
A small simple patch that I see no problems with.
msg114244 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-08-18 16:21
Can someone post a script demonstrating the proposed feature?  Is "clones of clones" issue mentioned by OP resolved in the latest patch?

Given that nobody commented on this issue for 7 years, I am skeptical about the utility of this feature.
msg189566 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2013-05-19 02:49
Ten years on does anybody actually care about this?  If you do the patch will need reworking as in Python 3 the code exists in lib/tkinter/
msg189637 - (view) Author: Greg Couch (gregcouch) Date: 2013-05-20 04:49
We still apply that patch to the Python that we embed in our application 
and have for the last 10 years :-)
msg189674 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2013-05-20 16:40
I've attached a patch for Python 3 which I hope is okay as it's my first attempt using TortoiseHg on Windows.
msg195928 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-08-22 22:02
issue18686 have a script which demonstrates the proposed feature.

However Guilherme's patch is a little wrong. Here is updated patch.

Note that even this patch is not absolute correct, because Tk adds count if new name is not unique. I.e. if .#12345 already exists it try .#123451, .#123452, .#123453, etc. It is impossible restore original name in general case.
msg270902 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-07-21 01:58
Serhiy, if you are sure your patch is an improvement, then I think you should commit it without waiting for perfection.

Only the other hand, #18686 does not demonstrate a problem *to me* as it does not fail for me.  Does anyone else reproduce the failure on a current non-Windows system?  Based just on what I can see, I would be inclined to close this.
msg271130 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-07-24 05:29
Issue18686 is reproducible to me. Here is a script based on issue18686 example. Run it and open the "File" menu.

$ python3 
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.5/tkinter/", line 1553, in __call__
    return self.func(*args)
  File "", line 24, in entry_focus_lost
    widget_with_focus = self.focus_get()
  File "/usr/lib/python3.5/tkinter/", line 550, in focus_get
    return self._nametowidget(name)
  File "/usr/lib/python3.5/tkinter/", line 1204, in nametowidget
    w = w.children[n]
KeyError: '#3069298188'

But my patch doesn't help in case of tearoff menu. Detach the "File" menu and hover a mouse on its item.

$ ./python 
Exception in Tkinter callback
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/tkinter/", line 1712, in __call__
    return self.func(*args)
  File "", line 24, in entry_focus_lost
    widget_with_focus = self.focus_get()
  File "/home/serhiy/py/cpython/Lib/tkinter/", line 692, in focus_get
    return self._nametowidget(name)
  File "/home/serhiy/py/cpython/Lib/tkinter/", line 1344, in nametowidget
    w = w.children[n]
KeyError: 'tearoff1'
msg271131 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-07-24 05:32
Following patch tries to handle the case when there are multiple clones  (but it doesn't help with tearoff menus).
msg397223 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-07-09 21:02
See #44592, closed as duplicate of this.
Date User Action Args
2022-04-10 16:08:38adminsetgithub: 38464
2021-07-09 21:02:53terry.reedysetmessages: + msg397223
2021-07-09 21:02:12terry.reedylinkissue44592 superseder
2021-07-09 20:46:54terry.reedyunlinkissue44592 superseder
2021-07-09 20:27:25serhiy.storchakalinkissue44592 superseder
2016-07-24 05:32:35serhiy.storchakasettype: enhancement -> behavior
versions: + Python 3.5, Python 3.6, - Python 3.3, Python 3.4
2016-07-24 05:32:07serhiy.storchakasetfiles: + nametowidget_clonedmenus_3.patch

messages: + msg271131
2016-07-24 05:29:20serhiy.storchakasetmessages: + msg271130
2016-07-21 01:58:15terry.reedysetnosy: + terry.reedy
messages: + msg270902
2014-02-03 17:02:52BreamoreBoysetnosy: - BreamoreBoy
2013-08-22 22:02:52serhiy.storchakasetfiles: + nametowidget_clonedmenus_2.diff
versions: + Python 2.7, Python 3.3, Python 3.4, - Python 3.2
nosy: + serhiy.storchaka

messages: + msg195928
2013-08-22 22:02:47serhiy.storchakalinkissue18686 superseder
2013-05-20 16:40:07BreamoreBoysetfiles: + issue734176_py3.patch

messages: + msg189674
2013-05-20 04:49:33gregcouchsetmessages: + msg189637
2013-05-19 02:49:56BreamoreBoysetmessages: + msg189566
2010-08-18 16:21:40belopolskysetnosy: + belopolsky
messages: + msg114244
2010-08-18 16:11:13BreamoreBoysetversions: + Python 3.2, - Python 2.6, Python 3.0, Python 3.1, Python 2.7
nosy: + BreamoreBoy

messages: + msg114239

type: enhancement
stage: patch review
2008-12-31 17:04:34gpolosetfiles: + nametowidget_clonedmenus.diff
nosy: + gpolo
messages: + msg78634
versions: + Python 2.6, Python 3.0, Python 3.1, Python 2.7, - Python 2.3
2003-05-07 20:11:52gregcouchcreate