Replace Tkinter label text on button press in Python
In this tutorial, let’s learn how to replace the tkinter label text on button press in Python. tkinter is the Python standard interface to the Tk/Tcl GUI toolkit. Using the tkinter package, users can create GUIs (graphical user interfaces) for desktop applications using various widgets like Text, Button, etc.
Initial Setup
If you have downloaded and installed Python from their official website, then the tkinter package should be installed in your system. But if it is not pre-installed for some reason, then execute the following command.
pip install tk
Now import the tkinter package into a Python file using an alias tk.
import tkinter as tk
Replace Tkinter label text on button press using config()
As we have the tkinter package imported into the file, let’s replace the label text on the button press using the config() method.
window = tk.Tk() window.geometry("620x360") def handleClick(): label.config(text="CodeSpeedy blog") label = tk.Label(window, text="Where to read the best programming content?") label.pack(pady=50) btn = tk.Button(window, text="Get Answer", command=handleClick) btn.pack() window.mainloop()
In the above code, first, we have created a new tk.Tk() class instance called window. This is the window that will be displayed to the user. After this, use the geometry method to set the width and height of the window. Create a function called handleClick() that runs on the button click. In this function, change the label text using the config() method.
Now create a label using the Label() method by giving two parameters, namely window, and text. The window is the window where the label will be displayed, and text will be the label’s text content. pack() is a geometry manager used to organize widgets in blocks horizontally or vertically. Also, the pady attribute in pack() is the padding in the y-axis relative to the label widget.
Create a button using the Button() method of tkinter. It has three parameters, namely the window on which the button will be displayed, the text content of the button, and the command that executes on button click. Here the command is the handleClick() function, which will change the text content of the label. Now pack the button too. The mainloop() function ensures the window is running until the user exits or some event happens that will stop it.
Output:
We also have other ways to achieve the same result.
By accessing text property using []
import tkinter as tk window = tk.Tk() window.geometry("620x360") def handleClick(): label['text'] = "CodeSpeedy blog" label = tk.Label(window, text="Where to read the best programming content?") label.pack(pady=50) btn = tk.Button(window, text="Get Answer", command=handleClick) btn.pack() window.mainloop()
The only difference here compared to the previous method is accessing the text property of the label directly and changing its value in the handleClick() function. The output should be the same as before.
Using the set() function
import tkinter as tk window = tk.Tk() window.geometry("620x360") def handleClick(): text.set("CodeSpeedy blog") text = tk.StringVar() text.set("Where to read the best programming content?") label = tk.Label(window, textvariable=text) label.pack(pady=50) btn = tk.Button(window, text="Get Answer", command=handleClick) btn.pack() window.mainloop()
Here the difference is we use the StringVar() function to store the text. Then refer the label to this variable, so the label will have it as its text content. Then on clicking the button, the handleClick()
function will update the text content using the set()
method. The output will be the same as before.
Leave a Reply