Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tkinter tests for different events. #118778

Merged
merged 4 commits into from
May 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 278 additions & 0 deletions Lib/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,284 @@ def test_wm_attribute(self):
1.0 if self.wantobjects else '1.0')


class EventTest(AbstractTkTest, unittest.TestCase):

def test_focus(self):
f = tkinter.Frame(self.root, width=150, height=100)
f.pack()
self.root.wait_visibility() # needed on Windows
self.root.update_idletasks()

events = []
f.bind('<FocusIn>', events.append)

f.focus_force()
self.root.update()
self.assertEqual(len(events), 1, events)
e = events[0]
self.assertIs(e.type, tkinter.EventType.FocusIn)
self.assertIs(e.widget, f)
self.assertIsInstance(e.serial, int)
self.assertEqual(e.time, '??')
self.assertIs(e.send_event, False)
self.assertFalse(hasattr(e, 'focus'))
self.assertEqual(e.num, '??')
self.assertEqual(e.state, '??')
self.assertEqual(e.char, '??')
self.assertEqual(e.keycode, '??')
self.assertEqual(e.keysym, '??')
self.assertEqual(e.keysym_num, '??')
self.assertEqual(e.width, '??')
self.assertEqual(e.height, '??')
self.assertEqual(e.x, '??')
self.assertEqual(e.y, '??')
self.assertEqual(e.x_root, '??')
self.assertEqual(e.y_root, '??')
self.assertEqual(e.delta, 0)
self.assertEqual(repr(e), '<FocusIn event>')

def test_configure(self):
f = tkinter.Frame(self.root, width=150, height=100)
f.pack()
self.root.wait_visibility() # needed on Windows
self.root.update_idletasks()

events = []
f.bind('<Configure>', events.append)

f.configure(height=120, borderwidth=10)
self.assertEqual(len(events), 1, events)
e = events[0]
self.assertIs(e.type, tkinter.EventType.Configure)
self.assertIs(e.widget, f)
self.assertIsInstance(e.serial, int)
self.assertEqual(e.time, '??')
self.assertIs(e.send_event, False)
self.assertFalse(hasattr(e, 'focus'))
self.assertEqual(e.num, '??')
self.assertEqual(e.state, '??')
self.assertEqual(e.char, '??')
self.assertEqual(e.keycode, '??')
self.assertEqual(e.keysym, '??')
self.assertEqual(e.keysym_num, '??')
self.assertEqual(e.width, 150)
self.assertEqual(e.height, 100)
self.assertEqual(e.x, 0)
self.assertEqual(e.y, 0)
self.assertEqual(e.x_root, '??')
self.assertEqual(e.y_root, '??')
self.assertEqual(e.delta, 0)
self.assertEqual(repr(e), '<Configure event x=0 y=0 width=150 height=100>')

def test_event_generate_key_press(self):
f = tkinter.Frame(self.root, width=150, height=100)
f.pack()
self.root.wait_visibility() # needed on Windows
self.root.update_idletasks()

events = []
f.bind('<KeyPress>', events.append)
f.focus_force()

f.event_generate('<Alt-z>')
self.assertEqual(len(events), 1, events)
e = events[0]
self.assertIs(e.type, tkinter.EventType.KeyPress)
self.assertIs(e.widget, f)
self.assertIsInstance(e.serial, int)
self.assertEqual(e.time, 0)
self.assertIs(e.send_event, False)
self.assertFalse(hasattr(e, 'focus'))
self.assertEqual(e.num, '??')
self.assertIsInstance(e.state, int)
self.assertNotEqual(e.state, 0)
self.assertEqual(e.char, 'z')
self.assertIsInstance(e.keycode, int)
self.assertNotEqual(e.keycode, 0)
self.assertEqual(e.keysym, 'z')
self.assertEqual(e.keysym_num, ord('z'))
self.assertEqual(e.width, '??')
self.assertEqual(e.height, '??')
self.assertEqual(e.x, -1 - f.winfo_rootx())
self.assertEqual(e.y, -1 - f.winfo_rooty())
self.assertEqual(e.x_root, -1)
self.assertEqual(e.y_root, -1)
self.assertEqual(e.delta, 0)
self.assertEqual(repr(e),
f"<KeyPress event state={e.state:#x} "
f"keysym=z keycode={e.keycode} char='z' x={e.x} y={e.y}>")

def test_event_generate_enter(self):
f = tkinter.Frame(self.root, width=150, height=100)
f.pack()
self.root.wait_visibility() # needed on Windows
self.root.update_idletasks()

events = []
f.bind('<Enter>', events.append)

f.event_generate('<Enter>', x=100, y=50)
self.assertEqual(len(events), 1, events)
e = events[0]
self.assertIs(e.type, tkinter.EventType.Enter)
self.assertIs(e.widget, f)
self.assertIsInstance(e.serial, int)
self.assertEqual(e.time, 0)
self.assertIs(e.send_event, False)
self.assertIs(e.focus, False)
self.assertEqual(e.num, '??')
self.assertEqual(e.state, 0)
self.assertEqual(e.char, '??')
self.assertEqual(e.keycode, '??')
self.assertEqual(e.keysym, '??')
self.assertEqual(e.keysym_num, '??')
self.assertEqual(e.width, '??')
self.assertEqual(e.height, '??')
self.assertEqual(e.x, 100)
self.assertEqual(e.y, 50)
self.assertEqual(e.x_root, 100 + f.winfo_rootx())
self.assertEqual(e.y_root, 50 + f.winfo_rooty())
self.assertEqual(e.delta, 0)
self.assertEqual(repr(e), '<Enter event focus=False x=100 y=50>')

