Pyqt5:多线程任务、窗体打开、常用控件介绍(含基础Demo)

一、多线程任务和第二窗体打开demo

Pyqt5:多线程任务、窗体打开、常用控件介绍(含基础Demo)_第1张图片

【main】

import untitled
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox

if __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, pyqtSignal


class 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.translate
        MainWindow.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, QtWidgets



class 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.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "子窗体"))

【效果图】

运行main.py函数,点击打开窗体即打开Form2,点击多线程任务即可实现多线程任务。

Pyqt5:多线程任务、窗体打开、常用控件介绍(含基础Demo)_第2张图片

Pyqt5:多线程任务、窗体打开、常用控件介绍(含基础Demo)_第3张图片

 

你可能感兴趣的:(qt,开发语言)