Wednesday, July 8, 2026
HomeLanguagesPyQt5 – How to access content of label ?

PyQt5 – How to access content of label ?

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):
        super().__init__()
  
  
        # set the title
        self.setWindowTitle("Python")
  
        # 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
        print(content)
  
        # 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
        print(self.label_2.text())
  
        # show all the widgets
        self.show()
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())


Output :

old label 
new Label 

RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12016 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12111 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6978 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS