PyQt5 – 如何对齐标签的文本

PyQt5 – 如何对齐标签的文本

在这篇文章中,我们将看到如何在PyQt5应用程序中对齐标签的文本,我们可以用三种不同的方式对齐文本,分别是左对齐、右对齐和居中对齐。

语法:

label.setAlignment(QtCore.Qt.AlignLeft)
label.setAlignment(QtCore.Qt.AlignCenter)
label.setAlignment(QtCore.Qt.AlignRight)

为了使用这个,我们必须从PyQt5导入Qtcore

from PyQt5 import QtCore

以下是执行情况:

# importing the required libraries
 
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5.QtGui import *
import sys
 
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
 
        # set the title
        self.setWindowTitle("Label")
 
        # setting  the geometry of window
        self.setGeometry(0, 0, 400, 300)
 
        # creating a label widget
        self.label_1 = QLabel('Left', self)
 
        # moving position
        self.label_1.move(100, 100)
 
        # setting up border
        self.label_1.setStyleSheet("border: 1px solid black;")
 
        # setting alignment to left
        self.label_1.setAlignment(QtCore.Qt.AlignLeft)
 
        # creating a label widget
        self.label_2 = QLabel('Center', self)
 
        # moving position
        self.label_2.move(100, 130)
 
        # setting up border
        self.label_2.setStyleSheet("border: 1px solid black;")
 
        # setting alignment to center
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
 
        # creating a label widget
        self.label_3 = QLabel('Right', self)
 
        # moving position
        self.label_3.move(100, 160)
 
        # setting up border
        self.label_3.setStyleSheet("border: 1px solid black;")
 
        # setting alignment to right
        self.label_3.setAlignment(QtCore.Qt.AlignRight)
 
        # 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())

输出 :

PyQt5 - 如何对齐标签的文本

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程