PyQt5+Pycharm, QThread

一、创建窗体控件

untitled.ui:

PushButton, PushButton_2, PushButton_3

lineEdit, lineEdit_2, lineEdit_3

二、编译窗体

自动生成untitled.py

三、新建py文件

myDemo.py

python 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from untitled import *
from PyQt5.QtCore import pyqtSlot, QThread, pyqtSignal
import time

class longTimeThread(QThread):
    number_singal = pyqtSignal(int, object)
    def __init__(self, object):
        super().__init__()
        self.object = object
    def run(self):
        for i in range(1000):
            self.number_singal.emit(i, self.object)
            time.sleep(0.1)

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.thread1 = longTimeThread(self.lineEdit_2)  # 创建一个线程
        self.thread2 = longTimeThread(self.lineEdit_3)  # 创建一个线程
        self.thread1.number_singal.connect(self.updates)
        self.thread2.number_singal.connect(self.updates)

    @pyqtSlot()
    def on_pushButton_clicked(self):
        self.lineEdit.setText('Hello World')

    @pyqtSlot()
    def on_pushButton_2_clicked(self):
        self.thread1.start()

    @pyqtSlot()
    def on_pushButton_3_clicked(self):
        self.thread2.start()

    def updates(self, number, object):
        object.setText(str(number))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = MainWindow()
    ui.show()
    sys.exit(app.exec_())

四、运行

点击PushButton_2, 更新lineEdit_2, 每0.1s加1

点击PushButton_3, 更新lineEdit_3, 每0.1s加1

lineEdit_2和lineEdit_3的更新互不影响,窗体不会卡死。

相关推荐
言之。13 分钟前
Andrej Karpathy 演讲【PyTorch at Tesla】
人工智能·pytorch·python
赵谨言21 分钟前
基于Python楼王争霸劳动竞赛数据处理分析
大数据·开发语言·经验分享·python
智启七月1 小时前
谷歌 Gemini 3.0 正式发布:一键生成 Web OS,编程能力碾压竞品
人工智能·python
2401_841495641 小时前
【强化学习】动态规划算法
人工智能·python·算法·动态规划·强化学习·策略迭代·价值迭代
测试19981 小时前
自动化测试报告生成(Allure)
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
DKunYu2 小时前
PyTorch入门
人工智能·pytorch·python·深度学习
ZhengEnCi2 小时前
Python_哈希表完全指南-从字典到高效查找的 Python 编程利器
python
今天没ID2 小时前
Python 集合类型全解析:从网球赛事案例看透字符串、列表、元组、集合与字典(1)
python
小宁爱Python2 小时前
从零搭建 RAG 智能问答系统 4:从多模态架构到 Milvus 向量存储实践
python·milvus
林炳然2 小时前
Python-Basic Day-2 python容器(列表、元组)
python