Managing focus and grab

Focus refers to the widget or window which is currently receiving keyboard input. Widgets can also grab focus, preventing mouse movements or keystrokes outside their bounds.

Tkinter gives us these widget methods for managing focus and grab, some of which are useful for running tests:

Method
Description
focus_set() Focuses the widget whenever its window next gains focus
focus_force() Focuses a widget and the window it's in, immediately
grab_set() The widget grabs all events for the application
grab_set_global() The widget grabs all screen events
grab_release() The widget relinquishes its grab

 

In a test environment, we can use these methods to make sure that our generated keyboard and mouse events ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.