Mills Tennis
Mills Tennis

Reputation: 59

add a separator to an option menu in python with Tkinter

I have python Tkinter code that I want to add a separator to the option menu. I can't figure out how to do it.

I have looked it up online but nobody seemed to ask this question yet.

here is the code

from tkinter import *

root = Tk()
root.geometry("1430x840")

# here is where I have the option menu. I want the separator in-between the word symbols and trash

var1 = StringVar()

opt1 = OptionMenu(root, var1, 
       'Mockups', 
       'Assets', 
       'Symbols', 
       # here is where the separator should be
       'Trash')

opt1.pack(side=LEFT, anchor=W)
var1.set('')

root.mainloop()

Upvotes: 2

Views: 2595

Answers (1)

Saad
Saad

Reputation: 3430

I've found a better way than my previous answer.

The OptionMenu's dropdown list is just a Tkinter Menu() class and it has all the functionalities of Menu(). So you can add separators in OptionMenu by access the Menu object inside the OptionMenu class.

Example:

Op = OptionMenu(root, var, 'First', 'Second', 'Third')
Op.pack()

Op['menu'].insert_separator(1)

Update Code:

from tkinter import *

root = Tk()
root.geometry("1430x840")
var1 = StringVar()

opt1 = OptionMenu(root, var1, 
                'Mockups', 
                'Assets', 
                'Symbols', 
                # here is where the separator should be
                'Trash')

opt1['menu'].insert_separator(3)

opt1.pack(side=LEFT, anchor=W)
var1.set('')

root.mainloop()

Upvotes: 2

Related Questions