How to remove the dashed line from the Tkinter menu UI?


A Menu Bar contains vertically stacked menu items. We can create a Menu bar by initializing the object of Menu(root). Whenever we initialize a Menu bar in an application, it displays a line separator at the top of the Menu Bar.

To remove the separator or the dashed line from the Menu, we can use the tearoff property. It can be created by defining the 'tearoff = off' property.

Example

#Import the required Libraries
from tkinter import *
from tkinter import ttk

#Create an instance of Tkinter frame
win = Tk()

#Set the geometry of Tkinter frame
win.geometry("750x250")
win.title("Editor")

# Adding Menubar
menu_bar = Menu(win)

#Create a New Menu in the MenuBar
file_menu = Menu(menu_bar, tearoff="off")

#All file menu-items will be added here next
menu_bar.add_cascade(label='File', menu=file_menu)

#Add Menu Items in the file Menu
file_menu.add_command(label="New", compound='left', underline=0)
file_menu.add_command(label="Open", compound='left', underline=0)
file_menu.add_command(label="Save", compound='left', underline=0)
file_menu.add_command(label="Exit", compound='left', underline=0)

win.config(menu=menu_bar)

win.mainloop()

Output

Running the above code will display a window with a Menubar at the top of the window.

Now, set 'tearoff = on' and run the code again to observe its effect on the Menubar.

Updated on: 25-May-2021

791 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements