use*_*508 9 python command tkinter button python-2.7
我想在单击按钮时运行多个功能.例如,我希望我的按钮看起来像
self.testButton = Button(self, text = "test",
command = func1(), command = func2())
Run Code Online (Sandbox Code Playgroud)
当我执行这个语句时,我得到一个错误,因为我不能两次为一个参数分配一些东西.如何让命令执行多个功能.
And*_*ark 23
您可以创建一个用于组合函数的通用函数,它可能看起来像这样:
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样创建你的按钮:
self.testButton = Button(self, text = "test",
command = combine_funcs(func1, func2))
Run Code Online (Sandbox Code Playgroud)
小智 18
您可以像这样简单地使用lambda:
self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])
Run Code Online (Sandbox Code Playgroud)
Jor*_*ley 13
def func1(evt=None):
do_something1()
do_something2()
...
self.testButton = Button(self, text = "test",
command = func1)
Run Code Online (Sandbox Code Playgroud)
也许?
我想也许你可以做点什么
self.testButton = Button(self, text = "test",
command = lambda x:func1() & func2())
Run Code Online (Sandbox Code Playgroud)
但这真的很糟糕......
小智 5
您可以为此使用 lambda:
self.testButton = Button(self, text = "test", lambda: [f() for f in [func1, funct2]])
Run Code Online (Sandbox Code Playgroud)
小智 5
Button(self, text="text", command=func_1()and func_2)
| 归档时间: |
|
| 查看次数: |
67800 次 |
| 最近记录: |