[issue20636] Better repr for tkinter widgets

Terry J. Reedy report at bugs.python.org
Thu Mar 20 22:07:44 CET 2014


Terry J. Reedy added the comment:

After looking more at testing entire (Idle) dialogs and windows for sanity, I like the idea even more. A person can check that everything that is present looks ok, but code is at least as good as using a checklist to verify that what is present is exactly what should be present. For this, widgets should have meaningful and predictable names. Once widgets are given names, they should be used in the representation. The proposed

>>> top.winfo_children()
[<tkinter.Frame object .top.child]

is more helpful than the current

>>> top.winfo_children()
[<tkinter.Frame object at 0x000000000350BCF8>]


As for Ezio's question: The new method is defined in class tkinter.Misc and only applies to instances of subclasses thereof -- BaseWidget, Widget, etc, in tkinter.__init__ and ttk.Widget. I suppose other code might subclass something, but as far as I know, Idle classes 'have a' widget rather than 'being a' widget. But if Misc or a subclass could be sensibly subclassed in C code, you could, to be safe, change "top.__class__.__module__" to "getattr(top.__class__, '__module__', 'module'). I believe 'self._w' is safe as an object without ._w is not a tk widget.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20636>
_______________________________________


More information about the Python-bugs-list mailing list