How to Update the Tkinter Button Text

  1. Use StringVar to Change the Tkinter Button Text
  2. Tkinter Button text Property to Change the Button Text

In this tutorial, we will introduce how to change the Tkinter button text. It is similar to the methods to change the Tkinter label text,

  • StringVar Method
  • Button text Property Method

Use StringVar to Change the Tkinter Button Text

StringVar is one type of Tkinter constructor to create the Tkinter string variable.

After we associate the StringVar variable to the Tkinter Button widget, Tkinter will update the text of this Button when the variable is modified.

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

Tkinter set Button text

self.text = tk.StringVar()
self.text.set("Original Text")

The Tkinter constructor couldn’t initiate the string variable with the string like self.text = tk.StringVar("Text").

We should call set method to set the StringVar value, like self.text.set("Original Text").

self.buttonA = tk.Button(self.root, textvariable=self.text)

The StringVar variable self.text is assigned to the option textvariable of self.buttonA. Tkinter will update the text of self.buttonA automatically if self.text is modified.

Tkinter Button text Property to Change the Button Text

Another solution to change the Tkinter button text is to change the text property of the button.

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 is one key of the Button object whose text could be initiated with text="Original Text" and could also be updated by assigning the new value to text.

tk.Button.configure() method could also change the text property to change the text of Tkinter Button, as shown below.

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

Related Article - Tkinter Button

  • How to Change the Tkinter Button Size
  • How to Bind Multiple Commands to Tkinter Button