Tkinter - Add separator in Menu
Add separator in Menu in Tkinter
To add a separator line in Menu in Tkinter, call add_separator() method on the Menu object.
Menu.add_separator()
In this tutorial, you will learn how to add a separator in the Menu in Tkinter, as shown in the following example menu.
Menu1 # Menu1 is a menu in menu bar
Item1 # item under Menu1
Item2 # item under Menu1
⎯⎯⎯⎯ # separator
Item3 # item under Menu1
Example
In this example, we shall create a window, create a menu bar, create a menu, say Menu1, then add some items to this Menu1 menu. In this menu, we add a separator after the first two elements, to visually separate the first two items from the next items.
Program
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("PythonExamples.org")
window.geometry("300x200")
# Create the menu bar
menu_bar = tk.Menu(window)
# Create the menu
menu_1 = tk.Menu(menu_bar, tearoff=0)
menu_1.add_command(label="Item1")
menu_1.add_command(label="Item2")
menu_1.add_separator()
menu_1.add_command(label="Item3", command=window.quit)
# Add the MyMenu 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
Run on MacOS.
Summary
In this Python Tkinter tutorial, we learned how to add a separator in the Menu using Tkinter, with the help of examples.