O'Reilly logo

Tkinter GUI Programming by Example by David Love

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generating events

When generating a custom event, we first need to name it in a way Tkinter will recognize. We can name an event anything we want (assuming it is not already reserved), as long as we enclose it between two less-than and greater-than signs, for example <<custom_event>>.

We can emit this event whenever we like by using the event_generate method of a Tkinter widget. The first argument to this widget will be our custom name, and the remaining keyword arguments will define some properties which will be sent to the handler via the usual event object.

To receive and handle this event, the bind method is used just like before.

Let's have a go at an example program which uses custom events:

import randomimport tkinter as tkwin = tk.Tk() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required