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

相关推荐
2402_857583492 分钟前
深入探索:scikit-learn中递归特征消除(RFE)的奥秘
python·机器学习·scikit-learn
范范08256 分钟前
探索Sklearn:从数据预处理到模型评估
人工智能·python·sklearn
2301_7869643612 分钟前
Django文档简化版——Django快速入门——创建一个基本的投票应用程序(3)
数据库·python·django·sqlite·html
roo_112 分钟前
3. 排序算法代码-python
python·算法·排序算法
yukai0800823 分钟前
Python一些可能用的到的函数系列130 UCS-Time Brick
开发语言·python
AI Chen23 分钟前
python的类中的super是做什么的
python
孟郎郎24 分钟前
Python打开Excel文档并读取数据
python·excel·xlsx·openpyxl·xlrd·xls
许思王1 小时前
【Python】组合数据类型:序列,列表,元组,字典,集合
开发语言·人工智能·python
程序员的开发手册2 小时前
新手教学系列——慎用Flask-SQLAlchemy慢日志记录
数据库·python·flask·sqlalchemy
木觞清4 小时前
Django学习第三天
python·学习·django