Come modificare il testo dell'etichetta Tkinter
-
Utilizzare
StringVarper cambiare il testo dell’etichetta Tkinter -
Proprietà
textdell’etichetta per modificare il testo dell’etichetta
In questo tutorial introduciamo come modificare il testo dell’etichetta Tkinter cliccando su un pulsante.
Utilizzare StringVar per cambiare il testo dell’etichetta Tkinter
StringVar è un tipo di costruttore di Tkinter per creare la variabile stringa di Tkinter.
Dopo aver associato la variabile StringVar ai widget di Tkinter, Tkinter aggiornerà questo particolare widget quando la variabile verrà modificata.
import tkinter as tk
class Test:
def __init__(self):
self.root = tk.Tk()
self.text = tk.StringVar()
self.text.set("Test")
self.label = tk.Label(self.root, textvariable=self.text)
self.button = tk.Button(
self.root, text="Click to change text below", command=self.changeText
)
self.button.pack()
self.label.pack()
self.root.mainloop()
def changeText(self):
self.text.set("Text updated")
app = Test()


self.text = tk.StringVar()
self.text.set("Test")
Il costruttore di Tkinter non può avviare la variabile stringa con la stringa come self.text = tk.StringVar().
Dovremmo chiamare il metodo set per impostare il valore StringVar, come self.text.set("Test").
self.label = tk.Label(self.root, textvariable=self.text)
Esso associa la variabile StringVar alla variabile self.text al widget label self.label impostando textvariable come self.text. Il toolkit Tk inizia a tracciare le modifiche di self.text e aggiornerà il testo self.label se self.text viene modificato.
Proprietà text dell’etichetta per modificare il testo dell’etichetta
Un’altra soluzione per cambiare il testo dell’etichetta di Tkinter è cambiare la proprietà “testo” dell’etichetta.
import tkinter as tk
class Test:
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(self.root, text="Text")
self.button = tk.Button(
self.root, text="Click to change text below", command=self.changeText
)
self.button.pack()
self.label.pack()
self.root.mainloop()
def changeText(self):
self.label["text"] = "Text updated"
app = Test()
Il testo dell’etichetta potrebbe essere iniziato con text="Text" e potrebbe anche essere aggiornato assegnando il nuovo valore alla chiave text dell’oggetto etichetta.
Si potrebbe anche cambiare la proprietà text con il metodo tk.Label.configure() come mostrato di seguito. Funziona allo stesso modo con i codici di cui sopra.
import tkinter as tk
class Test:
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(self.root, text="Text")
self.button = tk.Button(
self.root, text="Click to change text below", command=self.changeText
)
self.button.pack()
self.label.pack()
self.root.mainloop()
def changeText(self):
self.label.configure(text="Text Updated")
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.
LinkedIn Facebook