Skip to content
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

Closed
serhiy-storchaka opened this issue Feb 15, 2014 · 7 comments
Closed

Better repr for tkinter widgets #64835

serhiy-storchaka opened this issue Feb 15, 2014 · 7 comments
Assignees
Labels
topic-tkinter type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 20636
Nosy @terryjreedy, @ezio-melotti, @serhiy-storchaka
Files
  • tkinter_misc_repr.patch
  • 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:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2014-04-13.18:03:11.171>
    created_at = <Date 2014-02-15.11:53:01.767>
    labels = ['type-feature', 'expert-tkinter']
    title = 'Better repr for tkinter widgets'
    updated_at = <Date 2014-04-13.18:03:11.170>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2014-04-13.18:03:11.170>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2014-04-13.18:03:11.171>
    closer = 'serhiy.storchaka'
    components = ['Tkinter']
    creation = <Date 2014-02-15.11:53:01.767>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['34089']
    hgrepos = []
    issue_num = 20636
    keywords = ['patch']
    message_count = 7.0
    messages = ['211269', '211879', '211995', '212001', '214250', '214284', '215515']
    nosy_count = 5.0
    nosy_names = ['terry.reedy', 'gpolo', 'ezio.melotti', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue20636'
    versions = ['Python 3.5']

    @serhiy-storchaka
    Copy link
    Member Author

    Here is a patch which adds more helpful repr for Tkinter widgets. Was

    <tkinter.Button object at 0xb6aa6964>
    

    Becomes:

    <tkinter.Button object .3070343372.3066782348>
    

    or (if you assigned names to widgets)

    <tkinter.Button object .panel.b1>
    

    This is very helpful for debugging.

    @serhiy-storchaka serhiy-storchaka added topic-tkinter type-feature A feature request or enhancement labels Feb 15, 2014
    @terryjreedy
    Copy link
    Member

    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.

    @ezio-melotti
    Copy link
    Member

    <tkinter.Button object .3070343372.3066782348>

    Not knowing the internal of tkinter, this seems somewhat confusing.
    Is that an "anonymous name/id"?

    <tkinter.Button object .panel.b1>

    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__).

    @serhiy-storchaka
    Copy link
    Member Author

    > <tkinter.Button object .3070343372.3066782348>

    Not knowing the internal of tkinter, this seems somewhat confusing.
    Is that an "anonymous name/id"?

    If the name parameter is not specified, repr(id(self)) is used. Here is a
    button with id() == 3066782348, its parent has id() == 3070343372 and its
    grandparent is root. str() for this button returns full name
    ".3070343372.3066782348".

    > <tkinter.Button object .panel.b1>

    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)?

    Tk widgets are organized in hierarchical structure and names look similar to
    file system names. "." is the root, ".frame" is a frame in the root,
    ".frame.b1" is a button in frame ".frame".

    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__).

    Yes, for example __module__ is absent in _tkinter.TkappType. But I think that
    every Python implemented class has __module__.

    @serhiy-storchaka
    Copy link
    Member Author

    Are there any questions or objections?

    @serhiy-storchaka serhiy-storchaka self-assigned this Mar 20, 2014
    @terryjreedy
    Copy link
    Member

    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-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 4, 2014

    New changeset 66770f126c71 by Serhiy Storchaka in branch 'default':
    Issue bpo-20636: Improved the repr of Tkinter widgets.
    http://hg.python.org/cpython/rev/66770f126c71

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-tkinter type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants