MeC
MeC

Reputation: 463

How to add a sequential dropdown menu in a single Tkinter window?

I would like to add two dropdown menus to a Tkinter window.

I already have the code for a single dropdown menu that works nicely. But as it currently stands, the second popupMenu2 never appears.

root = tk.Tk()

showinfo('Window', "Select a metadata file")

root.update()

# setup window
root.title("Choose")

frame = tk.Frame(root)
frame.grid(row=3, column=2)

category = tk.StringVar()

tk.Button(frame, background="gray", text="Play Clip", command=play_audio).grid(row=1, column=1)

somechoices = {"A", "B", "C", "D"}
somemorechoices = {"1", "2", "3", "4"}
category.set("Pick a category")

popupMenu = tk.OptionMenu(frame, category, *somechoices)
popupMenu2 = tk.OptionMenu(frame, category, *somemorechoices) # this doesn't appear in the window
popupMenu.grid(row=3, column=1)

tk.Label(frame, text="Pick a category: ").grid(row = 3, column = 0)
tk.Button(frame, text="Next", command=next_recording, bg="gray").grid(row=3, column=2) # next_recording refers to a function that plays music clips

root.mainloop() 

I don't think I have to change anything in tk.Button - the user can push the button to move on in the function after making both classification choices. But how do I add the second dropdown for the user to pick from?

Upvotes: 2

Views: 2464

Answers (1)

martineau
martineau

Reputation: 123403

You need to call grid() for both popup menus (and adjust column values as necessary):

root = tk.Tk()

showinfo('Window', "Select a metadata file")

root.update()

# setup window
root.title("Choose")

frame = tk.Frame(root)
frame.grid(row=3, column=2)

category = tk.StringVar()

btn = tk.Button(frame, background="gray", text="Play Clip", command=play_audio)
btn.grid(row=1, column=1)

somechoices = {"A", "B", "C", "D"}
somemorechoices = {"1", "2", "3", "4"}
category.set("Pick a category")

popupMenu = tk.OptionMenu(frame, category, *somechoices)
popupMenu.grid(row=3, column=1)
popupMenu2 = tk.OptionMenu(frame, category, *somemorechoices)
popupMenu2.grid(row=3, column=2)  # ADDED

tk.Label(frame, text="Pick a category: ").grid(row=3, column=0)
btn = tk.Button(frame, text="Next", command=next_recording, bg="gray")
btn.grid(row=3, column=3)

root.mainloop()

Upvotes: 2

Related Questions