Tkinter.Menu का उपयोग करके, हम menus और submenus बना सकते हैं। साथ ही, tkinter menus के साथ कुछ अन्य properties भी उपयोग की जाती हैं।
Tearoff property window में menus को tearable बनाती है। tearoff attribute मेनू को main window या parent window से अलग करने के लिए Boolean value accept करता है। Tearoff attribute के साथ, हमारे पास दो विकल्प हैं:
यदि tearoff=0, तो मेनू Window से चिपका रहता है।
यदि tearoff=1, तो यह menus पर "----" खाली dotted lines प्रदर्शित करता है जिसके माध्यम से हम अपने मेनू को window से अलग कर सकते हैं।
उदाहरण
# Importing the tkinter library
from tkinter import *
win = Tk()
win.title("Tearoff Example")
win.geometry("600x500")
# Define a Function for Menu Selection Event
def mytext():
lab = Label(win, text="You have made a selection", font=('Helvetica', 20)).pack(pady=20)
# Create a Menubar
menu_bar = Menu(win)
# Make the menus non-tearable
file_menu = Menu(menu_bar, tearoff=0)
# Tearable Menu
# file_menu = Menu(menu_bar, tearoff=1)
file_menu.add_command(label="New", command=mytext)
# all file menu-items will be added here next
menu_bar.add_cascade(label='File', menu=file_menu)
win.config(menu=menu_bar)
mainloop()
आउटपुट
उपरोक्त snippet चलाने से output generate होगा और एक window दिखाएगा जिसमें एक मेनू होगा।
इस प्रकार, non-tearable और tearable menus (tearoff=0 और tearoff=1) के लिए output इस प्रकार होगा:

