How to create Virtual Events in Tkinter

In this Tkinter tutorial, we will explore how to create our own Custom Virtual Events.

Any GUI application requires a good event system to detect what the User is doing, and to act accordingly (e.g: Displaying a prompt when a button is clicked). Tkinter handles most of this for us internally, and all we have to do is tell tkinter which function to call (like the command parameter in the button).

Tkinter also allows us to “bind” our own functions to these events directly, as shown in the example below.

root.bind('<Enter>', FunctionCall)

The event being used above is “<Enter>” which triggers whenever the “Enter” key on the keyboard is pressed.

This is a really great system, but sometimes we may wish for a “custom event” which signify something important related to our application. For example, a graphing application may want an event like “graph was updated”, “graph was cleared”, “graph was draw”, etc.

And that is what this tutorial is about. Let’s begin!


Creating a Custom Virtual Event in Tkinter

There are three steps involved in creating a custom event.

  1. First we “add” the event to specific widget (e.g, a button, a frame or even the root application). We define the event name here, and the “events” that trigger it.
  2. We define a function which “handles” our event.
  3. We bind our new event to the newly defined function.

To create an event, we will use the event_add() method. The first parameter takes the event name. It is necessary to enclose the name between double crocodile brackets as shown below.

root.event_add('<<custom-event>>', '<Button-3>')

From the second parameter onwards, you can pass “sequences” or “events” which define which events will trigger your event (like a mouse-button click, or specific-keypress). These are events that are defined and handled by Tkinter itself.

In the above code, we bind our event to “Button-3” which represents the right-mouse click.

Next we define a simple function. It is important to remember that any function you plan on using with events, must have an event parameter. This will be automatically passed in by Tkinter, and will contain information about what event triggered the function.

def hello(event):
    print(event)

Next, we bind this function to our event using the bind() function.

root.bind('<<custom-event>>', hello)

Here is the complete code.

def hello(event):
    print(event)

root = tk.Tk()

root.event_add('<<custom-event>>', '<Button-3>')
root.bind('<<custom-event>>', hello)

root.mainloop()

Try running this code, and clicking the right most button on your mouse. You should get something like the following output:

<ButtonPress event num=3 x=159 y=69>

Deleting Virtual Events in Tkinter

You can also choose to add “triggers” for your custom event by adding additional parameters. There is no limit to the number of additional parameters you can pass.

root.event_add('<<custom-event>>', '<Button-3>', '<KeyPress>')

Now our event will trigger if either Button-3 is clicked, or if any key on the keyboard is pressed.

To delete an event, all we have to do is call event_delete() with the name of an event.

def remove_event(event):
    root.event_delete('<<custom-event>>')

root = tk.Tk()

root.event_add('<<custom-event>>', '<Button-3>', '<KeyPress>')
root.bind('<<custom-event>>', remove_event)

root.mainloop()

You can also choose to only remove specific triggers.

def remove_event(event):
    root.event_delete('<<custom-event>>', '<Button-3>')

The above code stops the custom-event from triggering on Button-3, but will still work for key presses.


This marks the end of the “How to create Virtual Events in Tkinter”. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments