[Python-checkins] cpython: Issue #27294: Improved repr for Tkinter event objects.

serhiy.storchaka python-checkins at python.org
Sat Jun 11 17:20:31 EDT 2016


https://hg.python.org/cpython/rev/61a92a102b2d
changeset:   101895:61a92a102b2d
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Jun 12 00:19:44 2016 +0300
summary:
  Issue #27294: Improved repr for Tkinter event objects.

files:
  Lib/tkinter/__init__.py |  75 ++++++++++++++++++++++++++++-
  Misc/NEWS               |   2 +
  2 files changed, 75 insertions(+), 2 deletions(-)


diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -30,6 +30,7 @@
 tk.mainloop()
 """
 
+import enum
 import sys
 
 import _tkinter # If this fails your Python may not be configured for Tk
@@ -132,6 +133,50 @@
         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 @@
     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 @@
         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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -38,6 +38,8 @@
 Library
 -------
 
+- Issue #27294: Improved repr for Tkinter event objects.
+
 - Issue #20508: Improve exception message of IPv{4,6}Network.__getitem__.
   Patch by Gareth Rees.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list