@@ -2,7 +2,8 @@ import sys
2
2
from enum import Enum
3
3
from tkinter .constants import * # noqa: F403
4
4
from types import TracebackType
5
- from typing import Any , Callable , Dict , Optional , Tuple , Type , Union
5
+ from typing import Any , Callable , Dict , Generic , Optional , Tuple , Type , TypeVar , Union , overload
6
+ from typing_extensions import Literal
6
7
7
8
TclError : Any
8
9
wantobjects : Any
@@ -52,7 +53,10 @@ if sys.version_info >= (3, 6):
52
53
VirtualEvent : str = ...
53
54
Visibility : str = ...
54
55
55
- class Event :
56
+ # Events considered covariant because you should never assign to event.widget.
57
+ _W = TypeVar ("_W" , covariant = True , bound = "Misc" )
58
+
59
+ class Event (Generic [_W ]):
56
60
serial : int
57
61
num : int
58
62
focus : bool
@@ -73,7 +77,7 @@ class Event:
73
77
type : EventType
74
78
else :
75
79
type : str
76
- widget : Misc
80
+ widget : _W
77
81
delta : int
78
82
79
83
def NoDefaultRoot (): ...
@@ -119,6 +123,8 @@ getdouble: Any
119
123
120
124
def getboolean (s ): ...
121
125
126
+ # This class is the base class of all widgets. Don't use BaseWidget or Widget
127
+ # for that because Tk doesn't inherit from Widget or BaseWidget.
122
128
class Misc :
123
129
def destroy (self ): ...
124
130
def deletecommand (self , name ): ...
@@ -222,12 +228,47 @@ class Misc:
222
228
def update (self ): ...
223
229
def update_idletasks (self ): ...
224
230
def bindtags (self , tagList : Optional [Any ] = ...): ...
225
- def bind (self , sequence : Optional [Any ] = ..., func : Optional [Any ] = ..., add : Optional [Any ] = ...): ...
226
- def unbind (self , sequence , funcid : Optional [Any ] = ...): ...
227
- def bind_all (self , sequence : Optional [Any ] = ..., func : Optional [Any ] = ..., add : Optional [Any ] = ...): ...
228
- def unbind_all (self , sequence ): ...
229
- def bind_class (self , className , sequence : Optional [Any ] = ..., func : Optional [Any ] = ..., add : Optional [Any ] = ...): ...
230
- def unbind_class (self , className , sequence ): ...
231
+ # bind with isinstance(func, str) doesn't return anything, but all other
232
+ # binds do. The default value of func is not str.
233
+ @overload
234
+ def bind (
235
+ self ,
236
+ sequence : Optional [str ] = ...,
237
+ func : Optional [Callable [[Event [Misc ]], Optional [Literal ["break" ]]]] = ...,
238
+ add : Optional [bool ] = ...,
239
+ ) -> str : ...
240
+ @overload
241
+ def bind (self , sequence : Optional [str ], func : str , add : Optional [bool ] = ...) -> None : ...
242
+ @overload
243
+ def bind (self , * , func : str , add : Optional [bool ] = ...) -> None : ...
244
+ # There's no way to know what type of widget bind_all and bind_class
245
+ # callbacks will get, so those are Misc.
246
+ @overload
247
+ def bind_all (
248
+ self ,
249
+ sequence : Optional [str ] = ...,
250
+ func : Optional [Callable [[Event [Misc ]], Optional [Literal ["break" ]]]] = ...,
251
+ add : Optional [bool ] = ...,
252
+ ) -> str : ...
253
+ @overload
254
+ def bind_all (self , sequence : Optional [str ], func : str , add : Optional [bool ] = ...) -> None : ...
255
+ @overload
256
+ def bind_all (self , * , func : str , add : Optional [bool ] = ...) -> None : ...
257
+ @overload
258
+ def bind_class (
259
+ self ,
260
+ className : str ,
261
+ sequence : Optional [str ] = ...,
262
+ func : Optional [Callable [[Event [Misc ]], Optional [Literal ["break" ]]]] = ...,
263
+ add : Optional [bool ] = ...,
264
+ ) -> str : ...
265
+ @overload
266
+ def bind_class (self , className : str , sequence : Optional [str ], func : str , add : Optional [bool ] = ...) -> None : ...
267
+ @overload
268
+ def bind_class (self , className : str , * , func : str , add : Optional [bool ] = ...) -> None : ...
269
+ def unbind (self , sequence : str , funcid : Optional [str ] = ...) -> None : ...
270
+ def unbind_all (self , sequence : str ) -> None : ...
271
+ def unbind_class (self , className : str , sequence : str ) -> None : ...
231
272
def mainloop (self , n : int = ...): ...
232
273
def quit (self ): ...
233
274
def nametowidget (self , name ): ...
@@ -419,7 +460,22 @@ class BaseWidget(Misc):
419
460
def __init__ (self , master , widgetName , cnf = ..., kw = ..., extra = ...): ...
420
461
def destroy (self ): ...
421
462
422
- class Widget (BaseWidget , Pack , Place , Grid ): ...
463
+ # This class represents any widget except Toplevel or Tk.
464
+ class Widget (BaseWidget , Pack , Place , Grid ):
465
+ # Allow bind callbacks to take e.g. Event[Label] instead of Event[Misc].
466
+ # Tk and Toplevel get notified for their child widgets' events, but other
467
+ # widgets don't.
468
+ @overload
469
+ def bind (
470
+ self : _W ,
471
+ sequence : Optional [str ] = ...,
472
+ func : Optional [Callable [[Event [_W ]], Optional [Literal ["break" ]]]] = ...,
473
+ add : Optional [bool ] = ...,
474
+ ) -> str : ...
475
+ @overload
476
+ def bind (self , sequence : Optional [str ], func : str , add : Optional [bool ] = ...) -> None : ...
477
+ @overload
478
+ def bind (self , * , func : str , add : Optional [bool ] = ...) -> None : ...
423
479
424
480
class Toplevel (BaseWidget , Wm ):
425
481
def __init__ (self , master : Optional [Any ] = ..., cnf = ..., ** kw ): ...
@@ -440,8 +496,19 @@ class Canvas(Widget, XView, YView):
440
496
def addtag_overlapping (self , newtag , x1 , y1 , x2 , y2 ): ...
441
497
def addtag_withtag (self , newtag , tagOrId ): ...
442
498
def bbox (self , * args ): ...
443
- def tag_unbind (self , tagOrId , sequence , funcid : Optional [Any ] = ...): ...
444
- def tag_bind (self , tagOrId , sequence : Optional [Any ] = ..., func : Optional [Any ] = ..., add : Optional [Any ] = ...): ...
499
+ @overload
500
+ def tag_bind (
501
+ self ,
502
+ tagOrId : Union [str , int ],
503
+ sequence : Optional [str ] = ...,
504
+ func : Optional [Callable [[Event [Canvas ]], Optional [Literal ["break" ]]]] = ...,
505
+ add : Optional [bool ] = ...,
506
+ ) -> str : ...
507
+ @overload
508
+ def tag_bind (self , tagOrId : Union [str , int ], sequence : Optional [str ], func : str , add : Optional [bool ] = ...) -> None : ...
509
+ @overload
510
+ def tag_bind (self , tagOrId : Union [str , int ], * , func : str , add : Optional [bool ] = ...) -> None : ...
511
+ def tag_unbind (self , tagOrId : Union [str , int ], sequence : str , funcid : Optional [str ] = ...) -> None : ...
445
512
def canvasx (self , screenx , gridspacing : Optional [Any ] = ...): ...
446
513
def canvasy (self , screeny , gridspacing : Optional [Any ] = ...): ...
447
514
def coords (self , * args ): ...
@@ -660,8 +727,18 @@ class Text(Widget, XView, YView):
660
727
): ...
661
728
def see (self , index ): ...
662
729
def tag_add (self , tagName , index1 , * args ): ...
663
- def tag_unbind (self , tagName , sequence , funcid : Optional [Any ] = ...): ...
664
- def tag_bind (self , tagName , sequence , func , add : Optional [Any ] = ...): ...
730
+ # tag_bind stuff is very similar to Canvas
731
+ @overload
732
+ def tag_bind (
733
+ self ,
734
+ tagName : str ,
735
+ sequence : Optional [str ],
736
+ func : Optional [Callable [[Event [Text ]], Optional [Literal ["break" ]]]],
737
+ add : Optional [bool ] = ...,
738
+ ) -> str : ...
739
+ @overload
740
+ def tag_bind (self , tagName : str , sequence : Optional [str ], func : str , add : Optional [bool ] = ...) -> None : ...
741
+ def tag_unbind (self , tagName : str , sequence : str , funcid : Optional [str ] = ...) -> None : ...
665
742
def tag_cget (self , tagName , option ): ...
666
743
def tag_configure (self , tagName , cnf : Optional [Any ] = ..., ** kw ): ...
667
744
tag_config : Any
0 commit comments