How to Get the Tkinter Label Text?
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 tkinter as tk
master = tk.Tk()
master.configure(bg = 'light grey' )
l = tk.Label(master,
text = "Welcome to geeksforgeeks" ,
bg = "red" )
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 tkinter as tk
master = tk.Tk()
master.configure(bg = 'light grey' )
l = tk.Label(master,
text = "Welcome to geeksforgeeks" ,
bg = "red" )
print ( "Label text: " , l[ "text" ])
l.pack()
tk.mainloop()
|
Output:
Share your thoughts in the comments
Please Login to comment...