[Python-checkins] cpython: Issue #27025: Generated names for Tkinter widgets are now more meanful

serhiy.storchaka python-checkins at python.org
Mon Jun 13 02:24:37 EDT 2016


https://hg.python.org/cpython/rev/304c61263ae6
changeset:   101974:304c61263ae6
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Jun 13 09:24:11 2016 +0300
summary:
  Issue #27025: Generated names for Tkinter widgets are now more meanful
and recognizirable.

files:
  Lib/tkinter/__init__.py                    |  16 +++++++--
  Lib/tkinter/test/test_tkinter/test_misc.py |   8 +++++
  2 files changed, 20 insertions(+), 4 deletions(-)


diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -489,6 +489,9 @@
 
     Base class which defines methods common for interior widgets."""
 
+    # used for generating child widget names
+    _last_child_ids = None
+
     # XXX font command?
     _tclCommands = None
     def destroy(self):
@@ -2174,7 +2177,15 @@
             name = cnf['name']
             del cnf['name']
         if not name:
-            name = repr(id(self))
+            name = self.__class__.__name__.lower()
+            if master._last_child_ids is None:
+                master._last_child_ids = {}
+            count = master._last_child_ids.get(name, 0) + 1
+            master._last_child_ids[name] = count
+            if count == 1:
+                name = '`%s' % (name,)
+            else:
+                name = '`%s%d' % (name, count)
         self._name = name
         if master._w=='.':
             self._w = '.' + name
@@ -3392,9 +3403,6 @@
         if not name:
             Image._last_id += 1
             name = "pyimage%r" % (Image._last_id,) # tk itself would use image<x>
-            # The following is needed for systems where id(x)
-            # can return a negative number, such as Linux/m68k:
-            if name[0] == '-': name = '_' + name[1:]
         if kw and cnf: cnf = _cnfmerge((cnf, kw))
         elif kw: cnf = kw
         options = ()
diff --git a/Lib/tkinter/test/test_tkinter/test_misc.py b/Lib/tkinter/test/test_tkinter/test_misc.py
--- a/Lib/tkinter/test/test_tkinter/test_misc.py
+++ b/Lib/tkinter/test/test_tkinter/test_misc.py
@@ -12,6 +12,14 @@
         f = tkinter.Frame(t, name='child')
         self.assertEqual(repr(f), '<tkinter.Frame object .top.child>')
 
+    def test_generated_names(self):
+        t = tkinter.Toplevel(self.root)
+        f = tkinter.Frame(t)
+        f2 = tkinter.Frame(t)
+        b = tkinter.Button(f2)
+        for name in str(b).split('.'):
+            self.assertFalse(name.isidentifier(), msg=repr(name))
+
     def test_tk_setPalette(self):
         root = self.root
         root.tk_setPalette('black')

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list