本文主要是介绍Pyqt5:多线程任务、窗体打开、常用控件介绍(含基础Demo),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、多线程任务和第二窗体打开demo
【main】
import untitled
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBoxif __name__ == '__main__':app = QApplication(sys.argv)MainWindow = QMainWindow()ui = untitled.Ui_MainWindow()#也可能是Ui_Form/Ui_widget这里填写的是生成的类名ui.setupUi(MainWindow)MainWindow.show()sys.exit(app.exec_())
【untitled.py】
# -*- coding: utf-8 -*-
import time# Form implementation generated from reading ui file 'untitled.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
from PyQt5.QtCore import QThread, pyqtSignalclass Ui_MainWindow(object):def __init__(self):super().__init__()def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(800, 600)icon = QtGui.QIcon()icon.addPixmap(QtGui.QPixmap("ril.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)MainWindow.setWindowIcon(icon)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.pushButton = QtWidgets.QPushButton(self.centralwidget)self.pushButton.setGeometry(QtCore.QRect(300, 150, 181, 31))self.pushButton.setObjectName("pushButton")self.pushButton.clicked.connect(self.open_new_window)self.progressBar = QtWidgets.QProgressBar(self.centralwidget)self.progressBar.setGeometry(QtCore.QRect(30, 440, 741, 51))self.progressBar.setProperty("value", 0)self.progressBar.setObjectName("progressBar")self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)self.pushButton_2.setGeometry(QtCore.QRect(290, 260, 201, 28))self.pushButton_2.setObjectName("pushButton_2")MainWindow.setCentralWidget(self.centralwidget)self.worker = WorkerThread() # 创建WorkerThread实例self.worker.progress.connect(self.update_progress) # 多线程连接进度条self.pushButton_2.clicked.connect(self.worker.start) # 开始按钮连接多线程self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.pushButton.setText(_translate("MainWindow", "点击我打开另一个窗体"))self.pushButton_2.setText(_translate("MainWindow", "点击我,执行多线程任务"))def open_new_window(self):import Form2# 创建新的子窗口self.new_window = Form2.Form2() # 使用 Form2 类self.new_window.setWindowTitle("一个新的窗体")self.new_window.show() # 显示新窗体def update_progress(self, value):#设置进度条的值self.progressBar.setValue(value)class WorkerThread(QThread): #多线程任务progress = pyqtSignal(int) # 自定义信号用于发送进度def run(self):for i in range(1, 6):time.sleep(i) # 模拟耗时操作self.progress.emit(i * 20) # 发送当前进度print(f"任务{i}完成了")self.progress.emit(100) # 任务全部完成
【Form2】
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'Form2.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, QtWidgetsclass Form2(QtWidgets.QWidget):def __init__(self):super().__init__()self.setupUi(self) # 初始化界面def setupUi(self, Form):Form.setObjectName("Form")Form.resize(571, 345)self.label = QtWidgets.QLabel(Form)self.label.setGeometry(QtCore.QRect(180, 90, 281, 161))font = QtGui.QFont()font.setFamily("Arial")font.setPointSize(36)self.label.setFont(font)self.label.setObjectName("label")self.retranslateUi(Form)QtCore.QMetaObject.connectSlotsByName(Form)def retranslateUi(self, Form):_translate = QtCore.QCoreApplication.translateForm.setWindowTitle(_translate("Form", "Form"))self.label.setText(_translate("Form", "子窗体"))
【效果图】
运行main.py函数,点击打开窗体即打开Form2,点击多线程任务即可实现多线程任务。
这篇关于Pyqt5:多线程任务、窗体打开、常用控件介绍(含基础Demo)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!