【PyQt5篇】多线程

Dear 丶 2024-04-17 06:45 121阅读 0赞

文章目录

  • ?使用QtDesigner进行设计
  • ?实现多线程
    • ?效果
    • ?原因

在这里插入图片描述

?使用QtDesigner进行设计

在这里插入图片描述

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>Form</class>
  4. <widget class="QWidget" name="Form">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>400</width>
  10. <height>205</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>Form</string>
  15. </property>
  16. <widget class="QLineEdit" name="lineEdit">
  17. <property name="geometry">
  18. <rect>
  19. <x>90</x>
  20. <y>60</y>
  21. <width>231</width>
  22. <height>31</height>
  23. </rect>
  24. </property>
  25. </widget>
  26. <widget class="QPushButton" name="pushButton">
  27. <property name="geometry">
  28. <rect>
  29. <x>90</x>
  30. <y>130</y>
  31. <width>93</width>
  32. <height>28</height>
  33. </rect>
  34. </property>
  35. <property name="text">
  36. <string>按钮1—卡</string>
  37. </property>
  38. </widget>
  39. <widget class="QPushButton" name="pushButton_2">
  40. <property name="geometry">
  41. <rect>
  42. <x>230</x>
  43. <y>130</y>
  44. <width>93</width>
  45. <height>28</height>
  46. </rect>
  47. </property>
  48. <property name="text">
  49. <string>按钮2—不卡</string>
  50. </property>
  51. </widget>
  52. </widget>
  53. <resources/>
  54. <connections/>
  55. </ui>

?实现多线程

代码如下

  1. import sys
  2. import time
  3. from PyQt5 import uic
  4. from PyQt5.QtCore import QThread
  5. from PyQt5.QtWidgets import QApplication, QWidget
  6. class MyThread(QThread):
  7. def __init__(self):
  8. super().__init__()
  9. def run(self):
  10. for i in range(5):
  11. print("是线程中执行...%d" % (i+1))
  12. time.sleep(1)
  13. class MyWin(QWidget):
  14. def __init__(self):
  15. super().__init__()
  16. self.init_ui()
  17. def init_ui(self):
  18. self.ui=uic.loadUi('./bun.ui',self)
  19. lineedit=self.ui.lineEdit
  20. btn1=self.ui.pushButton
  21. btn2=self.ui.pushButton_2
  22. # 绑定槽函数
  23. btn1.clicked.connect(self.click_1)
  24. btn2.clicked.connect(self.click_2)
  25. def click_1(self):
  26. for i in range(5):
  27. print("================= 卡的线程 =================中执行...%d"%(i+1))
  28. time.sleep(1)
  29. def click_2(self):
  30. self.my_thread=MyThread()
  31. self.my_thread.start()
  32. if __name__ == '__main__':
  33. app = QApplication(sys.argv)
  34. mywin = MyWin()
  35. mywin.ui.show()
  36. app.exec()

?效果

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

?原因

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

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

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PyQt5线

    这是因为QThread类提供了一个事件循环,可以在后台处理线程的任务,而不会影响到线程的响应性。,当在主线程中执行耗时操作时(例如click_1方法中的for循环),它。...

    相关 interview5-线

    一、线程的基础知识 (1)线程与进程 程序由指令和数据组成,但这些指令要运行,数据要读写,就必须将指令加载至 CPU,数据加载至内存。在指令运行过程中还需要用到磁盘