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 event objects #71481

Closed
serhiy-storchaka opened this issue Jun 11, 2016 · 18 comments
Closed

Better repr for Tkinter event objects #71481

serhiy-storchaka opened this issue Jun 11, 2016 · 18 comments
Labels
topic-tkinter type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 27294
Nosy @terryjreedy, @serhiy-storchaka
Files
  • tkinter_event_repr.patch
  • tkinter_event_repr_key_state.patch
  • tk-win-key-events.txt
  • tkinter_event_repr_key_state2.patch
  • tk-win-key-events2.txt
  • 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 = None
    closed_at = <Date 2016-06-18.20:09:22.448>
    created_at = <Date 2016-06-11.12:35:57.215>
    labels = ['type-feature', 'expert-tkinter']
    title = 'Better repr for Tkinter event objects'
    updated_at = <Date 2016-07-20.20:57:29.776>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2016-07-20.20:57:29.776>
    actor = 'terry.reedy'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-06-18.20:09:22.448>
    closer = 'serhiy.storchaka'
    components = ['Tkinter']
    creation = <Date 2016-06-11.12:35:57.215>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['43343', '43349', '43352', '43373', '43377']
    hgrepos = []
    issue_num = 27294
    keywords = ['patch']
    message_count = 18.0
    messages = ['268209', '268228', '268231', '268232', '268235', '268236', '268258', '268264', '268267', '268272', '268275', '268291', '268300', '268429', '268793', '268823', '268832', '270892']
    nosy_count = 3.0
    nosy_names = ['terry.reedy', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue27294'
    versions = ['Python 3.6']

    @serhiy-storchaka
    Copy link
    Member Author

    Proposed patch adds better repr for tkinter.Event objects. Currently default repr shows only class name (always the same) and object address (not interesting, since event objects are short-living). The patch makes repr showing most event attributes in human-readable form.

    For testing run following script. Press any keys (with modifiers), move and click mouse, select text, move and resize the window, switch to other windows.

    import tkinter
    t = tkinter.Text()
    t.pack()
    t.bind("<<Selection>>", print)
    t.bind("<<debug>>", print)
    for k in tkinter.EventType:
        try:
            t.event_add("<<debug>>", '<%s>' % k)
        except tkinter.TclError:
            pass
    
    tkinter.mainloop()

    @serhiy-storchaka serhiy-storchaka added topic-tkinter type-feature A feature request or enhancement labels Jun 11, 2016
    @terryjreedy
    Copy link
    Member

    +1! This would be helpful for exploration and quite possibly for tests. Earlier this year, I bound <Button-1> to a custom function(event) printing event.x, event.y so I could discover how the line thickness of Canvas rectangles was distributed inside and outside the nominal dimensions. (Using a magnifier to click on exact pixels, I discovered the rule.) Being able to bind to print instead have been nice.

    There is no patch uploaded. In the absence of seeing what you did... Event.type should be decoded. The hex address should be replaced by at least the widget class, if not its path name (which would make readable names imperative!) The Event representation should be customized to the event type.

    For me, there is no tkinter.EventType. The <<Selection>> binding worked to prove the idea.

    @serhiy-storchaka
    Copy link
    Member Author

    Oops! Sorry, here is a patch.

    Event.type is decoded. The address of the Event object is omitted as don't having much sense (this is short-living object). The name of widget is omitted since it is too long and usually know (in any case you can easy print both widget and event). Only non-default set attributes are output (thus the output depends on the type of the event).

    @serhiy-storchaka
    Copy link
    Member Author

    Sorry yet once. Uploaded patch contained experimental implementation. Here is cleaned patch.

    @terryjreedy
    Copy link
    Member

    Is this something that you might push before a2 tomorrow? (If I review now?) Or still in development?

    @serhiy-storchaka
    Copy link
    Member Author

    If you have no something to add, I'll push the patch.

    @terryjreedy
    Copy link
    Member

    Testing now.

    @terryjreedy
    Copy link
    Member

    Summary: Though I would like to see some refinements, this looks really useful as is and I would like it applied soon even if refinements have to wait for a second patch.

    Moving the window did not generate a printed event.

    I like the compact way of handling customization. Two more possible exclusions: 1. The key state seems like it should only be interesting for key events. 2. The 4 mouse positions are pretty noisy and seem not relevant to key events, and to many other non-mouse events.

    I wish "VirtualEvent event"s had the triggering sequence available, but you cannot change that.
    .

    @serhiy-storchaka
    Copy link
    Member Author

    The key state can be interesting for mouse events too. Ctrl-Click, Shift-Move. Maybe it is worth to omit the output of state=0.

    Agreed, the 4 mouse positions are pretty noisy. Maybe omit root_x and root_y? But mouse position can be relevant for such key events as pressing Ctrl. For example if the mouse pointer points on module name in import statement, pressing Ctrl can convert the name into a hyperling to a file of the module.

    @terryjreedy
    Copy link
    Member

    Right. This is why I suggested leaving anything we are not sure of to a later patch, after gaining more experience (and thought).

    However, 'state' behaved strangely in my experiments. In a sequence of about 900 events, it started as 8 (Left Alt according to Shipman, but I never touched the key) and changed to 0 here

    <Leave event state=8 focus=False x=120 y=-19 x_root=966 y_root=321>
    <Enter event state=8 focus=False x=797 y=482 x_root=1861 y_root=940>
    <ButtonPress event state=8 num=1 x=797 y=482 x_root=1861 y_root=940>
    <Motion event state=264 x=797 y=483 x_root=1861 y_root=941>
    <VirtualEvent event state=0 x=0 y=0 x_root=0 y_root=0>

    back to 8 here

    <VirtualEvent event state=0 x=0 y=0 x_root=0 y_root=0>
    <ButtonRelease event state=264 num=1 x=800 y=489 x_root=1864 y_root=947>
    <Leave event state=8 focus=False x=800 y=489 x_root=1864 y_root=947>

    and briefly back to 0 here

    <ButtonPress event state=8 num=1 x=178 y=134 x_root=1293 y_root=631>
    <VirtualEvent event state=0 x=0 y=0 x_root=0 y_root=0>
    <ButtonRelease event state=264 num=1 x=178 y=134 x_root=1293 y_root=631>
    <Motion event state=8 x=179 y=134 x_root=1294 y_root=631>

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 11, 2016

    New changeset 61a92a102b2d by Serhiy Storchaka in branch 'default':
    Issue bpo-27294: Improved repr for Tkinter event objects.
    https://hg.python.org/cpython/rev/61a92a102b2d

    @serhiy-storchaka
    Copy link
    Member Author

    Next patch decodes the state attribute. Modifier names can be platform depended, please test on Windows. What modifiers correspond Shift, Ctrl, Alt, Win, CapsLock, etc?

    @terryjreedy
    Copy link
    Member

    If you want more, say so, and I will do them after resting.

    @serhiy-storchaka
    Copy link
    Member Author

    Thank you Terry, this information is helpful. Mod1 is Alt on X11, but not on Windows.

    It looks to me that on Windows Mod1 is NumLock, Mod3 is ScrollLock, 0x20000 is Alt, 0x40000 is pad keyboard. It is not clear about Win. What events are emitted if your press say Win-X?

    On Linux (X11) Mod1 is Alt, Mod2 is NumLock, Mod4 is Win, 0x2000, 0x4000 and 0x6000 are alternative keyboard layouts. But many of this I presume is configurable.

    Mac OS can have its own peculiarities.

    I think on this stage it is better to left standard X11 modifiers (Shift, Lock, Control, Mod1-Mod5, Button1-Button5) and output the rest modifiers in hexadecimal. End user should interpret these names and numerics in platform depending way.

    @serhiy-storchaka
    Copy link
    Member Author

    According to Tk sources on Mac OSX: Mod1 is Command (=Meta), Mod2 is Option (=Alt), Mod3 is NumLock, Mod4 is Fn.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 18, 2016

    New changeset e0ec4abe659f by Serhiy Storchaka in branch 'default':
    Issue bpo-27294: Numerical state in the repr for Tkinter event objects is now
    https://hg.python.org/cpython/rev/e0ec4abe659f

    @serhiy-storchaka
    Copy link
    Member Author

    That's with this issue. I think we will return to Event repr after implementing named flags and providing standard mechanism for parsing and creating keystrokes (there are elements of this in IDLE).

    @terryjreedy
    Copy link
    Member

    Addendum: I verified flag 8 is Mod1 is Numlock on Windows. http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/event-handlers.html has table that claims that 8 is Left Alt and that 16 is NumLock. From what you say above, true on non-mac *nix, and only there.

    Flag 32 is Mod3 = Scrolllock on Windows.

    Question: autocomplete.py refers to event.mc_state in

        if hasattr(event, "mc_state") and event.mc_state:
            # A modifier was pressed along with the tab, continue as usual.
            return

    It is not mentioned as an event field in either the NMT ref or http://www.tcl.tk/man/tcl8.6/TkCmd/event.htm#M29. However, it seems to be the state minus any 'lock' flags, Nun/Caps/Scrolllock on Windows. It must use a system-specific mask. The only Google hit for "tk event mc_state" is my recent commit of test_autocomplete.py. Is this a normal tk event attribute or a local addition?

    There is also mc_type, which is 0 for a tab combinations.

    @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

    2 participants