How to change the Tkinter label text?

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing everything from tkinter
from tkinter import *
  
# creating the tkinter window
Main_window = Tk()
  
# variable
my_text = "GeeksforGeeks updated !!!"
  
# function define for 
# updating the my_label
# widget content
def counter():
    
    # use global variabel
    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 widget
my_label = Label(Main_window,
                 text = "geeksforgeeks")
  
# place the widgets 
# in the gui window
my_label.pack()
my_button.pack()
  
# Start the GUI 
Main_window.mainloop()

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing everything from tkinter
from tkinter import *
  
# create gui window
Main_window = Tk()
  
# set the configuration 
# of the window
Main_window.geometry("220x100")
  
# define a function 
# for setting the new text 
def java():
    my_string_var.set("You must go with Java")
  
# define a function 
# for setting the new text 
def 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 function
btn_2 = Button(Main_window, 
               text = "I love Machine Learning"
               command = python)
  
# create a StringVar class
my_string_var = StringVar()
  
# set the text
my_string_var.set("What should I learn")
  
# create a label widget
my_label = Label(Main_window, 
                 textvariable = my_string_var)
  
  
# place widgets into 
# the gui window
btn_1.pack()
btn_2.pack()
my_label.pack()
  
# Start the GUI  
Main_window.mainloop()

chevron_right


 
 

 

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.