HOWTO · Tkinter

Come legare più comandi al pulsante Tkinter

Questo tutorial introduce come legare più comandi al pulsante Tkinter. Esegue più comandi quando il pulsante viene premuto.

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,

def command():
    return [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()

Tkinter associa più comandi al pulsante

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()