📜  如何将多个命令绑定到 Tkinter 按钮?

📅  最后修改于: 2022-05-13 01:55:21.982000             🧑  作者: Mango

如何将多个命令绑定到 Tkinter 按钮?

Tkinter 中的按钮小部件提供了一种与应用程序交互的方式。用户按下按钮以执行附加到该按钮的某些操作。一般来说,我们用户为一个按钮提供了一个单一的动作,但是如果用户想要将多个动作附加到一个按钮上怎么办。

在本文中,我们将了解如何将多个动作/命令绑定到单个按钮。

要在 Tkinter 中创建按钮,请遵循以下语法。

方法 1 :通过使用 lambda函数和列表

在此方法中,我们将向 lambda 传递一个函数列表,然后将该 lambda 传递给 command。

Python3
# Import tkinter and Button Widget
from tkinter import Tk
from tkinter.ttk import Button
  
  
# Demo function 1
def fun1():
    print("Function 1")
  
  
# Demo function 2
def fun2():
    print("Function 2")
  
  
if __name__ == "__main__":
    # Creating top-level window
    master = Tk()
  
    # Setting window title
    master.title("Bind multiple function to Button")
  
    # Setting window Dimensions
    master.geometry("400x250")
  
    # Creating a button with more than one command using lambda
    button = Button(master, text="Button", command=lambda: [fun1(), fun2()])
  
    # Attaching button to the top-level window
    # Always remember to attach your widgets to the top-level
    button.pack()
  
    # Mainloop that will run forever
    master.mainloop()


Python3
# Import tkinter and Button Widget
from tkinter import Tk
from tkinter.ttk import Button
  
# funcs parameter will have the reference
# of all the functions that are passed as arguments i.e "fun1" and "fun2"
def combine_funcs(*funcs):
  
    # this function will call the passed functions
    # with the arguments that are passed to the functions
    def inner_combined_func(*args, **kwargs):
        for f in funcs:
  
            # Calling functions with arguments, if any
            f(*args, **kwargs)
  
    # returning the reference of inner_combined_func
    # this reference will have the called result of all
    # the functions that are passed to the combined_funcs
    return inner_combined_func
  
# Demo function 1
def fun1():
    print("Function 1")
  
# Demo function 2
def fun2():
    print("Function 2")
  
if __name__ == "__main__":
    # Creating top-level window
    master = Tk()
  
    # Setting window title
    master.title("Bind multiple function to Button")
  
    # Setting window Dimensions
    master.geometry("400x250")
  
    # Creating a button with more than one
    # command our own generic function
    button = Button(master, text="Button", 
                    command=combine_funcs(fun1, fun2))
  
    # Attaching button to the top-level window
    # Always remember to attach your widgets to the top-level
    button.pack()
  
    # Mainloop that will run forever
    master.mainloop()


Python3
# Import tkinter and Button Widget
from tkinter import Tk
from tkinter.ttk import Button
  
# funcs parameter will have the reference
# of all the functions that are 
# passed as arguments i.e "fun1" and "fun2"
def combine_funcs(*funcs):
  
    # this function will call the passed functions
    # with the arguments that are passed to the functions
    def inner_combined_func(*args, **kwargs):
        for f in funcs:
  
            # Calling functions with arguments, if any
            f(*args, **kwargs)
  
    # returning the reference of inner_combined_func
    # this reference will have the called result of all
    # the functions that are passed to the combined_funcs
    return inner_combined_func
  
  
# Demo function 1 with params
def fun1(param):
    print("Function 1 {}".format(param))
  
  
# Demo function 2 with params
def fun2(param):
    print("Function 2 {}".format(param))
  
  
