Last Updated : 12 Jul, 2025
Python 3.x comes bundled with the
Tkintermodule that is useful for making GUI based applications. Of all the other frameworks supported by Python Tkinter is the simplest and fastest. Tkinter offers a plethora of widgets that can be used to build GUI applications along with the main event loop that keeps running in the background until the application is closed manually.
Note:For more information, refer to
Python GUI – tkinterTkinter provides a mechanism to deal with events. The event is any action that must be handled by a piece of code inside the program. Events include mouse clicks, mouse movements or a keystroke of a user. Tkinter uses event sequences that allow the users to bind events to handlers for each widget.
Syntax:widget.bind(event, handler)
Tkinter widget is capable of capturing a variety of events such as Button, ButtonRelease, Motion, Double-Button, FocusIn, FocusOut, Key and many more. The code in this article basically deals with event handling where a popup menu with various options is displayed as soon as right-click is encountered on the parent widget. The Menu widget of Tkinter is used to implement toplevel, pulldown, and popup menus.
import tkinter
from tkinter import *
root = Tk()Syntax: Tk(screenName=None, baseName=None, className='Tk', useTk=1) Parameter: In this example, Tk class is instantiated without arguments. Explanation: The Tk() method creates a blank parent widget with close, maximize, and minimize buttons on the top.
L = Label(root, text="Right-click to display menu", width=40, height=20)Syntax: Label(master, **options) Parameter:
L.pack()Syntax: pack(options) Parameter:
m = Menu(root, tearoff=0)Syntax: Menu(master, options) Parameter:
m.add_command(label="Cut") m.add_command(label="Copy") m.add_command(label="Paste") m.add_command(label="Reload") m.add_separator() m.add_command(label="Rename")Syntax: add_command(options) Parameter:
def do_popup(event): try: m.tk_popup(event.x_root, event.y_root) finally: m.grab_release()Syntax: tk_popup(x_root, y_root) Parameter: This procedure posts a menu at a given position on the screen, x_root and y_root is the current mouse position relative to the upper left corner of the screen. Explanation: This method is the event handler. When the event(right click) happens the method is called and the menu appears on the parent widget at the position where the event occurs. The finally block ensures that the grab_release() method releases the event grab.
L.bind("<Button-3>", do_popup)Syntax: bind(event, handler) Parameter:
mainloop()Syntax: mainloop() Parameter: Takes no arguments. Explanation: It acts like an infinite loop which keeps the application running until the main window is closed manually.
import tkinter
from tkinter import *
root = Tk()
L = Label(root, text ="Right-click to display menu",
width = 40, height = 20)
L.pack()
m = Menu(root, tearoff = 0)
m.add_command(label ="Cut")
m.add_command(label ="Copy")
m.add_command(label ="Paste")
m.add_command(label ="Reload")
m.add_separator()
m.add_command(label ="Rename")
def do_popup(event):
try:
m.tk_popup(event.x_root, event.y_root)
finally:
m.grab_release()
L.bind("<Button-3>", do_popup)
mainloop()
Output Explanation
When a right-click is done on the parent window the popup menu appears and displays a list of choices.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4