https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QLabel.html
函数 | 作用 |
---|---|
setMovie | 显示QMovie图片 |
QPixmap | 显示QPixmap 图片 |
setText | 设置文本 |
setNum | 设置数字 |
setAlignment | 设置对齐方式 |
x | AlignLeft::水平方向靠左 |
x | AlignRight:水平方向靠右 |
x | AlignHCenter:水平方向居中 |
x | AlignJustify:水平方向调整间距两端对齐 |
x | AlignTop:垂直方向靠上 |
x | AlignBottom:垂直方向靠下 |
x | AlignVCenter:垂直方向居中 |
x | AlignCenter:等价于 Qt::AlignHCenter |
setOpenExternalLinks | 设置超链接 |
# Import Qt libraries
from PySide6.QtWidgets import *
from PySide6.QtCore import QFile,Qt,QTimer
# Import UI developed in Qt Creator
from label_ui import Ui_MainWindow # 导入界面
# Import PseudoSensor
# Import system tools and datetime
import sys
import statistics
import time
from datetime import datetime
from PySide6 import QtGui, QtWidgets
from PySide6.QtGui import QIcon, QPixmap, QMovie, QPainter, QBrush, QPen,QColor,QPalette,QFont,QImage,QPixmap
import random
# Create and start the Qt application
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# 设置界面为用户设计的界面
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.time_date = QTimer()
self.time_date.timeout.connect(self.updatetime)
self.time_date.start(1000)
# label1画椭圆
pic = QtGui.QPicture() # 创建绘画设备(画布)
painter = QtGui.QPainter(pic)
painter.setBrush(QtGui.QBrush(QtGui.QColor(0, 128, 128))) # 设置颜色
painter.drawEllipse(0, 0, 150, 150) # 绘制椭圆
painter.end()
self.ui.label.setPicture(pic) # 用于展示QPicture
# label2画矩形
pic2 = QtGui.QPicture() # 创建绘画设备(画布)
painter2 = QtGui.QPainter(pic2)
painter2.setBrush(QtGui.QBrush(QtGui.QColor(255, 0, 0))) # 设置颜色
painter2.drawRect(0, 0, 150, 150) # 绘制矩形
painter2.end()
self.ui.label_2.setPicture(pic2) # 用于展示QPicture
# label3显示gif动图
gif_pic_size = QPixmap(":/resources/image/R-C.gif").size()
self.ui.label_3.resize(gif_pic_size.width(),gif_pic_size.height()) # 设置label大小
gif = QMovie(":/resources/image/R-C.gif")
self.ui.label_3.setMovie(gif)
gif.start()
# label4显示图片
print(self.ui.label_4.size())
pic = QPixmap(":/resources/image/pic1.jpg").scaled(self.ui.label_4.size(), aspectMode=Qt.KeepAspectRatio) #图片自适应
self.ui.label_4.setPixmap(pic)
self.ui.label_5.setAlignment(Qt.AlignRight) # label5 设置水平方向靠右
self.ui.label_6.setAlignment(Qt.AlignLeft) # label6 设置水平方向靠左
# label7显示超链接
self.ui.label_7.setText("欢迎百度") # 设置超链接
self.ui.label_7.setOpenExternalLinks(True) # 允许访问链接
def updatetime(self):
self.ui.label_6.setNum(random.uniform(1, 10000)) # 显示随机数
self.ui.label_5.setText(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) # 显示文本
def closeAndExit(self):
sys.exit()
if __name__ == "__main__":
app = QApplication(sys.argv) # 初始化QApplication
# 初始化界面并显示界面
window = MainWindow()
window.show()
window.setFixedSize(window.width(), window.height())
sys.exit(app.exec())
程序可以通过百度网盘下载
链接:https://pan.baidu.com/s/1dU7wMKoSocvkLGCmfRr-ig
提取码:c9tr