一个pyqt5动态加载ui+多线程+信号刷新界面的例子
作者:互联网
一个pyqt5动态加载ui+多线程+信号刷新界面的例子
ui代码(用designer设计生成即可)
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>全国富婆通讯录</string> </property> <widget class="QWidget" name="formLayoutWidget"> <property name="geometry"> <rect> <x>90</x> <y>40</y> <width>211</width> <height>141</height> </rect> </property> <layout class="QFormLayout" name="formLayout"> <item row="0" column="1"> <widget class="QLineEdit" name="lineEdit"/> </item> <item row="0" column="0"> <widget class="QLabel" name="label"> <property name="text"> <string>开始寻找富婆:</string> </property> </widget> </item> <item row="2" column="0"> <widget class="QPushButton" name="pushButton"> <property name="text"> <string>开始</string> </property> </widget> </item> <item row="2" column="1"> <widget class="QPushButton" name="pushButton_2"> <property name="text"> <string>停止</string> </property> </widget> </item> </layout> </widget> </widget> <resources/> <connections/> </ui>
将ui文件转换成py类后如下:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'main.ui' # # Created by: PyQt5 UI code generator 5.15.2 # # 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_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 300) self.formLayoutWidget = QtWidgets.QWidget(Form) self.formLayoutWidget.setGeometry(QtCore.QRect(90, 40, 211, 141)) self.formLayoutWidget.setObjectName("formLayoutWidget") self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget) self.formLayout.setContentsMargins(0, 0, 0, 0) self.formLayout.setObjectName("formLayout") self.lineEdit = QtWidgets.QLineEdit(self.formLayoutWidget) self.lineEdit.setObjectName("lineEdit") self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lineEdit) self.label = QtWidgets.QLabel(self.formLayoutWidget) self.label.setObjectName("label") self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label) self.pushButton = QtWidgets.QPushButton(self.formLayoutWidget) self.pushButton.setObjectName("pushButton") self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.pushButton) self.pushButton_2 = QtWidgets.QPushButton(self.formLayoutWidget) self.pushButton_2.setObjectName("pushButton_2") self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.pushButton_2) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "全国富婆通讯录")) self.label.setText(_translate("Form", "开始寻找富婆:")) self.pushButton.setText(_translate("Form", "开始")) self.pushButton_2.setText(_translate("Form", "停止"))
运行代码如下:
import random import time from threading import Thread import null as null from PyQt5.QtCore import pyqtSignal from PyQt5.QtWidgets import QApplication, QWidget from ui.main import Ui_Form class MainWindow(QWidget): signal1 = pyqtSignal(str, str) # 定义带类型参数信号 stopSign = 0 thread = null woman = ["桂花", "翠花", "桂英", "桂凤", "翠芬", "桂兰", "红梅", "腊梅", "月红", "月英", "玉兰", "玉芝", "小兰", "蓉芳", "兰芳", "彩凤", "嫦娟", "招娣", "引娣"] def __init__(self): super().__init__() self.ui = Ui_Form() self.ui.setupUi(self) str = "string" self.ui.pushButton.clicked.connect(lambda: self.startThread(str)) self.ui.pushButton_2.clicked.connect(self.stopThread) self.signal1.connect(self.updateview) def startThread(self, str): self.stopSign = 0 # 关闭线程关闭标识 print(str) text = self.ui.lineEdit.text() if (self.thread != null and self.thread.isAlive()): return # 不重复开启线程 self.thread = Thread(target=self.threadSend, args=(text,) # 元组 ) self.thread.start() def stopThread(self): # 停止子线程 # self.thread.join() #这个方法的意思是阻塞其他线程包括主线程,专注于运行子线程的任务,直至子线程完成 self.stopSign = 1 # 打开线程关闭标识 self.thread.join() def threadSend(self, text): # 开启子线程,不阻塞主线程 while (self.stopSign == 0): time.sleep(0.1) item = self.woman[random.randint(0, 18)] self.signal1.emit(item, text) # 子线程不直接更新页面,发送信号让主线程去更新 def updateview(self, item, text): # 更新页面执行 self.ui.lineEdit.setText(text + "+" + item) app = QApplication([]) stats = MainWindow() stats.show() app.exec_()
运行效果如下:
标签:Form,self,pyqt5,QtWidgets,ui,formLayoutWidget,多线程,pushButton 来源: https://blog.51cto.com/u_14814727/2964965