Open In App
Related Articles

PyQt5 – How to access content of label ?

Like Article
Save Article
Report issue
We can create label using QLabel() method, and set the content using setText() method. In this article, we will see how to access the content of the label, in order to do this we will use text() method.
Syntax : label.text() Argument : It takes no argument. Return : It returns a string.
Code :
# importing the required libraries
from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 
import sys
class Window(QMainWindow):
    def __init__(self):
        # set the title
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
        # creating a label widget
        self.label_1 = QLabel("old label ", self)
        # setting up the border and background color
        self.label_1.setStyleSheet("border :3px solid black; 
                                    background : pink")
        # getting the content of label
        content = self.label_1.text()
        # printing the content of label
        # moving the label
        self.label_1.move(10, 100)
        # creating a new label widget
        self.label_2 = QLabel("new Label ", self)
        # setting up the border and background color
        self.label_2.setStyleSheet("border :3px solid black; 
                                    background : green")
        # moving the label
        self.label_2.move(110, 110)
        # printing the content of label
        # show all the widgets
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :
old label 
new Label 

Last Updated : 26 Mar, 2020
Like Article
Save Article
Share your thoughts in the comments
Similar Reads