How to change the Tkinter label text? - GeeksforGeeks

22 Jul, 2021
Prerequisites: Introduction to tkinter
Tkinter is a standard GUI (Graphical user interface) package for python. It provides a fast and easy way of creating a GUI application.
To create a tkinter application:
Importing the module — tkinter
Create the main window (container)
Add any number of widgets to the main window.
Apply the event Trigger on the widgets.
Widgets are the controlling tools for any GUI application. Here widget’s main role is to provide a good variety of control. Some widgets are buttons, labels, text boxes, and many more. 
One of its widgets is the label, which is responsible for implementing a display box-section for text and images. Click here For knowing more about the Tkinter label widget.
Now, let’ see how To change the text of the label:
Method 1: Using Label.config() method.
Syntax: Label.config(text)
Parameter: text– The text to display in the label. 
This method is used for performing an overwriting over label widget.
Example:
Python3
# importing everything from tkinterfrom tkinter import * # creating the tkinter windowMain_window = Tk() # variablemy_text = "GeeksforGeeks updated !!!" # function define for# updating the my_label# widget contentdef counter():       # use global variable    global my_text         # configure    my_label.config(text = my_text) # create a button widget and attached  # with counter function  my_button = Button(Main_window,                   text = "Please update",                   command = counter) # create a Label widgetmy_label = Label(Main_window,                 text = "geeksforgeeks") # place the widgets# in the gui windowmy_label.pack()my_button.pack() # Start the GUIMain_window.mainloop()
  Output:
 
 
Method 2: Using StringVar() class.
 
Syntax: StringVar()
Return: String variable object
 This class is used the for setting the values and changing it according to the requirements. 
Python3
# importing everything from tkinterfrom tkinter import * # create gui windowMain_window = Tk() # set the configuration# of the windowMain_window.geometry("220x100") # define a function# for setting the new textdef java():    my_string_var.set("You must go with Java") # define a function# for setting the new textdef python():    my_string_var.set("You must go with Python")   # create a Button widget and attached  # with java function  btn_1 = Button(Main_window,               text = "I love Android",               command = java) # create a Button widget and attached  # with python functionbtn_2 = Button(Main_window,               text = "I love Machine Learning",               command = python) # create a StringVar classmy_string_var = StringVar() # set the textmy_string_var.set("What should I learn") # create a label widgetmy_label = Label(Main_window,                 textvariable = my_string_var)  # place widgets into# the gui windowbtn_1.pack()btn_2.pack()my_label.pack() # Start the GUI Main_window.mainloop()
 
 
manikarora059
Python-tkinter
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Python Classes and Objects
Create a directory in Python