What does the 'tearoff' attribute do in a tkinter Menu?

12,043

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

Tkinter menu with tearoffHere 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.

Share:
12,043
Stevoisiak
Author by

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, 2022

Comments

  • Stevoisiak
    Stevoisiak almost 2 years

    I often see Tkinter applications initialize Menu widgets using tearoff=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 for tearoff 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 tkinter Menu widget?

  • Stevoisiak
    Stevoisiak about 6 years
    What does a floating menu look like?
  • wizzwizz4
    wizzwizz4 almost 6 years
    @StevenVascellaro Use VLC to see what it looks like, if your OS supports it.
  • cup
    cup over 2 years
    Usefulness 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.