Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have multiple commands when button is pressed

Tags:

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.

like image 262
user1876508 Avatar asked Dec 13 '12 17:12

user1876508


People also ask

Can you add 2 commands to a button in tkinter?

The Tkinter button has only one command property so that multiple commands or functions should be wrapped to one function that is bound to this command .

How do I get the value of a button in Python?

Practical Data Science using Python Let us suppose that for a particular application, we want to retrieve the button value by its name. In such cases, we can use the . cget() function.


2 Answers

You can simply use lambda like this:

self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])
like image 70
pradepghr Avatar answered Oct 20 '22 03:10

pradepghr


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))
like image 40
Andrew Clark Avatar answered Oct 20 '22 01:10

Andrew Clark