我在另一篇文章中有同样的问题。我实际上已经阅读了这篇文章和答案,但没有发现这两个选项菜单之间的细微差别。所以,这也是我的答案,即使我是 Python 和 tkinter 的新手。
正如@Bryan Oakley 回答的那样,是的,文档字符串显示使用 ttk.OptionMenu 而不是 tk.OptionMenu 时有一个“附加”必需参数。如果您随意将声明从 ttk.OptionMenu 更改回 tk.OptionMenu,这种差异将破坏(或至少弄乱)您的菜单。这是因为 tk.OptionMenu 不需要声明“默认”选项。如果出于某种原因将 ttk.OptionMenu 更改回 tk.OptionMenu 并在声明中保留“默认”参数,它将复制 OptionMenu 中的第一个选项。我正在用头撞墙,想弄清楚为什么这两个人的行为方式不完全相同。在我个人看来,情况不应该如此。如果有的话,'default' 参数应该是 tk.OptionMenu 和 ttk 中的关键字。OptionMenu 的兼容性。也许我错了,但这是我目前的看法。
为了证明这一点,请参见下面的代码:
# test-optionmenu.py
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# tk OptionMenu lists
optionList1 = ('a', 'b', 'c')
optionList2 = ('d', 'e', 'f')
optionList3 = ('g', 'h', 'j')
# ttk OptionMenu lists
optionList4 = ('1', '2', '3')
optionList5 = ('4', '5', '6')
optionList6 = ('7', '8', '9')
optionList7 = ('z', 'x', 'y')
# Set up the StringVars for each OptionMenu
v1 = tk.StringVar()
v2 = tk.StringVar()
v3 = tk.StringVar()
v4 = tk.StringVar()
v5 = tk.StringVar()
v6 = tk.StringVar()
v7 = tk.StringVar()
# tk.OptionMenu requires the default option
# to be declared via the set() method.
v2.set(optionList2[1]) # Default tk.OptionMenu value for om2
v3.set(optionList3[2]) # Default tk.OptionMenu item value for om3
v5.set(optionList5[1]) # Default ttk.OptionMenu item value for om5
# -------------------------------------------
# tk OptionMenu om1 does not automatically
# assign a default based on your list, so
# this does not display a list item initially
om1 = tk.OptionMenu(root, v1, *optionList1)
# -------------------------------------------
# om2 demonstrates the 'default' parameter
# from a former ttk.OptionMenu is ignored by
# tk.OptionMenu because it needs to have the
# default list item set via v2.set() call.
# Notice the 'e' is displayed initially as
# set, but now 'd' is duplicated in the list
# as a remnant of the former ttk.OptionMenu
# 'default' parameter.
om2 = tk.OptionMenu(root, v2, optionList2[0], *optionList2)
# -------------------------------------------
# om3 is a tk.OptionMenu and has no 'default'
# paramter declared like om2 above does. Its
# default is set to option 2, or 'j', and is
# initially displayed on the dropdown. None
# of the OptionMenu list items are duplicated.
om3 = tk.OptionMenu(root, v3, *optionList3)
# -------------------------------------------
# om4 shows how the 'default' parameter
# from a former ttk.OptionMenu is ignored by
# tk.OptionMenu because it needs to have the
# default list item set via v4.set() call.
# Since v4 is not set, nothing at all is
# diplayed initially and when the OptionMenu
# is clicked, it will show that '1' is listed
# twice.
om4 = tk.OptionMenu(root, v4, optionList4[0], *optionList4)
# -------------------------------------------
# by changing a tk.OptionMenu to a
# ttk.OptionMenu, without properly declaring
# the default OptionMenu item in the ttk way
# will result in item '5' not being displayed
# initially, and '4' will not be displayed in
# the OptionMenu choices after a choice has
# been made.
om5 = ttk.OptionMenu(root, v5, *optionList5)
# -------------------------------------------
# om6 is the same as om5, except it does not
# have a tk.OptionMenu default declared. It
# is only here for consiceness.
om6 = ttk.OptionMenu(root, v6, *optionList6)
# -------------------------------------------
# om7 is the proper way to declare a
# ttk.OptionMenu, with a default option
# initially set.
om7 = ttk.OptionMenu(root, v7, optionList7[2], *optionList7)
# Pack'em all up
om1.pack()
om2.pack()
om3.pack()
om4.pack()
om5.pack()
om6.pack()
om7.pack()
# Execute the mainloop
root.mainloop()
我知道代码对某些人来说可能很难看,如果您真的想更改它,请继续。:)