How to Bind Multiple Commands to Tkinter Button? (original) (raw)
Last Updated : 26 Dec, 2020
The button widget in Tkinter provides a way to interact with the application. The user presses a button to perform certain actions that are attached to that button. In general, we user provides a single action to a button but what if the user wants to attach more than one action to a button.
In this article, we are going to see how we can bind more than one action/command to a single button.
To create a button in Tkinter please follow the below syntax.
Syntax: Button(master, text=”Button”, command=function, options, …)
Parameters:
- master: refers to the top-level window in which button is placed
- text: Text to show button
- command: An action which will be called on button press
There are other options as well but they are rarely used.- compound: To show both image and text
- image: To show image
- pady: To provide vertical padding
- padx: To provide horizontal padding
Method 1: By using the lambda function and list
In this method, we are going to pass a list of functions to lambda, and then that lambda will be passed to command.
Python3
from
tkinter
import
Tk
from
tkinter.ttk
import
Button
def
fun1():
`` print
(
"Function 1"
)
def
fun2():
`` print
(
"Function 2"
)
if
__name__
=
=
"__main__"
:
`` master
=
Tk()
`` master.title(
"Bind multiple function to Button"
)
`` master.geometry(
"400x250"
)
`` button
=
Button(master, text
=
"Button"
, command
=
lambda
: [fun1(), fun2()])
`` button.pack()
`` master.mainloop()
Output:
https://media.geeksforgeeks.org/wp-content/uploads/20201217192934/ice_video_20201217-192812.mp4
Method 2: By creating our own generic function that will call functions for us.
Python3
from
tkinter
import
Tk
from
tkinter.ttk
import
Button
def
combine_funcs(
*
funcs):
`` def
inner_combined_func(
*
args,
*
*
kwargs):
`` for
f
in
funcs:
`` f(
*
args,
*
*
kwargs)
`` return
inner_combined_func
def
fun1():
`` print
(
"Function 1"
)
def
fun2():
`` print
(
"Function 2"
)
if
__name__
=
=
"__main__"
:
`` master
=
Tk()
`` master.title(
"Bind multiple function to Button"
)
`` master.geometry(
"400x250"
)
`` button
=
Button(master, text
=
"Button"
,
`` command
=
combine_funcs(fun1, fun2))
`` button.pack()
`` master.mainloop()
In the above method, you may be wondering how we are going to pass arguments to fun1 and fun2 because if we do the following
combine_funcs(fun1(arguments), fun2(arguments))
It will immediately call the functions as soon as the application runs, but we want that these functions should be called only when the button is pressed. So the answer is simple if you want to pass arguments to fun1 or fun2 use the below syntax:
combine_funcs(lambda: fun1(arguments), lambda: fun2(arguments))
Let see the below example where we actually have parameters to fun1 and fun2.
Python3
from
tkinter
import
Tk
from
tkinter.ttk
import
Button
def
combine_funcs(
*
funcs):
`` def
inner_combined_func(
*
args,
*
*
kwargs):
`` for
f
in
funcs:
`` f(
*
args,
*
*
kwargs)
`` return
inner_combined_func
def
fun1(param):
`` print
(
"Function 1 {}"
.
format
(param))
def
fun2(param):
`` print
(
"Function 2 {}"
.
format
(param))
if
__name__
=
=
"__main__"
:
`` master
=
Tk()
`` master.title(
"Bind multiple function to Button"
)
`` master.geometry(
"400x250"
)
`` button
=
Button(master,
`` text
=
"Button"
,
`` command
=
combine_funcs(
lambda
: fun1(
"Function 1 PARAM"
),
`` lambda
: fun2(
"Function 2 PARAM"
)))
`` button.pack()
`` master.mainloop()
Output:
https://media.geeksforgeeks.org/wp-content/uploads/20201217192934/ice_video_20201217-192812.mp4