Open In App

How to Get the Tkinter Label Text?

Last Updated : 11 Dec, 2020
Suggest changes
Share
Like Article
Like
Report

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:


Next Article

Similar Reads

Article Tags :
Practice Tags :