|
30 | 30 | tk.mainloop() |
31 | 31 | """ |
32 | 32 |
|
| 33 | +import enum |
33 | 34 | import sys |
34 | 35 |
|
35 | 36 | 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): |
132 | 133 | dict[key] = value |
133 | 134 | return dict |
134 | 135 |
|
| 136 | + |
| 137 | +class EventType(str, enum.Enum): |
| 138 | + KeyPress = '2' |
| 139 | + Key = KeyPress, |
| 140 | + KeyRelease = '3' |
| 141 | + ButtonPress = '4' |
| 142 | + Button = ButtonPress, |
| 143 | + ButtonRelease = '5' |
| 144 | + Motion = '6' |
| 145 | + Enter = '7' |
| 146 | + Leave = '8' |
| 147 | + FocusIn = '9' |
| 148 | + FocusOut = '10' |
| 149 | + Keymap = '11' # undocumented |
| 150 | + Expose = '12' |
| 151 | + GraphicsExpose = '13' # undocumented |
| 152 | + NoExpose = '14' # undocumented |
| 153 | + Visibility = '15' |
| 154 | + Create = '16' |
| 155 | + Destroy = '17' |
| 156 | + Unmap = '18' |
| 157 | + Map = '19' |
| 158 | + MapRequest = '20' |
| 159 | + Reparent = '21' |
| 160 | + Configure = '22' |
| 161 | + ConfigureRequest = '23' |
| 162 | + Gravity = '24' |
| 163 | + ResizeRequest = '25' |
| 164 | + Circulate = '26' |
| 165 | + CirculateRequest = '27' |
| 166 | + Property = '28' |
| 167 | + SelectionClear = '29' # undocumented |
| 168 | + SelectionRequest = '30' # undocumented |
| 169 | + Selection = '31' # undocumented |
| 170 | + Colormap = '32' |
| 171 | + ClientMessage = '33' # undocumented |
| 172 | + Mapping = '34' # undocumented |
| 173 | + VirtualEvent = '35', # undocumented |
| 174 | + Activate = '36', |
| 175 | + Deactivate = '37', |
| 176 | + MouseWheel = '38', |
| 177 | + def __str__(self): |
| 178 | + return self.name |
| 179 | + |
135 | 180 | class Event: |
136 | 181 | """Container for the properties of an event. |
137 | 182 |
|
@@ -174,7 +219,30 @@ class Event: |
174 | 219 | widget - widget in which the event occurred |
175 | 220 | delta - delta of wheel movement (MouseWheel) |
176 | 221 | """ |
177 | | - pass |
| 222 | + def __repr__(self): |
| 223 | + state = {k: v for k, v in self.__dict__.items() if v != '??'} |
| 224 | + if not self.char: |
| 225 | + del state['char'] |
| 226 | + elif self.char != '??': |
| 227 | + state['char'] = repr(self.char) |
| 228 | + if not getattr(self, 'send_event', True): |
| 229 | + del state['send_event'] |
| 230 | + if self.state == 0: |
| 231 | + del state['state'] |
| 232 | + if self.delta == 0: |
| 233 | + del state['delta'] |
| 234 | + # widget usually is known |
| 235 | + # serial and time are not very interesing |
| 236 | + # keysym_num duplicates keysym |
| 237 | + # x_root and y_root mostly duplicate x and y |
| 238 | + keys = ('send_event', |
| 239 | + 'state', 'keycode', 'char', 'keysym', |
| 240 | + 'num', 'delta', 'focus', |
| 241 | + 'x', 'y', 'width', 'height') |
| 242 | + return '<%s event%s>' % ( |
| 243 | + self.type, |
| 244 | + ''.join(' %s=%s' % (k, state[k]) for k in keys if k in state) |
| 245 | + ) |
178 | 246 |
|
179 | 247 | _support_default_root = 1 |
180 | 248 | _default_root = None |
@@ -1271,7 +1339,10 @@ def getint_event(s): |
1271 | 1339 | except TclError: pass |
1272 | 1340 | e.keysym = K |
1273 | 1341 | e.keysym_num = getint_event(N) |
1274 | | - e.type = T |
| 1342 | + try: |
| 1343 | + e.type = EventType(T) |
| 1344 | + except ValueError: |
| 1345 | + e.type = T |
1275 | 1346 | try: |
1276 | 1347 | e.widget = self._nametowidget(W) |
1277 | 1348 | except KeyError: |
|
0 commit comments