You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
tkinter.Event is not subscriptable at runtime but generic in stub file #123341
It would be great if this problem (a little more detailed version here) could be fixed in the entire standard library. As of right now, the inconsistency of class subscription support is just way too confusing for developers.
CPython versions tested on:
3.8, 3.9, 3.10, 3.11, 3.12, 3.13, CPython main branch
It means that some_event.widget is specifically a tkinter.Canvas object. Even if you don't directly use that attribute, being able to annotate your event handler functions with the specific widget would allow checkers to catch passing the wrong event handler to bind().
Well but the event does come from tkinter.Frame so imo the correct way to type hint this is tkinter.Event[tkinter.Frame] since hovering over the frame is what causes the function to be called
You are using Tk.bind(), which is already typed with tkinter.Event[tkinter.Misc] (that is, an event where the widget may be of any type). The same goes for Toplevel.bind. For other widgets, event.widget is always* the same widget whose .bind() was called, and the stubs define it in that way.
* Technically not correct. You could use a Frame as a toplevel with the wm manage command and then receive events from arbitrary widgets, as with Tk and Toplevel, but most tkinter users don't do that and tkinter wasn't written to support it very well (Frame doesn't inherit from Wm).
Activity
serhiy-storchaka commentedon Aug 26, 2024
What is the meaning of
tkinter.Event[tkinter.Canvas]? Why the Event class is considered generic?yowoda commentedon Aug 26, 2024
python/typeshed#12591 (comment)
In this case, it implies the event was caused by an interaction with the canvas
serhiy-storchaka commentedon Aug 27, 2024
How do canvas events differ from others?
TeamSpen210 commentedon Aug 27, 2024
It means that
some_event.widgetis specifically atkinter.Canvasobject. Even if you don't directly use that attribute, being able to annotate your event handler functions with the specific widget would allow checkers to catch passing the wrong event handler tobind().Akuli commentedon Aug 27, 2024
You can also do e.g.
event.widget.create_rectangle(...)with an event that came from a Canvas.To me, this is a very stable part of tkinter stubs. The genericness of
Eventwill not change any time soon, andEventhas been generic since 2020.(I am the typeshed maintainer who basically takes care of everything tkinter related.)
Akuli commentedon Aug 27, 2024
Older discussion about this: python/typeshed#4431
serhiy-storchaka commentedon Aug 27, 2024
So this is for the
widgetattribute? This is not correct.widgetis not always the same as the widget for whichbind()was called.Example:
The output is
.frame, not.. And the commented out assert in the handler would fail.yowoda commentedon Aug 27, 2024
Well but the event does come from
tkinter.Frameso imo the correct way to type hint this istkinter.Event[tkinter.Frame]since hovering over the frame is what causes the function to be calledAkuli commentedon Aug 27, 2024
You are using
Tk.bind(), which is already typed withtkinter.Event[tkinter.Misc](that is, an event where the widget may be of any type). The same goes forToplevel.bind. For other widgets,event.widgetis always* the same widget whose.bind()was called, and the stubs define it in that way.* Technically not correct. You could use a
Frameas a toplevel with thewm managecommand and then receive events from arbitrary widgets, as withTkandToplevel, but most tkinter users don't do that and tkinter wasn't written to support it very well (Framedoesn't inherit fromWm).serhiy-storchaka commentedon Aug 28, 2024
Well, you're probably right. I was not able to find example with non-toplevel widgets.
gh-123341: Support `tkinter.Event` type subcript (#123353)