#importing necessary library from PyQt5.QtWidgets import QLabel, QApplication from PyQt5.QtCore import Qt #creating a QLabel widget label = QLabel('This is a centered text') #setting alignment for the text label.setAlignment(Qt.AlignCenter) #showing the widget label.show()
#creating a QLabel widget label = QLabel('This text is aligned to the right') #setting alignment for the text label.setAlignment(Qt.AlignRight | Qt.AlignVCenter) #showing the widget label.show()In this second example, we create another QLabel widget with the text "This text is aligned to the right" and set the alignment of the text to be aligned to the right horizontally and centered vertically. Package library: PyQt5.QtWidgets