if __name__ == "__main__":
    # Creating top-level window
    master = Tk()
  
    # Setting window title
    master.title("Bind multiple function to Button")
  
    # Setting window Dimensions
    master.geometry("400x250")
  
    # Creating a button with more than
    # one command our own generic function
    button = Button(master,
                    text="Button",
                      
                    # Passing arguments to "fun1" and "fun2"
                    command=combine_funcs(lambda: fun1("Function 1 PARAM"),
                                          lambda: fun2("Function 2 PARAM")))
  
    # Attaching button to the top-level window
    # Always remember to attach your widgets to the top-level
    button.pack()
  
    # Mainloop that will run forever
    master.mainloop()


输出:

方法 2 :通过创建我们自己的通用函数来为我们调用函数。

蟒蛇3

# Import tkinter and Button Widget
from tkinter import Tk
from tkinter.ttk import Button
  
# funcs parameter will have the reference
# of all the functions that are passed as arguments i.e "fun1" and "fun2"
def combine_funcs(*funcs):
  
    # this function will call the passed functions
    # with the arguments that are passed to the functions
    def inner_combined_func(*args, **kwargs):
        for f in funcs:
  
            # Calling functions with arguments, if any
            f(*args, **kwargs)
  
    # returning the reference of inner_combined_func
    # this reference will have the called result of all
    # the functions that are passed to the combined_funcs
    return inner_combined_func
  
# Demo function 1
def fun1():
    print("Function 1")
  
# Demo function 2
def fun2():
    print("Function 2")
  
if __name__ == "__main__":
    # Creating top-level window
    master = Tk()
  
    # Setting window title
    master.title("Bind multiple function to Button")
  
    # Setting window Dimensions
    master.geometry("400x250")
  
    # Creating a button with more than one
    # command our own generic function
    button = Button(master, text="Button", 
                    command=combine_funcs(fun1, fun2))
  
    # Attaching button to the top-level window
    # Always remember to attach your widgets to the top-level
    button.pack()
  
    # Mainloop that will run forever
    master.mainloop()

在上面的方法中,您可能想知道我们将如何将参数传递给fun1fun2因为如果我们执行以下操作

combine_funcs(fun1(arguments), fun2(arguments))

它会在应用程序运行后立即调用这些函数,但我们希望这些函数仅在按下按钮时才被调用。因此,如果您想将参数传递给 fun1 或 fun2,答案很简单,请使用以下语法:

combine_funcs(lambda: fun1(arguments), lambda: fun2(arguments))

让我们看看下面的例子,我们实际上有fun1fun2的参数。

蟒蛇3

# Import tkinter and Button Widget
from tkinter import Tk
from tkinter.ttk import Button
  
# funcs parameter will have the reference
# of all the functions that are 
# passed as arguments i.e "fun1" and "fun2"
def combine_funcs(*funcs):
  
    # this function will call the passed functions
    # with the arguments that are passed to the functions
    def inner_combined_func(*args, **kwargs):
        for f in funcs:
  
            # Calling functions with arguments, if any
            f(*args, **kwargs)
  
    # returning the reference of inner_combined_func
    # this reference will have the called result of all
    # the functions that are passed to the combined_funcs
    return inner_combined_func
  
  
# Demo function 1 with params
def fun1(param):
    print("Function 1 {}".format(param))
  
  
# Demo function 2 with params
def fun2(param):
    print("Function 2 {}".format(param))
  
  
if __name__ == "__main__":
    # Creating top-level window
    master = Tk()
  
    # Setting window title
    master.title("Bind multiple function to Button")
  
    # Setting window Dimensions
    master.geometry("400x250")
  
    # Creating a button with more than
    # one command our own generic function
    button = Button(master,
                    text="Button",
                      
                    # Passing arguments to "fun1" and "fun2"
                    command=combine_funcs(lambda: fun1("Function 1 PARAM"),
                                          lambda: fun2("Function 2 PARAM")))
  
    # Attaching button to the top-level window
    # Always remember to attach your widgets to the top-level
    button.pack()
  
    # Mainloop that will run forever
    master.mainloop()

输出