Cómo actualizar el texto del botón Tkinter
-
Usa
StringVarpara cambiar el texto del botón de Tkinter -
La propiedad
textdel botón de Tkinter para cambiar el texto del botón
En este tutorial, introduciremos cómo cambiar el texto del botón de Tkinter. Es similar a los métodos para cambiar el texto de la etiqueta de Tkinter,
- Método
StringVar - Botón
textMétodo de propiedad
Usa StringVar para cambiar el texto del botón de Tkinter
StringVar es un tipo de constructor de Tkinter para crear la variable de cadena de Tkinter.
Después de asociar la variable StringVar al widget Button de Tkinter, Tkinter actualizará el texto de este Button cuando la variable sea modificada.
import tkinter as tk
class Test:
def __init__(self):
self.root = tk.Tk()
self.root.geometry("250x100")
self.text = tk.StringVar()
self.text.set("Original Text")
self.buttonA = tk.Button(self.root, textvariable=self.text)
self.buttonB = tk.Button(
self.root, text="Click to change text", command=self.changeText
)
self.buttonA.pack(side=tk.LEFT)
self.buttonB.pack(side=tk.RIGHT)
self.root.mainloop()
def changeText(self):
self.text.set("Updated Text")
app = Test()

self.text = tk.StringVar()
self.text.set("Original Text")
El constructor de Tkinter no pudo iniciar la variable cadena con la cadena como self.text = tk.StringVar("Text").
Deberíamos llamar al método set para establecer el valor de StringVar, como self.text.set("Original Text").
self.buttonA = tk.Button(self.root, textvariable=self.text)
La variable StringVar self.text se asigna a la opción textvariable de self.buttonA. Tkinter actualizará el texto de self.buttonA automáticamente si self.text es modificado.
La propiedad text del botón de Tkinter para cambiar el texto del botón
Otra solución para cambiar el texto del botón de Tkinter es cambiar la propiedad text del botón.
import tkinter as tk
class Test:
def __init__(self):
self.root = tk.Tk()
self.root.geometry("250x100")
self.buttonA = tk.Button(self.root, text="Original Text")
self.buttonB = tk.Button(
self.root, text="Click to change text", command=self.changeText
)
self.buttonA.pack(side=tk.LEFT)
self.buttonB.pack(side=tk.RIGHT)
self.root.mainloop()
def changeText(self):
self.buttonA["text"] = "Updated Text"
app = Test()
text es una clave del objeto Button cuyo texto puede ser iniciado con text="Original Text" y también puede ser actualizado asignando el nuevo valor a text.
El método tk.Button.configure() también podría cambiar la propiedad text para cambiar el texto de Tkinter Button, como se muestra a continuación.
import tkinter as tk
class Test:
def __init__(self):
self.root = tk.Tk()
self.root.geometry("250x100")
self.buttonA = tk.Button(self.root, text="Original Text")
self.buttonB = tk.Button(
self.root, text="Click to change text", command=self.changeText
)
self.buttonA.pack(side=tk.LEFT)
self.buttonB.pack(side=tk.RIGHT)
self.root.mainloop()
def changeText(self):
self.buttonA.configure(text="Updated Text")
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