List of All Tkinter Events

TkinterPythonGUI-Programming

Tkinter is a Python library which is used to create GUI-based application. Tkinter comes with many inbuilt features and extensions which can be used to optimize the application performance and behaviour. Tkinter Events are generally used to provide an interface that works as a bridge between the User and the application logic. We can use Events in any Tkinter application to make it operable and functional.

Here is a list of some common Tkinter events which are generally used for making the application interactive.

  • <Button> − Use the Button event in a handler for binding the Mouse wheels and Buttons.
  • <ButtonRelease> − Instead of clicking a Button, you can also trigger an event by releasing the mouse buttons.
  • <Configure> − Use this event to change the widgets properties.
  • Destroy − Use this event to kill or terminate a particular widget.
  • <Enter> − It actually works like <return> event that can be used to get the focus on a widget with mouse Pointer
  • <Expose> − The event occurs whenever a widget or some part of the application becomes visible that covered by another window in the application.
  • <Focus In> − This event is generally used to get the focus on a particular widget.
  • <Focus Out> − To move the focus from the current widget.
  • <Key Press> − Start the process or call the handler by pressing the key.
  • <KeyRelease> − Start the process or call an event by releasing a key.
  • <Leave> − Use this event to track the mouse pointer when user switches from one widget to another widget.
  • <Map> − Use Map event to show or display any widget in the application.
  • <Motion> − Track the event whenever the mouse pointer moves entirely within the application.
  • <Unmap> − A widget can be unmapped from the application. It is similar to hiding the widget using grid_remove().
  • <Visibility> − An event can happen if some part of the application gets visible in the screen.

Example

In this example, we use <Button> event to display a widget whenever the mouse buttons will be pressed.

# Import the Required libraries
from tkinter import *

# Create an instance of tkinter frame or window
win= Tk()

# Set the size of the window
win.geometry("700x350")

# Define a function to display the message
def display_text(e):
   label.config(text="Code never lies, comments sometimes do", font=('Helvetica 17 bold'))

# Create a label widget to add some text
label= Label(win, text= "")
label.pack(pady= 50)

# Bind the Mouse button event
win.bind('<Button-1>',display_text)
win.mainloop()

Output

Running the above Python script will display an empty window. Left-clicking the window will display some message on the window.

raja
Published on 07-Jun-2021 11:15:09
Advertisements