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

Jinku Hu 2023年1月30日 2019年12月2日
  1. 将多个命令绑定到 Tkinter Button
  2. 将函数组合为一个函数
如何将多个命令绑定到 Tkinter 按钮

在本教程中,我们将演示如何将多个命令绑定到 Tkinter 按钮。单击该按钮后将执行多个命令。

将多个命令绑定到 Tkinter Button

Tkinter 按钮只有一个 command 属性,因此应将多个命令或函数组合在一起,然后此单函数绑定到按钮的 command

我们可以用 lambda 组合多个命令,

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

lambda 函数将分别执行 funcAfuncBfuncC

labmda 绑定多个函数举例

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 多个函数绑定到按钮命令

将函数组合为一个函数

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

上面的函数在函数内部定义一个函数,然后返回该函数对象。

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

它执行函数 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()
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

相关文章 - Tkinter Button