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

相关推荐
weixin_418813872 小时前
Python-可视化学习笔记
笔记·python·学习
Danceful_YJ2 小时前
4.权重衰减(weight decay)
python·深度学习·机器学习
Zonda要好好学习3 小时前
Python入门Day5
python
电商数据girl4 小时前
有哪些常用的自动化工具可以帮助处理电商API接口返回的异常数据?【知识分享】
大数据·分布式·爬虫·python·系统架构
CoooLuckly4 小时前
numpy数据分析知识总结
python·numpy
超龄超能程序猿4 小时前
(六)PS识别:源数据分析- 挖掘图像的 “元语言”技术实现
python·组合模式
amazinging5 小时前
北京-4年功能测试2年空窗-报培训班学测开-第四十四天
python·学习·appium
UrbanJazzerati5 小时前
Xlwings安装报错:Connection timed out & WinError 32?一招解决你的安装难题!
python
Tipriest_5 小时前
Python异常类型介绍
开发语言·python·异常
前端付豪5 小时前
21、用 Python + Pillow 实现「朋友圈海报图生成器」📸(图文合成 + 多模板 + 自动换行)
后端·python