【PyQt5篇】多线程


在这里插入图片描述

🍔使用QtDesigner进行设计

在这里插入图片描述

在这里插入图片描述
对应的代码btn.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>205</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QLineEdit" name="lineEdit">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>60</y>
     <width>231</width>
     <height>31</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>130</y>
     <width>93</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>按钮1—卡</string>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton_2">
   <property name="geometry">
    <rect>
     <x>230</x>
     <y>130</y>
     <width>93</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>按钮2—不卡</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

🛸实现多线程

代码如下

import sys

import time
from PyQt5 import uic
from PyQt5.QtCore import QThread
from PyQt5.QtWidgets import QApplication, QWidget


class MyThread(QThread):
    def __init__(self):
        super().__init__()

    def run(self):
        for i in range(5):
            print("是线程中执行...%d" % (i+1))
            time.sleep(1)


class MyWin(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.ui=uic.loadUi('./bun.ui',self)

        lineedit=self.ui.lineEdit
        btn1=self.ui.pushButton
        btn2=self.ui.pushButton_2

        # 绑定槽函数
        btn1.clicked.connect(self.click_1)
        btn2.clicked.connect(self.click_2)

    def click_1(self):
        for i in range(5):
            print("================= 卡的线程  =================中执行...%d"%(i+1))
            time.sleep(1)

    def click_2(self):
        self.my_thread=MyThread()
        self.my_thread.start()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    mywin = MyWin()
    mywin.ui.show()
    app.exec()

🌹效果

我们点击第一个按钮后,不能向文本框里面输入内容
直到循环结束
在这里插入图片描述
当我们按第二个按钮的时候,能够输入内容
在这里插入图片描述

🔎原因

一个线程是在主线程(UI线程)中运行的,当在主线程中执行耗时操作时(例如click_1方法中的for循环),它会阻塞主线程,导致界面无响应。

另一方面,使用QThread创建的线程会在后台运行,不会阻塞主线程。这是因为QThread类提供了一个事件循环,可以在后台处理线程的任务,而不会影响到线程的响应性。(这个属于自己创建的线程,不属于主线程

相关推荐

  1. QT基础(12)QT5线

    2024-04-07 05:40:06       27 阅读
  2. 【13】PyQt线&任务管理

    2024-04-07 05:40:06       32 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-07 05:40:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-07 05:40:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-07 05:40:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-07 05:40:06       18 阅读

热门阅读

  1. 在使用clickhouse的一些心得

    2024-04-07 05:40:06       21 阅读
  2. RobotFramework测试框架(6)测试用例文件结构

    2024-04-07 05:40:06       29 阅读
  3. 面对对象编程(四)

    2024-04-07 05:40:06       17 阅读
  4. leetcode 169.多数元素

    2024-04-07 05:40:06       45 阅读
  5. ROC与决策树介绍

    2024-04-07 05:40:06       22 阅读
  6. 在 HTML 中禁用 Chrome 浏览器的 Google 翻译功能

    2024-04-07 05:40:06       36 阅读
  7. MongoDB的简单使用

    2024-04-07 05:40:06       20 阅读
  8. leetcode 72.编辑距离

    2024-04-07 05:40:06       22 阅读
  9. 深入了解go的通道类型

    2024-04-07 05:40:06       16 阅读
  10. 外刊杂志经济学人获取方式

    2024-04-07 05:40:06       18 阅读
  11. golang mutex

    2024-04-07 05:40:06       18 阅读
  12. 【Rust】基础语法

    2024-04-07 05:40:06       20 阅读
  13. 设计模式:外观模式

    2024-04-07 05:40:06       17 阅读
  14. 机器学习软件perming的使用文档

    2024-04-07 05:40:06       14 阅读
  15. AJAX

    AJAX

    2024-04-07 05:40:06      14 阅读