gpt4 book ai didi

python - 如何在单个 Tkinter 窗口中添加顺序下拉菜单?

转载 作者:行者123 更新时间:2023-12-01 01:01:36 25 4
gpt4 key购买 nike

我想向 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 中的任何内容 - 用户可以在做出两个分类选择后按下按钮以在功能中继续前进。但是如何添加第二个下拉菜单供用户选择?

最佳答案

您需要为两个弹出菜单调用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()

关于python - 如何在单个 Tkinter 窗口中添加顺序下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55750244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com