Tkinter Label Textual content can simply be modified by utilizing the “config” operate after which altering the “textual content” attribute to the brand new desired textual content. Alternatively, if the label textual content has been made utilizing the “StringVar()” then the consumer can make the most of the “set()” operate to alter the label Textual content.
As Labels are essentially the most essential parts of a Graphical Consumer Interface, subsequently, it’s fairly vital to know methods to change the label textual content every time required. In case you are new to creating interfaces with python and its Tkinter library, then this submit will exhibit the completely different strategies of adjusting the label textual content contained in the Tkinter window.
How you can Change Tkinter Label Textual content Utilizing the config() Perform?
As already talked about above, the consumer can simply change the label textual content with the assistance of the config(). Nevertheless, to exhibit this, take this code to create a fundamental Tkinter Window with a label:
# Tkinter Window
tkWindow = Tk();
tkWindow.geometry(“200×200”)
# Tkinter Label
text1 = Label(tkWindow, textual content=“Good day World!”)
text1.pack()
tkWindow.mainloop()
On this code snippet:
- A easy Tkinter Window is created with a top of 200 and a width of 200 as effectively.
- A Label “text1” has been created and connected to the principle window.
Operating the above code will produce the next Tkinter Window:
To alter this Label upon button press, add the next strains of code:
text1.config(textual content= “You modified textual content”)
Button(tkWindow, textual content=‘Change Textual content!’, width=15, top=2,
command=change_text).pack(increase=True)
On this code snippet:
- A easy button has been added to the window that calls the “change_text” operate.
- The “change_text()” accesses the label variable “text1”, calls the config operate, and adjustments the worth of the “textual content” attribute.
The whole code snippet for this demonstration is as:
from tkinter import *
# Tkinter Window
tkWindow = Tk();
tkWindow.geometry(“200×200”)
text1 = Label(tkWindow, textual content=“Good day World!”)
text1.pack()
# Perform to Change the Textual content
def change_text():
text1.config(textual content= “You modified textual content”)
#Outline a button to alter textual content
Button(tkWindow, textual content=‘Change Textual content!’, width=15, top=2,
command=clear_text).pack(increase=True)
tkWindow.mainloop()
Operating this code will produce the next consequence:
The output verifies that the Label textual content adjustments as quickly because the button is pressed.
How you can Change Tkinter Label Textual content Utilizing the set() Perform?
To exhibit the working of the set() operate, first, create a Tkinter window with label textual content created via StringVar() utilizing the next strains of code:
# Tkinter Window
tkWindow = Tk();
tkWindow.geometry(“200×200”)
# Create StringVar variable
textString = StringVar()
#Give Textual content Worth to StringVar Variable
textString.set(“Good day, this Label is created via StringVar()”)
#Create Label utilizing StringVar Variable
Label(tkWindow,textvariable=textString).pack(increase=True)
tkWindow.mainloop()
On this above code:
- A variable “textString” is created via the “StringVar()” operate
- Give worth to “textString” utilizing the “set()” operate.
- Create a Label by specifying the Tkinter window and set the “textvariable” attribute equal to “textString”.
Operating the above code will produce the next output on the Tkinter window:
To alter the Label textual content utilizing the set() operate, add within the following strains of code:
def change_text():
textString.set(“Google”)
#Outline a button to alter textual content
Button(tkWindow, textual content=‘Change Textual content!’, width=15, top=2,
command=change_text).pack(increase=True)
On this code:
- A button is created that can name the “change_text()” operate
- The change_text() operate takes the StringVar variable “textString” and makes use of the “set()” operate to alter its textual content.
The whole code snippet for this instance is as follows:
# Tkinter Window
tkWindow = Tk();
tkWindow.geometry(“200×200”)
# Create StringVar variable
textString = StringVar()
#Give Textual content Worth to StringVar Variable
textString.set(“Good day, this Label is created via StringVar()”)
#Create Label utilizing StringVar Variable
Label(tkWindow,textvariable=textString).pack(increase=True)
#Outline operate to alter textual content of StringVar variable
def change_text():
textString.set(“Google”)
#Outline a button to alter textual content
Button(tkWindow, textual content=‘Change Textual content!’, width=15, top=2,
command=change_text).pack(increase=True)
tkWindow.mainloop()
Operating this whole code will produce the next final result on the Tkinter Window:
The output confirms that the Label textual content contained in the Tkinter window was modified as quickly because the button was pressed.
Conclusion
Altering the Label Textual content inside Tkinter GUI is quite a simple job that may be carried out via the usage of the config() operate and the set() operate. Altering the Label textual content is sort of a helpful motion that the developer has to consistently carry out to inform/inform the consumer about numerous actions and states. This submit has clearly proven the 2 alternative ways of adjusting the Label textual content inside Tkinter.