我想在Tkinter窗口中添加两个下拉菜单。
我已经有一个单独的下拉菜单的代码,可以很好地工作。但是就目前而言,第二个popupMenu2从未出现。
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()
我不认为我必须更改tk.Button中的任何内容-用户在做出两个分类选择后,可以在功能上继续按下按钮。但是,如何添加第二个下拉菜单供用户选择?
答案 0 :(得分:2)
您需要为两个弹出菜单调用grid()
(并根据需要调整column
的值):
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()