Skip to content
Related Articles

Related Articles

Improve Article

How to Get the Tkinter Label Text?

  • Difficulty Level : Basic
  • Last Updated : 11 Dec, 2020

Prerequisite: Python GUI – Tkinter

Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using tkinter is an easy task.

In this article, we are going to write a Python script to get the tkinter label text. Below are the various methods discussed:

Method #1: Using cget() method.

Approach:



  • Importing the module.
  • Create the main window (container).
  • Add Label widgets to the main window.
  • Apply the cget() method and get label text.

Implementation:

Python3




# import modules
import tkinter as tk
  
# object of tkinter
# and background set for light grey
master = tk.Tk()
master.configure(bg='light grey')
  
# create label
l = tk.Label(master,
             text="Welcome to geeksforgeeks",
             bg="red")
  
# apply cget()
print("Label text: ", l.cget("text"))
  
l.pack()
master.mainloop()

Output:

Method #2: Using Dictionary label object.

Approach:

  • Importing the module.
  • Create the main window (container).
  • Add Label widgets to the main window.
  • Use Dictionary label object and get label text.

Python3




# import modules 
import tkinter as tk
  
# object of tkinter 
# and background set for light grey 
master = tk.Tk() 
master.configure(bg = 'light grey')   
  
# create label
l = tk.Label(master, 
             text = "Welcome to geeksforgeeks"
             bg = "red")
  
# using dictionary label object
print("Label text: ", l["text"])
  
l.pack()
tk.mainloop()

Output:

 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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :