PyQt5 – Setting and accessing name of a Label
Last Updated :
29 Dec, 2022
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.
To set the Name –
Syntax : label.setAccessibleName(name) Argument : It takes string as argument. Return : No return value.
To access the Name –
Syntax : label.setaccessibleName() Argument : It takes no argument. Return : It returns string.
Code :
Python3
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5 import QtGui
import sys
class Window(QMainWindow):
def __init__( self ):
super ().__init__()
self .setWindowTitle("Name")
self .setGeometry( 0 , 0 , 400 , 300 )
self .label_1 = QLabel("Label", self )
self .label_1.move( 100 , 100 )
self .label_1.setStyleSheet("border: 1px solid black;")
self .label_1.setAccessibleName("Info")
name = self .label_1.setaccessibleName()
self .label_2 = QLabel(name, self )
self .label_2.move( 100 , 130 )
self .label_2.adjustSize()
self .show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App. exec ())
|
Output :
Share your thoughts in the comments
Please Login to comment...