Skip to content

Commit 9c6c3fb

Browse files
Issue #27294: Numerical state in the repr for Tkinter event objects is now
represented as a compination of known flags.
1 parent 45ff4a5 commit 9c6c3fb

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

Lib/tkinter/__init__.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,28 +220,41 @@ class Event:
220220
delta - delta of wheel movement (MouseWheel)
221221
"""
222222
def __repr__(self):
223-
state = {k: v for k, v in self.__dict__.items() if v != '??'}
223+
attrs = {k: v for k, v in self.__dict__.items() if v != '??'}
224224
if not self.char:
225-
del state['char']
225+
del attrs['char']
226226
elif self.char != '??':
227-
state['char'] = repr(self.char)
227+
attrs['char'] = repr(self.char)
228228
if not getattr(self, 'send_event', True):
229-
del state['send_event']
229+
del attrs['send_event']
230230
if self.state == 0:
231-
del state['state']
231+
del attrs['state']
232+
elif isinstance(self.state, int):
233+
state = self.state
234+
mods = ('Shift', 'Lock', 'Control',
235+
'Mod1', 'Mod2', 'Mod3', 'Mod4', 'Mod5',
236+
'Button1', 'Button2', 'Button3', 'Button4', 'Button5')
237+
s = []
238+
for i, n in enumerate(mods):
239+
if state & (1 << i):
240+
s.append(n)
241+
state = state & ~((1<< len(mods)) - 1)
242+
if state or not s:
243+
s.append(hex(state))
244+
attrs['state'] = '|'.join(s)
232245
if self.delta == 0:
233-
del state['delta']
246+
del attrs['delta']
234247
# widget usually is known
235248
# serial and time are not very interesing
236249
# keysym_num duplicates keysym
237250
# x_root and y_root mostly duplicate x and y
238251
keys = ('send_event',
239-
'state', 'keycode', 'char', 'keysym',
252+
'state', 'keysym', 'keycode', 'char',
240253
'num', 'delta', 'focus',
241254
'x', 'y', 'width', 'height')
242255
return '<%s event%s>' % (
243256
self.type,
244-
''.join(' %s=%s' % (k, state[k]) for k in keys if k in state)
257+
''.join(' %s=%s' % (k, attrs[k]) for k in keys if k in attrs)
245258
)
246259

247260
_support_default_root = 1

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.6.0 alpha 3
1010
Library
1111
-------
1212

13+
- Issue #27294: Numerical state in the repr for Tkinter event objects is now
14+
represented as a compination of known flags.
15+
1316
- Issue #27177: Match objects in the re module now support index-like objects
1417
as group indices. Based on patches by Jeroen Demeyer and Xiang Zhang.
1518

0 commit comments

Comments
 (0)