diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index 174e04e89e8..cfb52686220 100644 --- a/Lib/tkinter/__init__.py +++ b/Lib/tkinter/__init__.py @@ -30,6 +30,7 @@ button.pack(side=BOTTOM) tk.mainloop() """ +import enum import sys import _tkinter # If this fails your Python may not be configured for Tk @@ -132,6 +133,50 @@ def _splitdict(tk, v, cut_minus=True, conv=None): dict[key] = value return dict + +class EventType(str, enum.Enum): + KeyPress = '2' + Key = KeyPress, + KeyRelease = '3' + ButtonPress = '4' + Button = ButtonPress, + ButtonRelease = '5' + Motion = '6' + Enter = '7' + Leave = '8' + FocusIn = '9' + FocusOut = '10' + Keymap = '11' # undocumented + Expose = '12' + GraphicsExpose = '13' # undocumented + NoExpose = '14' # undocumented + Visibility = '15' + Create = '16' + Destroy = '17' + Unmap = '18' + Map = '19' + MapRequest = '20' + Reparent = '21' + Configure = '22' + ConfigureRequest = '23' + Gravity = '24' + ResizeRequest = '25' + Circulate = '26' + CirculateRequest = '27' + Property = '28' + SelectionClear = '29' # undocumented + SelectionRequest = '30' # undocumented + Selection = '31' # undocumented + Colormap = '32' + ClientMessage = '33' # undocumented + Mapping = '34' # undocumented + VirtualEvent = '35', # undocumented + Activate = '36', + Deactivate = '37', + MouseWheel = '38', + def __str__(self): + return self.name + class Event: """Container for the properties of an event. @@ -174,7 +219,30 @@ class Event: widget - widget in which the event occurred delta - delta of wheel movement (MouseWheel) """ - pass + def __repr__(self): + state = {k: v for k, v in self.__dict__.items() if v != '??'} + if not self.char: + del state['char'] + elif self.char != '??': + state['char'] = repr(self.char) + if not getattr(self, 'send_event', True): + del state['send_event'] + if self.state == 0: + del state['state'] + if self.delta == 0: + del state['delta'] + # widget usually is known + # serial and time are not very interesing + # keysym_num duplicates keysym + # x_root and y_root mostly duplicate x and y + keys = ('send_event', + 'state', 'keycode', 'char', 'keysym', + 'num', 'delta', 'focus', + 'x', 'y', 'width', 'height') + return '<%s event%s>' % ( + self.type, + ''.join(' %s=%s' % (k, state[k]) for k in keys if k in state) + ) _support_default_root = 1 _default_root = None @@ -1271,7 +1339,10 @@ class Misc: except TclError: pass e.keysym = K e.keysym_num = getint_event(N) - e.type = T + try: + e.type = EventType(T) + except ValueError: + e.type = T try: e.widget = self._nametowidget(W) except KeyError: diff --git a/Misc/NEWS b/Misc/NEWS index 0a2b4eabbb2..7d72f6511b5 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -38,6 +38,8 @@ Core and Builtins Library ------- +- Issue #27294: Improved repr for Tkinter event objects. + - Issue #20508: Improve exception message of IPv{4,6}Network.__getitem__. Patch by Gareth Rees.