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

相关推荐
lulu12165440781 分钟前
国内怎么用GPT5.5?基于weelinking零门槛合规接入GPT5.5全系列生产级能力
java·人工智能·python·gpt·ai编程
南宫萧幕4 分钟前
基于 DQN 与 Python-Simulink 联合仿真的 HEV 能量管理策略实战
开发语言·python·matlab·汽车·控制
iwS2o90XT7 分钟前
Java多线程编程:Thread与Runnable的并发控制
java·开发语言·python
tengyuxin8 分钟前
使用ComfyUI 制作图片
python
2301_7693406711 分钟前
SQL如何处理分组后的空值统计_善用COALESCE与聚合函数
jvm·数据库·python
2601_9561394212 分钟前
航空航天品牌策划公司哪家强
大数据·人工智能·python
2301_7820404514 分钟前
在 Go 语言中声明包级(全局)映射的正确方法
jvm·数据库·python
㳺三才人子14 分钟前
簡介 python 文字轉語音
开发语言·python
zh15702315 分钟前
如何在 macOS 中使用 launchd 每分钟执行一次 PHP 脚本
jvm·数据库·python
阿正呀15 分钟前
HTML怎么显示计量值_HTML meter标签应用场景【指南】
jvm·数据库·python