New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better repr for tkinter widgets #64835
Comments
Here is a patch which adds more helpful repr for Tkinter widgets. Was
Becomes:
or (if you assigned names to widgets)
This is very helpful for debugging. |
I like this. It would make naming widgets more useful. I checked one Idle dialog and nothing is named. I suspect this is typical. The proposed change would break a doctest that follows the fix in the doctest manual. >>> C() # the default repr() for instances embeds an address
<__main__.C instance at 0x00AC18F0> The ELLIPSIS directive gives a nice approach for the last example:
>>> C() #doctest: +ELLIPSIS
<__main__.C instance at 0x...> I think the recommendation should better be <main.C instance ...> except that 'instance' is now 'object' -- and *that* change must have broken much more that this one would. |
Not knowing the internal of tkinter, this seems somewhat confusing.
This already looks more useful. How is that determined? Why the "first" object is missing (i.e. .panel seems to be an attribute of a missing object)? Regarding the patch, are you sure that .__class__.__module__ is always available? I seem to remember that it might be missing in some cases, e.g. modules written in C (but I might be confusing it with something else like __file__). |
If the name parameter is not specified, repr(id(self)) is used. Here is a
Tk widgets are organized in hierarchical structure and names look similar to
Yes, for example __module__ is absent in _tkinter.TkappType. But I think that |
Are there any questions or objections? |
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. |
New changeset 66770f126c71 by Serhiy Storchaka in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: