Update the Tkinter Button Text

Jinku Hu Mar 21, 2022
  1. Use StringVar to Change the Tkinter Button Text
  2. Tkinter Button text Property to Change the Button Text
Update the Tkinter 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()
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

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

Related Article - Tkinter Button