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

相关推荐
m0_7335654612 分钟前
mysql数据库执行全量备份影响业务_利用xtrabackup实现无锁备份
jvm·数据库·python
2401_8800714017 分钟前
golang如何编写DNS查询工具_golang DNS查询工具编写大全
jvm·数据库·python
Vertira23 分钟前
python 配置PostgreSQL 数据库
开发语言·python
m0_5913647330 分钟前
JavaScript中Object-hasOwn作为现代安全检测方案
jvm·数据库·python
m0_6245785932 分钟前
html标签怎么避免标签嵌套错误_div不能放在p内原因【详解】
jvm·数据库·python
2301_769340671 小时前
怎样导出用于负载测试的样本数据_LIMIT限制数据量提取
jvm·数据库·python
2401_850491651 小时前
c++如何通过文件映射mmap在多进程间实现高性能数据共享【进阶】
jvm·数据库·python
iuvtsrt1 小时前
PHP 中高效查找 CSV 行并获取前后指定偏移行的数据
jvm·数据库·python
m0_463672201 小时前
MySQL从库出现大量锁等待怎么办_分析从库执行计划与锁日志
jvm·数据库·python
2301_809204701 小时前
为 Go 语言 WaitGroup.Wait() 添加超时机制的实用方案
jvm·数据库·python