Label(标签)
介绍
标签控件是一款只能显示预设的文本内容而不能编辑的控件。
双击可以修改标签文本,拖动边缘可以放大缩小标签。还有字体大小、对齐方式等可以通过右边属性栏都可以设置。
该窗口生成的py代码如下:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(480, 320)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(200, 120, 91, 31))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(12)
self.label.setFont(font)
self.label.setCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 480, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "我是标签"))
其中跟标签相关的代码如下:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
...
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(200, 120, 91, 31))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(12)
self.label.setFont(font)
self.label.setCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label") #设置标签对象名称,非显示名称。
def retranslateUi(self, MainWindow):
...
self.label.setText(_translate("MainWindow", "我是标签")) #显示内容为“我是标签”