Skip to content

Conversation

@mkiever
Copy link

@mkiever mkiever commented May 27, 2018

This adds Tk %d event field as 'detail' and 'user_data' attributes to tkinter events.

I have not yet changed the documentation, because I am not sure about the intent
of the table in "25.1.6.7. Bindings and Events" in tkinter documentation.
If supposed to be complete, I suggest also adding the other three missing fields (%#, %b, %D).

I have added a simple test of virtual events and the userdata attribute including bind/unbind.
To make the test possible I use a pump_events function found on
stackoverflow (https://stackoverflow.com/questions/4083796/how-do-i-run-unittest-on-a-tkinter-app/49028688#49028688).
Note, I tested this only on Debian Linux.
Also, I had spurious "Bad Drawable" errors from the X server running the tkinter unittest,
but I have those in the same way on the changed and unchanged Python.
Seems to be my non-mainstream setup (ctwm with interactive window placement).

This is my first pull request. Sorry, if I have forgotten obvious things.

https://bugs.python.org/issue3405

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

When your account is ready, please add a comment in this pull request
and a Python core developer will remove the CLA not signed label
to make the bot check again.

Thanks again to your contribution and we look forward to looking at it!

@mkiever
Copy link
Author

mkiever commented Jun 8, 2018

I see a "Contributor Form Received: Yes" now on my account on bpo. So CLA has arrived.

@koutoftimer
Copy link

koutoftimer commented Jun 17, 2020

@the-knights-who-say-ni , what is missing? Why it is not accepted yet?

ERROR: test_ignore (test.test_multiprocessing_forkserver.TestIgnoreEINTR)
FAIL: test_stdin_broken_pipe (test.test_asyncio.test_subprocess.SubprocessSafeWatcherTests)

Those erros are not related to tkinter

Copy link

@koutoftimer koutoftimer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified for Python 3.7.3 win64.

@aleroddepaz
Copy link

I have not seen any news about this issue after the migration, but it looks like this is the most complete patch. @serhiy-storchaka could anything else be done to help with this review?

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Sep 21, 2024
for s in args]
nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y, D = args
# Missing: (a, c, d, m, o, v, B, R)
nsign, b, d, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y, D = args

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it breaking change? Because f argument is moved one position to the right and number of arguments is changed. Just making a point in case you have missed it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, d is no longer missing, comment lies.

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Dec 15, 2024
@picnixz picnixz changed the title bpo-3405: Add support for user data of Tk virtual events to tkinter gh-47655: Add support for user data of Tk virtual events to tkinter Dec 15, 2024
@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Feb 27, 2025
@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Apr 9, 2025
@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label May 10, 2025
@ZelphirKaltstahl
Copy link

ZelphirKaltstahl commented Nov 15, 2025

Any progress here? @serhiy-storchaka I think your review is requested?

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Nov 15, 2025
@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Dec 15, 2025
@ZelphirKaltstahl
Copy link

Ping.

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Dec 16, 2025
@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Jan 15, 2026
@ZelphirKaltstahl
Copy link

Ping.

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Jan 15, 2026
@serhiy-storchaka
Copy link
Member

Thank you for reminder @ZelphirKaltstahl. I'm going to finish this. Sorry that it took so much time, @mkiever.

For now, I have the following comments:

  • Now there are tests for events in Lib/test/test_tkinter/test_misc.py. Please move new tests there, preferably following the style of the existing tests. Maybe it is enough to add few lines to existing tests.
  • This is the only substitution that is exposed as two fields. It may be confusing. Could we only set data for virtual events and detail for other events? BTW, since even generate uses -data, not -user_data, I think that the name data is more preferable.
  • There are other subsitutions, like %a and %i. Should they also be supported?

If you have no time, @mkiever, or lost interest, I'll continue this work, preserving your credits.

@koutoftimer
Copy link

koutoftimer commented Jan 19, 2026

@serhiy-storchaka

BTW, since even generate uses -data, not -user_data, I think that the name data is more preferable.

man n event talks about user_data field. If you are about to change it to just data field of the event object it will cause additional friction.

There are also mentioning of detail field

Both user_data and detail should be, or at least behave as, object fields. It would be nice for the event object to have access to the args directly and perform substitution lazily. Anyway, because %d corresponds to 2 different fields the question of "single source of truth arrives". I'd prefer implementation with lazy field access or some private __d variable that corresponds to substitution with @property. And type checking to prevent access to user_data and detail fields (if they act as union). This issue has to be resolved or it is technical dept.

The data you are referring to is more like "argument name for the event constructor", not the name of the field of the event object itself.

UPD: WidgetViewSync is one of the predefined virtual events but it has detail field instead of user_data.

       <<WidgetViewSync>>
              This is sent to a text widget when its internal data become obsolete, and again when these
              internal data are back in sync with the widget view. The detail field (%d substitution) is
              either true (when the widget is in sync) or false (when it is not).

@serhiy-storchaka
Copy link
Member

Well, lets keep user_data. There is already precedence -- send_event, x_root. These names are field names in the XEvent union.

As for the WidgetViewSync event, this is an error in the documentation. All virtual events have field user_data, not detail. You can look at the code of GenerateWidgetViewSyncEvent and Tk_SendVirtualEvent for generating the event and
at the code of ExpandPercents in generic/tkBind.c for substitution:

	case 'd':
	    if (flags & (CROSSING|FOCUS)) {
		int detail = (flags & FOCUS) ? evPtr->xfocus.detail : evPtr->xcrossing.detail;
		string = TkFindStateString(notifyDetail, detail);
	    } else if (flags & CONFIGREQ) {
		if (evPtr->xconfigurerequest.value_mask & CWStackMode) {
		    string = TkFindStateString(configureRequestDetail, evPtr->xconfigurerequest.detail);
		} else {
		    string = "";
		}
	    } else if (flags & VIRTUAL) {
		XVirtualEvent *vePtr = (XVirtualEvent *) evPtr;
		string = vePtr->user_data ? Tcl_GetString(vePtr->user_data) : "";
	    }
	    break;

@koutoftimer
Copy link

These names are field names in the XEvent union.

If you are talking about this XEvent all fields are hidden behind respected event types, send_event and x_root aren't exposed directly.

As for class Event it can be just a dictionary. It would be nice to have inheritance similar to XEvent (abstract class) with XKeymapEvent etc. as concrete implementations (proper inheritance would allow to use object.__slots__) but it is not the scope of this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants