HomeLanguagesHow to Get the Tkinter Label Text?

How to Get the Tkinter Label Text?

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 neveropen",
             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 neveropen", 
             bg = "red")
  
# using dictionary label object
print("Label text: ", l["text"])
  
l.pack()
tk.mainloop()


Output:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS