Hi all,
I'm attempting to write a unit test for a custom widget using Python
unittest. I'm trying to simulate actual keystrokes in my unit test.
Following the example from the tkinter test suite, I created a test that
is approximately this:
from tkinter import Entry as MyWidget
from tkinter.test.support import AbstractTkTest
import unittest
class TestMyWidget(AbstractTkTest, unittest.TestCase):
def setUp(self):
super().setUp()
self.mywidget = self.create()
self.mywidget.wait_visibility()
def tearDown(self):
super().tearDown()
self.mywidget.destroy()
def create(self):
mw = MyWidget(self.root)
mw.pack()
return mw
def _keystroke(self, char):
self.mywidget.event_generate('<KeyPress-{}>'.format(char))
self.mywidget.event_generate('<KeyRelease-{}>'.format(char))
self.mywidget.update_idletasks()
def test_key_entry(self):
self._keystroke('a')
self.assertEqual(self.mywidget.get(), 'a')
if __name__ == '__main__':
unittest.main()
This doesn't work, though. The keypresses don't register and create any
text in the entry. I can add text using event_generate inside a
mainloop, but without mainloop it doesn't seem to work.
Interestingly, I can use the same method to generate mouse events and
they seem to be recognized. Is there a way to make keypresses work
without mainloop?
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss