1 标签(QLabel)


1.1 纯文本标签及其格式

标签 — QLable
```python from PyQt6.QtWidgets import QMainWindow, QApplication, QLabel, QFrame from PyQt6.QtCore import Qt import sys class QWindow(QMainWindow): def init(self): super().init() self.resize(180,150) self.initUI() def initUI(self): # 创建标签 lb = QLabel(self) # 可使用:self.lb = QLabel(‘歪日’),顺带设置文本信息 # 设置文本信息 lb.setText(‘歪日\n这不科学’) # 设置是否可以换行(缺省值为:False) lb.setWordWrap(True) # 设置标签位置 lb.setAlignment(Qt.AlignmentFlag.AlignCenter) # Qt.AlignmentFlag.AlignLeft 左对齐 # Qt.AlignmentFlag.AlignRight 右对齐 # Qt.AlignmentFlag.AlignHCenter 水平居中 # Qt.AlignmentFlag.AlignJustify 水平对齐可用文本 # Qt.AlignmentFlag.AlignTop 顶部对齐 # Qt.AlignmentFlag.AlignBottom 底部对齐 # Qt.AlignmentFlag.AlignVCenter 垂直居中 # Qt.AlignmentFlag.AlignBaseline 与基线对齐 # Qt.AlignmentFlag.AlignCenter 居中 等同于:AlignVCenter AlignHCenter # 设置标签形状/阴影 lb.setFrameStyle(QFrame.Shape.Panel QFrame.Shadow.Sunken) # QFrame.Shape.NoFrame 形状:无边框 # QFrame.Shape.Box 形状:边框 # QFrame.Shape.Panel 形状:凹凸面板 # QFrame.Shape.PaStyledPanelnel 形状:升起/下沉面板 # QFrame.Shape.HLine 形状:水平线(作为分隔符使用) # QFrame.Shape.VLine 形状:水平线(作为分隔符使用) # QFrame.Shape.WinPanel 形状:win样式面板 # QFrame.Shadow.Plain 阴影:与环境保持水平 # QFrame.Shadow.Raised 阴影:3D凸起线 # QFrame.Shadow.Sunken 阴影:3D凹陷线 self.setCentralWidget(lb) self.show() def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if name == ‘main‘: main() ```

1.2 富文本标签

标签 — QLable
python from PyQt6.QtWidgets import QMainWindow, QApplication, QLabel from PyQt6.QtCore import Qt import sys class QWindow(QMainWindow): def __init__(self): super().__init__() self.resize(180,150) self.initUI() def initUI(self): lb = QLabel(self) lb.setText('<h1>歪日</h1>') self.show() def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if __name__ == '__main__': main()

1.3 图片标签

图片型标签 — QLable
python import sys from PyQt6.QtWidgets import QMainWindow, QApplication, QLabel, QPushButton from PyQt6.QtGui import QPixmap class QWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): # 创建标签 lb = QLabel(self) # 即便在此设置文本,在设置图片时,文本仪就会被覆盖 # 移动标签位置 lb.move(20, 20) # 标签设置为图片型 lb.setPixmap(QPixmap('voice.png')) # 修改图片显示大小(通常是和图片连用,只修改显示尺寸,不会伸缩) # 方法一: lb.resize(300, 100) # 方法二:同时修改位置 # lb.setGeometry(20, 20, 50, 50) # 组件.setGeometry(水平位置, 竖直位置, 宽度, 高度) # 方法三:自动调节图片大小 # qlb.adjustSize() # 不设定大小的情况下,默认就是自动 # 修改图片实际大小(拉伸缩放) lb.setScaledContents(True) print(lb.size()) # 返回图片显示大小 print(lb.hasScaledContents()) # 返回图片是否拉伸缩放 # 修改图片实际大小(等比例) lb.adjustSize() # 自动调节大小 lb.resize(lb.size()*0.1) # 长宽等比例缩小为原来的 0.1 倍 lb.setScaledContents(True) # 图片可拉伸缩放 # 标签设置边距和缩进(会影响图片显示大小) # qlb.setMargin(8) # 图像外侧添加边框 lb.setStyleSheet("border: 2px solid red") # 将标签居中平铺 # self.setCentralWidget(qlb) self.show() def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if __name__ == '__main__': main()
DLC
[QLabel Class Qt Widgets 6.3.2](https://doc.qt.io/qt-6/qlabel.html)

1.4 动画标签

动画型标签 — QLable
python import sys from PyQt6.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QApplication, QLabel, QPushButton from PyQt6.QtGui import QPixmap, QMovie class QWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): # 创建图片对象 self.pix = QPixmap('变身.gif') # 创建动画对象 self.movie = QMovie("变身.gif") # 创建标签对象 self.lb = QLabel(self) # 标签设置为图片标签 self.lb.setPixmap(self.pix) # 设置图片是否可以缩放 self.lb.setScaledContents(True) self.bt1 = QPushButton('开始',self) self.bt1.clicked.connect(self.bt1clicked) self.bt2 = QPushButton('停止',self) self.bt2.clicked.connect(self.bt2clicked) self.bt3 = QPushButton('1.5x倍速',self) self.bt3.clicked.connect(self.bt3clicked) hbox = QHBoxLayout() hbox.addWidget(self.bt1) hbox.addWidget(self.bt2) hbox.addWidget(self.bt3) vbox = QVBoxLayout() vbox.addWidget(self.lb) vbox.addLayout(hbox) widget = QWidget() widget.setLayout(vbox) self.setCentralWidget(widget) self.show() def bt1clicked(self): # 标签设置为动画标签 self.lb.setMovie(self.movie) if self.bt1.text() == '开始': self.bt1.setText('暂停') # 开始动画 self.movie.start() elif self.bt1.text() == '暂停': self.bt1.setText('继续') # 暂停动画 self.movie.setPaused(True) elif self.bt1.text() == '继续': self.bt1.setText('暂停') # 继续动画 self.movie.setPaused(False) def bt2clicked(self): self.movie.stop() self.lb.setPixmap(self.pix) self.bt1.setText('开始') def bt3clicked(self): if self.bt3.text() == '1.5x倍速': # 设置动画播放速度 self.movie.setSpeed(150) self.bt3.setText('2x倍速') elif self.bt3.text() == '2x倍速': self.movie.setSpeed(200) self.bt3.setText('0.75x倍速') elif self.bt3.text() == '0.75x倍速': self.movie.setSpeed(75) self.bt3.setText('正常速度') elif self.bt3.text() == '正常速度': self.movie.setSpeed(100) self.bt3.setText('1.5x倍速') def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if __name__ == '__main__': main()
DLC
[QLabel Class Qt Widgets 6.3.2](https://doc.qt.io/qt-6/qlabel.html) [QMovie Class Qt GUI 6.3.2](https://doc.qt.io/qt-6/qmovie.html)

2 数字板(QLCDNumber)

数字板 — QDLCNumber
python from PyQt6.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QApplication, QLCDNumber, QDial from PyQt6.QtCore import Qt import sys class QWindow(QMainWindow): def __init__(self): super().__init__() self.resize(180,150) self.initUI() def initUI(self): # 创建LCD显示器 lcd = QLCDNumber(self) # 设置LCD显示器位数 lcd.setDigitCount(4) # 设置LCD数字进制 lcd.setMode(QLCDNumber.Mode.Dec) # QLCDNumber.Mode.Bin 二进制 不可显示浮点 # QLCDNumber.Mode.Oct 八进制 不可显示浮点 # QLCDNumber.Mode.Dec 十进制 可显示浮点 缺省值 # QLCDNumber.Mode.Hex 十六进制 不可显示浮点 # 设置LCD显示格式 lcd.setStyleSheet('font:bold;background:transparent') # font:bold 加粗显示 # border-image:'' 背景图片 # border-radius:3px 边框弧度 # border-width:3px 边框大小 # font-size:3px 字体大小 # font-weight:3px 字体深浅 # background-color:blue 背景颜色 # background-transparent 背景透明 # color:rgb(250,250,250) 前置颜色 # selection-color:rgb(250,250,250) 选定后颜色 # border-top/right/bottom/left 按钮四周边框 # border-top-color/style/width 边框颜色、样式、宽度 # 设定显示器数值(数字、字符串皆可) lcd.display('1314') dial = QDial() dial.setRange(0,9999) dial.setWrapping(False) # LCD显示器接收信号 dial.valueChanged.connect(lcd.display) vbox = QVBoxLayout() vbox.addWidget(lcd) vbox.addWidget(dial) widget = QWidget() widget.setLayout(vbox) self.setCentralWidget(widget) self.show() def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if __name__ == '__main__': main()
DLC
[QLCDNumber Class Qt Widgets 6.3.2](https://doc.qt.io/qt-6/qlcdnumber.html)

3 进度条(QProgressBar)

进度条 — QProgressBar
python from PyQt6.QtWidgets import QMainWindow, QWidget, QApplication, QProgressBar, QPushButton, QVBoxLayout, QHBoxLayout, QMessageBox from PyQt6.QtCore import Qt, QBasicTimer from PyQt6.QtGui import QPixmap import sys class QWindow(QMainWindow): def __init__(self): super().__init__() self.resize(180,150) self.initUI() def initUI(self): # 创建进度条 self.pb1 = QProgressBar(self) # 设置进度条方向 self.pb1.setOrientation(Qt.Orientation.Horizontal) # Qt.Orientation.Vertical 垂直放置 # Qt.Orientation.Horizontal 水平放置 # 进度条最小值(缺省值为 0) self.pb1.setMinimum(0) # 进度条最大值(缺省值为 100) self.pb1.setMaximum(200) # 当最小值和最大值都设置为 0 时,进度条变为一直滚动状态(跑马灯) # 设置进度条文本样式 self.pb1.setFormat("已完成:%p%") # '%p' 显示完成百分比,缺省值为 '%p%' # '%v' 显示数值 # '%m' 总step # 进度条方向转置(缺省值为 False:从左向右) self.pb1.setInvertedAppearance(False) self.btn1 = QPushButton('变成跑马灯',self) self.btn1.clicked.connect(self.btn1clicked) self.btn2 = QPushButton('开始',self) self.btn2.clicked.connect(self.btn2clicked) self.btn3 = QPushButton('反向移动',self) self.btn3.clicked.connect(self.btn3clicked) self.btn4 = QPushButton('垂直放置',self) self.btn4.clicked.connect(self.btn4clicked) hbox1 = QHBoxLayout() hbox1.addStretch(0) hbox1.addWidget(self.pb1) hbox1.addStretch(0) hbox2 = QHBoxLayout() hbox2.addWidget(self.btn1) hbox2.addWidget(self.btn2) hbox2.addWidget(self.btn3) hbox2.addWidget(self.btn4) vbox = QVBoxLayout() vbox.addStretch(0) vbox.addLayout(hbox1) vbox.addStretch(0) vbox.addLayout(hbox2) widget = QWidget() widget.setLayout(vbox) self.timer = QBasicTimer() self.step = 0 self.setCentralWidget(widget) self.show() def timerEvent(self, e): if self.step >= 200: self.timer.stop() QMessageBox.information(self,'提示','运行结束') self.btn2.setText('再来一次') self.step = 0 return self.step = self.step + 1 self.pb1.setValue(self.step) print(self.step) def btn1clicked(self): if self.btn1.text() == '变成跑马灯': self.pb1.setMaximum(0) self.pb1.setInvertedAppearance(True) self.btn1.setText('变回普通进度条') else: self.pb1.setMaximum(200) self.timer.start(100, self) self.btn1.setText('变成跑马灯') if self.btn2.text() == '停止': self.timer.start(100, self) else: self.timer.stop() if self.btn1.text() == '变成跑马灯': self.btn2.setText('恢复') def btn2clicked(self): if self.btn2.text() == '停止': self.timer.stop() self.btn2.setText('继续') else: self.timer.start(100, self) self.btn2.setText('停止') def btn3clicked(self): if self.btn3.text() == '反向移动': self.pb1.setInvertedAppearance(True) self.btn3.setText('正向移动') else: self.pb1.setInvertedAppearance(False) self.btn3.setText('反向移动') def btn4clicked(self): if self.pb1.orientation() == Qt.Orientation.Vertical: self.pb1.setOrientation(Qt.Orientation.Horizontal) self.btn4.setText('垂直放置') elif self.pb1.orientation() == Qt.Orientation.Horizontal: self.pb1.setOrientation(Qt.Orientation.Vertical) self.btn4.setText('水平放置') def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if __name__ == '__main__': main()
DLC
[QProgressBar Class Qt Widgets 6.3.2](https://doc.qt.io/qt-6/qprogressbar.html)

4 画板(QPainter)

  1. import sys
  2. from PyQt6.QtWidgets import (QApplication, QLabel, QWidget)
  3. from PyQt6.QtGui import QPainter, QColor, QPen
  4. from PyQt6.QtCore import Qt
  5. class Example(QWidget):
  6. distance_from_center = 0
  7. def __init__(self):
  8. super().__init__()
  9. self.initUI()
  10. self.setMouseTracking(True)
  11. def initUI(self):
  12. self.setGeometry(200, 200, 1000, 500)
  13. self.setWindowTitle('学点编程吧')
  14. self.label = QLabel(self)
  15. self.label.resize(500, 40)
  16. self.show()
  17. self.pos = None
  18. def mouseMoveEvent(self, event):
  19. distance_from_center = round(((event.position().y() - 250)**2 + (event.position().x() - 500)**2)**0.5)
  20. self.label.setText('坐标: ( x: %d ,y: %d )' % (event.position().x(), event.position().y()) + " 离中心点距离: " + str(distance_from_center))
  21. self.pos = event.pos()
  22. self.update()
  23. def paintEvent(self, event):
  24. # 找到坐标便执行
  25. if self.pos:
  26. # 创建画板对象
  27. q = QPainter(self)
  28. # 绘制直线
  29. q.drawLine(0, 0, self.pos.x(), self.pos.y())
  30. # q.drawLine(x1,, y1, x2, y2)
  31. if __name__ == '__main__':
  32. app = QApplication(sys.argv)
  33. ex = Example()
  34. sys.exit(app.exec())

5 边框(QFrame)

边框 — QFrame
python import sys from PyQt6.QtWidgets import (QMainWindow, QWidget, QPushButton, QApplication, QGridLayout, QLabel, QFrame) class QWindow(QMainWindow): def __init__(self): super().__init__() self.Init_UI() def Init_UI(self): grid = QGridLayout() for i in range(4): grid.addWidget(QPushButton(str(i)),0,i*4,1,4) grid.addWidget(QLabel('lineWidth()'),0,16) for i in range(4): for j in range(4): grid.addWidget(QPushButton(str(j)),1,i*4+j,1,1) grid.addWidget(QLabel('midLineWidth()'),1,16) # QFrame 的 Shape 枚举 fshape = [QFrame.Shape.Box, QFrame.Shape.Panel, QFrame.Shape.WinPanel, QFrame.Shape.HLine, QFrame.Shape.VLine, QFrame.Shape.StyledPanel] # QFrame 的 Shadow 枚举 fshadow = [QFrame.Shadow.Plain, QFrame.Shadow.Raised, QFrame.Shadow.Sunken] for i in range(18): for j in range(16): # 创建边框 temp = QFrame(self) # 设置边框形状 temp.setFrameShape(fshape[i//3]) # 设置边框阴影 temp.setFrameShadow(fshadow[i%3]) # 设置边框线宽 temp.setLineWidth(j//4) # 设置边框中线宽 temp.setMidLineWidth(j%4) # 设置边框填充颜色(可连接颜色对话框) temp.setStyleSheet('background-color:#cyan') grid.addWidget(temp,i+2,j) grid.addWidget(QLabel(str(fshape[i//3]) + ',' + str(fshadow[i%3])),i+2,16) widget = QWidget() widget.setLayout(grid) self.setCentralWidget(widget) self.resize(1500,1000) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = QWindow() app.exit(app.exec())
运行结果(本人测试的和官方文档里的图有点出入,可能是栅格布局的原因)
Python PyQt6 显示类组件 - 图1
官方文档
[QFrame Class Qt Widgets 6.3.2](https://doc.qt.io/qt-6/qframe.html)