Tkinter OptionMenu 参数

Sha*_*aun 2 python tkinter optionmenu

我喜欢明确指定参数。例如:

def func(a, b):
    return a+b
Run Code Online (Sandbox Code Playgroud)

每当我调用它时,我都会写:

func(a=6, b=7)
Run Code Online (Sandbox Code Playgroud)

代替:

func(6, 7)
Run Code Online (Sandbox Code Playgroud)

我不能用OptionMenuTkinter 中的类来做到这一点。以下示例位于自定义类中

self.var = tk.StringVav()
choices = ['op1', 'op2']
self.menu_m = tk.OptionMenu(master=self.frame_1, variable=self.var, *choices)
Run Code Online (Sandbox Code Playgroud)

这导致参数“master”有多个值。如何显式定义要使用的主选项、变量和选项列表?

Bry*_*ley 5

Unfortunately, the OptionMenu widget is somewhat poorly implemented. Regardless of your preferences, the optionmenu isn't designed to accept keyword arguments for the first three parameters master, variable, and value. They must be presented in that order as positional arguments.

self.menu_m = tk.OptionMenu(self.frame_1, self.var, *choices)
Run Code Online (Sandbox Code Playgroud)

  • @martineau:我显然很困惑。不幸的是,tk 和 ttk OptionMenus 有微妙的不同,我当时肯定一直在考虑 ttk optionmenu。`ttk.OptionMenu` 在位置参数之后立即接受一个名为 `default` 的附加关键字参数。我会修正答案。 (2认同)