Skip to content

Tkinter: Changing tk.Button text with Stringvar() and trace_add

Tkinter: Changing tk.Button text with Stringvar() and trace_add

In this example, we will see how we can create a text Entry and connect a variable to it. The text in the Entry will then dynamically update a button on the form. You can see how to create a Tkinter application and a button in this earlier post.

The code

#! python3

# -*- coding: utf-8 -*-

import tkinter as tk

class entries_tester_app(tk.Tk):
def __init__(self, frame_title):
super().__init__()
# Create a title for the frame
self.title(frame_title)
# Create a variable for holding the text in the Entry
self.entry_text_var = tk.StringVar()
self.entry_text_var.trace_add("write", self.change_button_text)
# Create an Entry connected to the entry_text_var
self.entry_box = tk.Entry(self, text=self.entry_text_var)
# Create the button
self.print_button = tk.Button(self, text = "PLACEHOLDER TEXT", command=self.print_text)
# Put the button and the entry to the frame
self.entry_box.pack()
self.print_button.pack()

def change_button_text(self, *args): # Note the args
# Change the button text
self.print_button.config(text=self.entry_text_var.get())

def print_text(self):
# Print the text to the console
print("You entered: \"{}\"".format(self.entry_box.get()))

if __name__ == "__main__":
root = entries_tester_app("Entry test")
root.mainloop()

The explanation

We need to create a variable that we can track. We create a StringVar() (self.entry_text_var) to hold our text entry. You can also create number variables and boolean variables. We trace the variable with the trace_add method. Note that trace_add replaces the trace method and that the modes are "write", "read" and "unset". We also connect the trace to the change_button_text method.

The button's text will now update as you write in the text entry. Pretty cool right!