Tkinter Python:如何通过 OptionMenu 中的 lambda 函数传递多个参数

Tkinter Python: How to pass more than one argument through a lambda function in an OptionMenu

因此,正如标题所暗示的那样,我无法将 2 个参数传递给要在更改选项菜单时调用的函数。

下面是代码:

OPTIONS = [
        "Fire",
        "Ice",
        "Storm",
        "Life",
        "Myth",
        "Death",
        "Balance"
    ]

    var = StringVar(frame)
    var.set("Select School") # initial value

    option = OptionMenu(frame, var,*OPTIONS,command= lambda frame,var : changeSchool(frame,var))
    option.grid(row=0,column=0,sticky=(N,W))

我做了一些研究,我认为我做的一切都是正确的,但是当我 select 选项菜单中的一个选项时,我收到以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Gunner\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Users\Gunner\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3287, in __call__
self.__callback(self.__value, *args)
TypeError: <lambda>() missing 1 required positional argument: 'var'

我以为我已经将 var 传入了 changeSchool 函数。

感谢任何帮助!

编辑:这是我的 changeSchool 功能,供希望查看的人使用:

def changeSchool(frame,school):
    print (school)
    WizTool.schoolType = school
    self.initBT(frame,WizTool.schoolType)
    print (WizTool.schoolType)

编辑:这是整个程序(显示框架不在范围内)

from tkinter import *
from tkinter import ttk
from functools import partial
import webbrowser
import pprint

class WizTool:
schoolType = "Fire"


#initialize the GUI
def __init__(self, master):
    content = ttk.Frame(master, padding=(3,3,12,12))
    frame = ttk.LabelFrame(content, borderwidth=5, relief="sunken", width=400, height=400,padding=(3,3,12,12),text = "Blade Tracker")
    content.grid(column=0,row=0,sticky=(N,S,E,W))
    frame.grid(column=0, row=0, columnspan=4, sticky=(N, S, E, W))
    self.master = master
    self.initUI(content)
    self.initBT(frame,WizTool.schoolType)


def initUI(self,frame):
    self.master.title("Bootstrapper")


def changeSchool(frame,school):
    print (school)
    WizTool.schoolType = school
    self.initBT(frame,WizTool.schoolType)
    print (WizTool.schoolType)

def initBT(self,frame,mySchool):
#option menu for selecting school

    OPTIONS = [
        "Fire",
        "Ice",
        "Storm",
        "Life",
        "Myth",
        "Death",
        "Balance"
    ]

    var = StringVar(frame)
    var.set("Select School") # initial value

    option = OptionMenu(frame, var,*OPTIONS,command= lambda frame,var : changeSchool(frame,var))
    option.grid(row=0,column=0,sticky=(N,W))





def main():

root = Tk()
root.geometry("800x500+300+300")
app = WizTool(root)
root.mainloop()

main()

OptionMenu 小部件的 command 函数只接受一个参数:selected item

  1. 您是在调用 command 函数吗?答:没有。
  2. 谁在调用 command 函数?答案:python.

Python 将您指定的函数存储在某处,然后在将来某个时间在 OptionsMenu 中进行选择后,python 调用 command 函数。所以 python 开始决定将多少参数传递给 command 函数,结果 python 使用一个参数调用 command 函数:

 command(selection)
  1. 你怎么知道 python 用一个参数调用 command 函数?答:你看文档

  2. 如果您在文档中找不到描述您需要分配给 command 的函数类型的任何内容,会发生什么情况?答:你测试一下。

第一个:

...
...

def do_stuff(x):
    print(x)

tk.OptionMenu(
    root, 
    str_var, 
    *OPTIONS,
    command=do_stuff).pack()

...
...

--output:--
Storm

下一个:

...
...
def do_stuff(x, y):
    print(x, y)

tk.OptionMenu(
    root, 
    str_var, 
    *OPTIONS,
    command=do_stuff).pack()

...
...

--output:--
> TypeError: do_stuff() missing 1 required positional argument: 'y'

有多种方法可以解决这个问题...

使用python的范围规则:

import tkinter as tk

OPTIONS = [
        "Fire",
        "Ice",
        "Storm",
        "Life",
        "Myth",
        "Death",
        "Balance"
]

root = tk.Tk()
root.title("Hello")
root.geometry("300x200+10+100")

frame = [1, 2, 3]

def do_stuff(selection):  
    print(selection, frame)  #frame is visible inside the def.

str_var = tk.StringVar(root)
str_var.set(OPTIONS[0]) # default value

tk.OptionMenu(
    root, 
    str_var, 
    *OPTIONS,
    command=do_stuff
).pack()

root.mainloop()

但是让函数操纵全局变量并不是一个好主意,所以你可以...

使用封装函数:

import tkinter as tk

OPTIONS = [
        "Fire",
        "Ice",
        "Storm",
        "Life",
        "Myth",
        "Death",
        "Balance"
]

root = tk.Tk()
root.title("Hello")
root.geometry("300x200+10+100")

def wrapper(other):
    def do_stuff(selection):
        print(selection, other)

    return do_stuff

str_var = tk.StringVar(root)
str_var.set(OPTIONS[0]) # default value

tk.OptionMenu(
    root, 
    str_var, 
    *OPTIONS,
    command=wrapper('xxxx')  #This actually executes wrapper(), and a
                             #function call in your code is replaced
                             #by its return value--which happens to 
                             #be the name of a function that takes one
                             #argument.  The one arg function name is
                             #then assigned to command.
).pack()

root.mainloop()

为您的额外参数变量使用默认值:

frame = [1, 2, 3]

def do_stuff(selection, other=frame):  #<****HERE****
    print(selection, other)

str_var = tk.StringVar(root)
str_var.set(OPTIONS[0]) # default value

tk.OptionMenu(
    root, 
    str_var, 
    *OPTIONS,
    command=do_stuff
).pack()

警告:参数变量的默认值有其自身的问题。默认值在定义函数时分配一次。随后,无论您执行该函数多少次,默认值都将使用相同的值。这意味着如果默认值是一个列表,并且第一次调用该函数时您更改了该列表,那么下次您调用该函数时默认值将是更改后的列表。