Have multiple commands when button is pressed

2019-01-17 18:15发布

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.

5条回答
2楼-- · 2019-01-17 18:36

You can simply use lambda like this:

self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])
查看更多
放我归山
3楼-- · 2019-01-17 18:53

You can use the lambda for this:

self.testButton = Button(self, text = "test", lambda: [f() for f in [func1, funct2]])
查看更多
虎瘦雄心在
4楼-- · 2019-01-17 18:56

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))
查看更多
我只想做你的唯一
5楼-- · 2019-01-17 19:00
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 ...

查看更多
疯言疯语
6楼-- · 2019-01-17 19:01

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

查看更多
登录 后发表回答