我想单击按钮时运行多个功能。例如,我希望我的按钮看起来像按下按钮时有多个命令
self.testButton = Button(self, text = "test",
command = func1(), command = func2())
当我执行该语句,我得到一个错误,因为我不能分配东西的参数两次。我怎样才能让命令执行多个功能。
我想单击按钮时运行多个功能。例如,我希望我的按钮看起来像按下按钮时有多个命令
self.testButton = Button(self, text = "test",
command = func1(), command = func2())
当我执行该语句,我得到一个错误,因为我不能分配东西的参数两次。我怎样才能让命令执行多个功能。
你可以为组合功能创建一个通用的功能,它可能是这个样子:
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
然后,你可以这样创建按钮:
self.testButton = Button(self, text = "test",
command = combine_funcs(func1, func2))
def func1(evt=None):
do_something1()
do_something2()
...
self.testButton = Button(self, text = "test",
command = func1)
也许?
我想也许你可以做这样的事情
self.testButton = Button(self, text = "test",
command = lambda x:func1() & func2())
但真毛...
您可以使用此拉姆达:
self.testButton = Button(self, text = "test", lambda: [f() for f in [func1, funct2]])
您可以简单地使用拉姆达这样的:
self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])
Button(self, text="text", command=func_1()and func_2)
虽然这段代码可能会回答这个问题,但提供关于为什么和/或该代码如何回答问题的其他内容可以提高其长期价值。 – adiga
定义一个函数来完成你想要的可能是最好的解。把按钮中的某些逻辑本身打乱了我,并且在以后出现潜在的维护问题。 – PeterBB