How to bind multiple commands to Tkinter button

In this tutorial, we will demonstrate how to bind multiple commands to a Tkinter button. Multiple commands will be executed after the button is clicked.

Bind Multiple commands to Tkinter Button

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.

We could use lambda to combine multiple commands as,

command=lambda:[funcA(), funcB(), funcC()]

This lambda function will execute funcA, funcB, and funcC one by one.

labmda bind multiple commands example

try:
    import Tkinter as tk
except:
    import tkinter as tk
    

class Test():
   def __init__(self):
       self.root = tk.Tk()
       self.root.geometry('200x100')
       self.button = tk.Button(self.root,
                          text = 'Click Me',
                          command=lambda:[self.funcA(), self.funcB(), self.funcC()])
       self.button.pack()

       self.labelA = tk.Label(self.root, text="A")
       self.labelB = tk.Label(self.root, text="B")
       self.labelC = tk.Label(self.root, text="C")

       self.labelA.pack()
       self.labelB.pack()
       self.labelC.pack()
       
       self.root.mainloop()

   def funcA(self):
       self.labelA["text"] = "A responds"

   def funcB(self):
       self.labelB["text"] = "B responds"
   def funcC(self):
       self.labelC["text"] = "C responds"
       
app = Test()

Tkinter bind multiple commands to button

Combine Functions to One Single Function

   def combineFunc(self, *funcs):
       def combinedFunc(*args, **kwargs):
            for f in funcs:
                f(*args, **kwargs)
       return combinedFunc

Above function defines a function inside a function and then returns the function object.

for f in funcs:
                f(*args, **kwargs)

It executes all the functions given in the parenthesis of combineFunc.

try:
    import Tkinter as tk
except:
    import tkinter as tk
    

class Test():
   def __init__(self):
       self.root = tk.Tk()
       self.root.geometry('200x100')
       self.button = tk.Button(self.root,
                          text = 'Click Me',
                          command = self.combineFunc(self.funcA, self.funcB, self.funcC))
       self.button.pack()

       self.labelA = tk.Label(self.root, text="A")
       self.labelB = tk.Label(self.root, text="B")
       self.labelC = tk.Label(self.root, text="C")

       self.labelA.pack()
       self.labelB.pack()
       self.labelC.pack()
       
       self.root.mainloop()

   def combineFunc(self, *funcs):
       def combinedFunc(*args, **kwargs):
            for f in funcs:
                f(*args, **kwargs)
       return combinedFunc

   def funcA(self):
       self.labelA["text"] = "A responds"

   def funcB(self):
       self.labelB["text"] = "B responds"

   def funcC(self):
       self.labelC["text"] = "C responds"
       
app = Test()

Related Articles - Tkinter Button

  • How to Pass Arguments to Tkinter Button Command
  • How to Close a Tkinter Window With a Button
  • How to change Tkinter Button state
  • How to create a new window by clicking a button in Tkinter
  • comments powered by Disqus