What does the 'tearoff' attribute do in a tkinter Menu?
Solution 1
The official python docs admit that they're a little light on details:
The tkinter package is a thin object-oriented layer on top of Tcl/Tk. To use tkinter, you don’t need to write Tcl code, but you will need to consult the Tk documentation, and occasionally the Tcl documentation.
The Tk documentation for tearoff
gives you what you're looking for:
tearoff allows you to detach menus for the main window creating floating menus. If you create a menu you will see dotted lines at the top when you click a top menu item. If you click those dotted lines the menu tears off and becomes floating.
Solution 2
Here you can see a tkinter Menu
tear-off with the code for it in the background. I'm not sure how useful this is going to be but according to New Mexico Tech:
Normally, a menu can be torn off: the first position (position 0) in the list of choices is occupied by the tear-off element, and the additional choices are added starting at position 1. If you set tearoff=0, the menu will not have a tear-off feature, and choices will be added starting at position 0.
Stevoisiak
Active programmer specializing in Python, SQL, C++, Java, and AutoHotkey. My goal is to make technology simpler to use by solving problems before users encounter them. That includes making easy-to-maintain code for anyone I collaborate with. (He/Him)
Updated on July 29, 2022Comments
-
Stevoisiak almost 2 years
I often see Tkinter applications initialize
Menu
widgets usingtearoff=0
in the constructor.import tkinter as tk root = tk.Tk() menubar = tk.Menu(root) filemenu = tk.Menu(menubar, tearoff=0)
effbot.org's documentation for
Menu
specifies that the default value fortearoff
is 1, but it doesn't explain what the value is used for.tearoff= Default value is 1. (tearOff/TearOff) tearoffcommand= No default value. (tearOffCommand/TearOffCommand)
What does the
tearoff
attribute do when initializing a tkinterMenu
widget? -
Stevoisiak about 6 yearsWhat does a floating menu look like?
-
wizzwizz4 almost 6 years@StevenVascellaro Use VLC to see what it looks like, if your OS supports it.
-
cup over 2 yearsUsefulness depends on how the developer structures the menus. Sometimes it is easier to just have the menus on the side that you can click on instead of having to drop down to select.