Displaying context menus
Tkinter menus do not necessarily have to be located on the menu bar, but they can actually be freely placed at any coordinate. These types of menus are called context menus, and they are usually displayed when users right-click on an item.
Context menus are widely used in GUI applications; for instance, file browsers display them to offer the available operations over the selected file, so it is intuitive for users to know how to interact with them.
Getting ready
We will build a context menu for a Text widget to display some common actions of text editors, such as Cut
, Copy
, Paste
, and Delete
:

How to do it...
Instead of configuring a menu instance with a top-level container as a top menu bar, you can explicitly place it using its post
method.
All the commands in the menu entries call a method that uses the text instance to retrieve the current selection or the insertion position:
import tkinter as tk class App(tk.Tk): def __init__(self): super().__init__()...