def test_event_generate_button_press(self):
f = tkinter.Frame(self.root, width=150, height=100)
f.pack()
self.root.wait_visibility() # needed on Windows
self.root.update_idletasks()

events = []
f.bind('<ButtonPress>', events.append)
f.focus_force()

f.event_generate('<Button-1>', x=100, y=50)
self.assertEqual(len(events), 1, events)
e = events[0]
self.assertIs(e.type, tkinter.EventType.ButtonPress)
self.assertIs(e.widget, f)
self.assertIsInstance(e.serial, int)
self.assertEqual(e.time, 0)
self.assertIs(e.send_event, False)
self.assertFalse(hasattr(e, 'focus'))
self.assertEqual(e.num, 1)
self.assertEqual(e.state, 0)
self.assertEqual(e.char, '??')
self.assertEqual(e.keycode, '??')
self.assertEqual(e.keysym, '??')
self.assertEqual(e.keysym_num, '??')
self.assertEqual(e.width, '??')
self.assertEqual(e.height, '??')
self.assertEqual(e.x, 100)
self.assertEqual(e.y, 50)
self.assertEqual(e.x_root, f.winfo_rootx() + 100)
self.assertEqual(e.y_root, f.winfo_rooty() + 50)
self.assertEqual(e.delta, 0)
self.assertEqual(repr(e), '<ButtonPress event num=1 x=100 y=50>')

def test_event_generate_motion(self):
f = tkinter.Frame(self.root, width=150, height=100)
f.pack()
self.root.wait_visibility() # needed on Windows
self.root.update_idletasks()

events = []
f.bind('<Motion>', events.append)
f.focus_force()

f.event_generate('<B1-Motion>', x=100, y=50)
self.assertEqual(len(events), 1, events)
e = events[0]
self.assertIs(e.type, tkinter.EventType.Motion)
self.assertIs(e.widget, f)
self.assertIsInstance(e.serial, int)
self.assertEqual(e.time, 0)
self.assertIs(e.send_event, False)
self.assertFalse(hasattr(e, 'focus'))
self.assertEqual(e.num, '??')
self.assertEqual(e.state, 0x100)
self.assertEqual(e.char, '??')
self.assertEqual(e.keycode, '??')
self.assertEqual(e.keysym, '??')
self.assertEqual(e.keysym_num, '??')
self.assertEqual(e.width, '??')
self.assertEqual(e.height, '??')
self.assertEqual(e.x, 100)
self.assertEqual(e.y, 50)
self.assertEqual(e.x_root, f.winfo_rootx() + 100)
self.assertEqual(e.y_root, f.winfo_rooty() + 50)
self.assertEqual(e.delta, 0)
self.assertEqual(repr(e), '<Motion event state=Button1 x=100 y=50>')

def test_event_generate_mouse_wheel(self):
f = tkinter.Frame(self.root, width=150, height=100)
f.pack()
self.root.wait_visibility() # needed on Windows
self.root.update_idletasks()

events = []
f.bind('<MouseWheel>', events.append)
f.focus_force()

f.event_generate('<MouseWheel>', x=100, y=50, delta=-5)
self.assertEqual(len(events), 1, events)
e = events[0]
self.assertIs(e.type, tkinter.EventType.MouseWheel)
self.assertIs(e.widget, f)
self.assertIsInstance(e.serial, int)
self.assertIs(e.send_event, False)
self.assertFalse(hasattr(e, 'focus'))
self.assertEqual(e.time, 0)
self.assertEqual(e.num, '??')
self.assertEqual(e.state, 0)
self.assertEqual(e.char, '??')
self.assertEqual(e.keycode, '??')
self.assertEqual(e.keysym, '??')
self.assertEqual(e.keysym_num, '??')
self.assertEqual(e.width, '??')
self.assertEqual(e.height, '??')
self.assertEqual(e.x, 100)
self.assertEqual(e.y, 50)
self.assertEqual(e.x_root, f.winfo_rootx() + 100)
self.assertEqual(e.y_root, f.winfo_rooty() + 50)
self.assertEqual(e.delta, -5)
self.assertEqual(repr(e), '<MouseWheel event delta=-5 x=100 y=50>')

def test_generate_event_virtual_event(self):
f = tkinter.Frame(self.root, width=150, height=100)
f.pack()
self.root.wait_visibility() # needed on Windows
self.root.update_idletasks()

events = []
f.bind('<<Spam>>', events.append)
f.focus_force()

f.event_generate('<<Spam>>', x=50)
self.assertEqual(len(events), 1, events)
e = events[0]
self.assertIs(e.type, tkinter.EventType.VirtualEvent)
self.assertIs(e.widget, f)
self.assertIsInstance(e.serial, int)
self.assertEqual(e.time, 0)
self.assertIs(e.send_event, False)
self.assertFalse(hasattr(e, 'focus'))
self.assertEqual(e.num, '??')
self.assertEqual(e.state, 0)
self.assertEqual(e.char, '??')
self.assertEqual(e.keycode, '??')
self.assertEqual(e.keysym, '??')
self.assertEqual(e.keysym_num, '??')
self.assertEqual(e.width, '??')
self.assertEqual(e.height, '??')
self.assertEqual(e.x, 50)
self.assertEqual(e.y, 0)
self.assertEqual(e.x_root, f.winfo_rootx() + 50)
self.assertEqual(e.y_root, -1)
self.assertEqual(e.delta, 0)
self.assertEqual(repr(e),
f"<VirtualEvent event x=50 y=0>")


class BindTest(AbstractTkTest, unittest.TestCase):

def setUp(self):
Expand Down
Loading