Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PyQt5 – Setting and accessing name of a Label

  • Last Updated : 26 Mar, 2020

While designing a Gui (Graphical User Interface) application, we mostly end up making lots of labels. For back-end coders, it become tough to distinguish between the label. To overcome this situation, need of settings label name is required, for examples among hundreds of label we can distinguish them by setting their names like some are “info” labels, some are “warning” labels just like this.

A label is generally a graphical control element which displays text on a form. It is usually a static control; having no interactivity. A label is generally used to identify a nearby text box or other widget.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

To set the Name –

Syntax : label.setAccessibleName(name)

Argument : It takes string as argument.

Return : No return value.

To access the Name –

Syntax : label.accessibleName()

Argument : It takes no argument.

Return : It returns string.

Code :

# importing the required libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore
from PyQt5 import QtGui
import sys
class Window(QMainWindow):
    def __init__(self):
        # set the title
        # setting  the geometry of window
        self.setGeometry(0, 0, 400, 300)
        # creating a label widget
        self.label_1 = QLabel("Label", self)
        # moving position
        self.label_1.move(100, 100)
        # setting up border
        self.label_1.setStyleSheet("border: 1px solid black;")
        # setting up the name of label_1
        # getting name of label_1
        name = self.label_1.accessibleName()
        # new label to display name
        self.label_2 = QLabel(name, self)
        self.label_2.move(100, 130)
        # show all the widgets
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!