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的更新互不影响,窗体不会卡死。

相关推荐
FreeCode9 分钟前
一文了解LangGraph智能体设计开发过程:Thinking in LangGraph
python·langchain·agent
西柚小萌新21 分钟前
【深入浅出PyTorch】--9.使用ONNX进行部署并推理
人工智能·pytorch·python
nvd1123 分钟前
SSE 流式输出与 Markdown 渲染实现详解
javascript·python
LDG_AGI23 分钟前
【推荐系统】深度学习训练框架(十):PyTorch Dataset—PyTorch数据基石
人工智能·pytorch·分布式·python·深度学习·机器学习
是Dream呀33 分钟前
昇腾实战|算子模板库Catlass与CANN生态适配
开发语言·人工智能·python·华为
培根芝士1 小时前
使用llm-compressor 对 Qwen3-14B 做 AWQ + INT4 量化
人工智能·python
拾贰_C1 小时前
【Python | Anaconda】 python-Anaconda 一些命令使用
开发语言·python
Aspect of twilight1 小时前
ACM输入输出格式详解
python·acm
见识星球1 小时前
名企校招攻略
大数据·python
TL滕1 小时前
从0开始学算法——第四天(题目参考答案)
数据结构·笔记·python·学习·算法