How to add a separator in Menu item in Tkinter?



The Tkinter Menu widget is used to create a dropdown menu in an application. With menu widgets, we can select an item from the menu and run a specific task in the application.

In many applications, we see a dotted separator line that separates the menu items in the menu. The separator separates the menu item of one type from another, and we can use it to visualize the hierarchy of the menu items. To create a separator among the menu items, you can use the add_separator() method.

Example

# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of Tkinter frame win=Tk() # Set the geometry of the Tkinter library win.geometry("700x350") menubar=Menu(win) # Add Menu my_menu=Menu(menubar, tearoff=0) # Adding Menu Items my_menu.add_command(label="Refresh") my_menu.add_command(label="Edit") # Add a separator my_menu.add_separator() my_menu.add_command(label="View") my_menu.add_command(label="Save") my_menu.add_command(label="Close") menubar.add_cascade(label='File', menu=my_menu) win.config(menu=menubar) win.mainloop()

Output

Running the above code will display a window with a menu bar at the top of the window. Click the 'File' Menu to display the menu items in the menubar. The menu items are separated by a separator.

Updated on: 2021-08-05T14:04:36+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements