Python 按下按钮时有多个命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13865009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:46:35  来源:igfitidea点击:

Have multiple commands when button is pressed

pythonpython-2.7buttontkintercommand

提问by user1876508

I want to run multiple functions when I click a button. For example I want my button to look like

当我单击一个按钮时,我想运行多个功能。例如,我希望我的按钮看起来像

self.testButton = Button(self, text = "test", 
                         command = func1(), command = func2())

when I execute this statement I get an error because I cannot allocate something to an argument twice. How can I make command execute multiple functions.

当我执行这个语句时,我得到一个错误,因为我不能为一个参数分配两次。如何让命令执行多个功能。

采纳答案by Andrew Clark

You could create a generic function for combining functions, it might look something like this:

您可以创建一个用于组合函数的通用函数,它可能如下所示:

def combine_funcs(*funcs):
    def combined_func(*args, **kwargs):
        for f in funcs:
            f(*args, **kwargs)
    return combined_func

Then you could create your button like this:

然后你可以像这样创建你的按钮:

self.testButton = Button(self, text = "test", 
                         command = combine_funcs(func1, func2))

回答by Joran Beasley

def func1(evt=None):
    do_something1()
    do_something2()
    ...

self.testButton = Button(self, text = "test", 
                         command = func1)

maybe?

也许?

I guess maybe you could do something like

我想也许你可以做类似的事情

self.testButton = Button(self, text = "test", 
                         command = lambda x:func1() & func2())

but that is really gross ...

但这真的很恶心......

回答by blkpws

You can use the lambda for this:

您可以为此使用 lambda:

self.testButton = Button(self, text = "test", lambda: [f() for f in [func1, funct2]])

回答by pradepghr

You can simply use lambda like this:

您可以像这样简单地使用 lambda:

self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])

回答by chibbbb

Button(self, text="text", command=func_1()and func_2)

Button(self, text="text", command=func_1()and func_2)

回答by Link

I've found also this, which works for me. In a situation like...

我也发现了这个,这对我有用。在这样的情况下...

b1 = Button(master, text='FirstC', command=firstCommand)
b1.pack(side=LEFT, padx=5, pady=15)

b2 = Button(master, text='SecondC', command=secondCommand)
b2.pack(side=LEFT, padx=5, pady=10)

master.mainloop()

... you can do...

... 你可以做...

b1 = Button(master, command=firstCommand)
b1 = Button(master, text='SecondC', command=secondCommand)
b1.pack(side=LEFT, padx=5, pady=15)

master.mainloop()

What I did was just renaming the second variable b2the same as the first b1and deleting, in the solution, the first button text (so only the second is visible and will act as a single one).

我所做的只是将第二个变量重命名b2为与第一个变量相同的名称b1,并在解决方案中删除第一个按钮文本(因此只有第二个是可见的,并且将作为一个单独的)。

I also tried the function solution but for an obscure reason it don't work for me.

我也尝试了函数解决方案,但由于一个晦涩的原因,它对我不起作用。

回答by Rahim Mazouz

this is a short example : while pressing the next button it will execute 2 functions in 1 command option

这是一个简短的示例:按下下一步按钮时,它将在 1 个命令选项中执行 2 个功能

    from tkinter import *
    window=Tk()
    v=StringVar()
    def create_window():
           next_window=Tk()
           next_window.mainloop()

    def To_the_nextwindow():
        v.set("next window")
        create_window()
   label=Label(window,textvariable=v)
   NextButton=Button(window,text="Next",command=To_the_nextwindow)

   label.pack()
   NextButton.pack()
   window.mainloop()