Come legare più comandi al pulsante Tkinter

In questo tutorial, mostreremo come legare più comandi ad un Tkinter button. I comandi multipli saranno eseguiti dopo aver cliccato il pulsante.
Legare più comandi al pulsante Tkinter
Il pulsante Tkinter ha una sola proprietà di command
, in modo che più comandi o funzioni siano raggruppati in un’unica funzione legata a questo command
.
Potremmo usare lambda
per combinare più comandi come,
command=lambda:[funcA(), funcB(), funcC()]
Questa funzione lambda
eseguirà funcA
, funcB
e funcC
uno per uno.
Esempio di comandi multipli di labmda
bind
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()
Combinare le funzioni in un’unica funzione
def combineFunc(self, *funcs):
def combinedFunc(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combinedFunc
Sopra la funzione definisce una funzione all’interno di una funzione e poi restituisce l’oggetto della funzione.
for f in funcs:
f(*args, **kwargs)
Esegue tutte le funzioni indicate nella parentesi di 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()
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.
LinkedInArticolo correlato - Tkinter Button
- Come passare gli argomenti al comando del pulsante Tkinter
- Come chiudere una finestra Tkinter con un pulsante
- Come cambiare lo stato del pulsante Tkinter
- Come creare una nuova finestra cliccando un pulsante in Tkinter
- Modifica la dimensione del pulsante Tkinter