Tkinter - Tear-off Menu


Tear-off Menu in Tkinter

In Tkinter, tear-off menus allow users to detach a menu from the main window and convert it into a separate window.

To create a tear-off menu in Tkinter, create a menu using Tk.Menu class, and pass True for the tearoff parameter.

For example, consider that we are creating a tear-off menu menu_1 in menu bar. The sample code snippet would be as shown in the following.

menu_1 = tk.Menu(menu_bar, tearoff=True)

In this tutorial, you will learn how to create a tear-off menu, with examples.

Example

In this example, we shall create a tear-off menu with four menu items. For a tear-off menu, a dashed line appears below the menu. When you click on it, the menu tears off into a separate window.

Program

import tkinter as tk

# Create the main window
window = tk.Tk()

# Create the menu bar
menu_bar = tk.Menu(window)

# Create the Tear-off menu Menu1
menu_1 = tk.Menu(menu_bar, tearoff=True)

# Add items for Menu1
menu_1.add_command(label="Item1")
menu_1.add_cascade(label="Item2")
menu_1.add_cascade(label="Item3")

# Add the menu to the menu bar
menu_bar.add_cascade(label="Menu1", menu=menu_1)

# Attach the menu bar to the main window
window.config(menu=menu_bar)

# Start the Tkinter event loop
window.mainloop()

Output

Summary

In this Python Tkinter tutorial, we learned how to create a tear-off menu, with examples.