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

相关推荐
Data_agent2 小时前
学术爬虫实战:构建知网论文关键词共现网络的技术指南
python·算法
_一路向北_3 小时前
爬虫框架:Feapder使用心得
爬虫·python
皇族崛起4 小时前
【3D标注】- Unreal Engine 5.7 与 Python 交互基础
python·3d·ue5
你想知道什么?4 小时前
Python基础篇(上) 学习笔记
笔记·python·学习
Swizard5 小时前
速度与激情:Android Python + CameraX 零拷贝实时推理指南
android·python·ai·移动开发
一直跑5 小时前
Liunx服务器centos7离线升级内核(Liunx服务器centos7.9离线/升级系统内核)
python
leocoder5 小时前
大模型基础概念入门 + 代码实战(实现一个多轮会话机器人)
前端·人工智能·python
Buxxxxxx5 小时前
DAY 37 深入理解SHAP图
python
ada7_5 小时前
LeetCode(python)108.将有序数组转换为二叉搜索树
数据结构·python·算法·leetcode
请一直在路上5 小时前
python文件打包成exe(虚拟环境打包,减少体积)
开发语言·python