Pyqt5 - 实现加载界面至主界面
目录
一,实际演示如下:
图片展示
二、首先创建,加载界面和主界面【利用QT工具创建】
【1】加载界面文件如下,文件命名为:loadwin_ui.py,可自定义
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'f:\personaldata\Python资料\Python_studyltem\视频转化为图片\myqt\loadwin.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_loadwin(object):
def setupUi(self, loadwin):
loadwin.setObjectName("loadwin")
loadwin.resize(455, 349)
self.gridLayout = QtWidgets.QGridLayout(loadwin)
self.gridLayout.setObjectName("gridLayout")
self.label_logo = QtWidgets.QLabel(loadwin)
self.label_logo.setMinimumSize(QtCore.QSize(0, 280))
self.label_logo.setText("")
self.label_logo.setObjectName("label_logo")
self.gridLayout.addWidget(self.label_logo, 0, 0, 1, 1)
self.label_info = QtWidgets.QLabel(loadwin)
self.label_info.setObjectName("label_info")
self.gridLayout.addWidget(self.label_info, 1, 0, 1, 1)
self.progressBar = QtWidgets.QProgressBar(loadwin)
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.gridLayout.addWidget(self.progressBar, 2, 0, 1, 1)
self.retranslateUi(loadwin)
QtCore.QMetaObject.connectSlotsByName(loadwin)
def retranslateUi(self, loadwin):
_translate = QtCore.QCoreApplication.translate
loadwin.setWindowTitle(_translate("loadwin", "Form"))
self.label_info.setText(_translate("loadwin", "TextLabel"))
【2】主界面文件如下,文件命名为:Ui_maiwin.py,可自定义
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'f:\personaldata\Python资料\Python_studyltem\视频转化为图片\myqt\maiwin.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_mainwin(object):
def setupUi(self, mainwin):
mainwin.setObjectName("mainwin")
mainwin.resize(609, 432)
self.calendarWidget = QtWidgets.QCalendarWidget(mainwin)
self.calendarWidget.setGeometry(QtCore.QRect(170, 80, 248, 197))
self.calendarWidget.setObjectName("calendarWidget")
self.lineEdit = QtWidgets.QLineEdit(mainwin)
self.lineEdit.setGeometry(QtCore.QRect(180, 320, 231, 21))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(mainwin)
QtCore.QMetaObject.connectSlotsByName(mainwin)
def retranslateUi(self, mainwin):
_translate = QtCore.QCoreApplication.translate
mainwin.setWindowTitle(_translate("mainwin", "Form"))
三、完整代码如下:
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, pyqtSignal, QThread, QBasicTimer
import sys
from time import sleep
from myqt.loadwin_ui import Ui_loadwin # 根据自己的目录设置文件路径
from myqt.Ui_maiwin import Ui_mainwin # 根据自己的目录设置文件路径
class LoadWin(QWidget, Ui_loadwin): # 启动画面类
def __init__(self):
super(LoadWin, self).__init__()
self.setupUi(self)
self.setWindowFlags(Qt.FramelessWindowHint) # 无边框
self.setStyleSheet("#Form{background-color:'#4682B4'}"
"#label_info{background-color:'#4682B4';color:white;font-weight:600;}"
) # 设置启动窗背景色和进度信息的字体样式等
pix = QPixmap("./load_logo.png") # 加载logo
self.label_logo.setPixmap(pix)
self.label_logo.setScaledContents(True) # 图像拉伸填充
self.timer = QBasicTimer() # 定时器对象
self.step = 0 # 进度值
self.proess_run()
self.ret = None
# 第一步执行流程
def proess_run(self): # 启动进度线程
self.cal = LoadThread() # 线程对象
self.cal.part_signal.connect(self.process_set_part)
self.cal.data_signal.connect(self.show_main_win)
self.cal.start() # 调用线程run
def process_set_part(self, num):
self.step = num # 进度从num开始
self.progressBar.setValue(self.step)
if num == 0:
self.timer.start(20, self) # 启动QBasicTimer, 每20毫秒调用一次回调函数
self.label_info.setText("等待主页加载...")
def timerEvent(self, *args, **kwargs): # QBasicTimer的事件回调函数
self.progressBar.setValue(self.step) # 设置进度条的值
if self.step < 100:
self.step += 1
else:
self.cal.set_ret()
# 显示主界面
def show_main_win(self, mes):
w.close()
zhu.set_data(mes)
zhu.show()
class LoadThread(QThread): # 自定义计算线程类 -----------
part_signal = pyqtSignal(int) # 进度信号
data_signal = pyqtSignal(str) # 数据传输信号
def __init__(self):
super().__init__()
self.ret = None
# 启动线程
def run(self):
# 该消息发送后,启用process_set_part函数
self.part_signal.emit(0)
while True:
if self.ret == True:
self.data_signal.emit("计算结果:2023 要结束了!") #发送消息使主界面功能加载
break
else:
sleep(1)
continue
# 建立标志
def set_ret(self):
self.ret = True
class MainWin(QWidget, Ui_mainwin): # 主页界面类 -----------
def __init__(self):
super(MainWin, self).__init__()
self.setupUi(self)
self.setWindowTitle("主界面")
def set_data(self, mes): # 接收进度线程的计算结果
self.lineEdit.setText(mes)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = LoadWin()
w.show()
zhu = MainWin()
sys.exit(app.exec())