当前位置:   article > 正文

pyqt5 GUI应用程序多线程(thread)执行多个功能的示例代码_pythongui多线程

pythongui多线程

        下面是使用三个线程,分别由两个按钮控制它启动线程和关闭线程,即每个线程两个按钮,一共六个按钮控制三个线程的启动与关闭的代码实现:

  1. import sys
  2. import time
  3. from PyQt5.QtCore import pyqtSignal, QObject, QThread
  4. from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton
  5. # 线程1
  6. class Thread1(QThread):
  7. signal = pyqtSignal(str)
  8. def __init__(self):
  9. super().__init__()
  10. self.running = False
  11. def run(self):
  12. self.running = True
  13. while self.running:
  14. current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  15. self.signal.emit(current_time)
  16. time.sleep(1)
  17. def stop(self):
  18. self.running = False
  19. # 线程2
  20. class Thread2(QThread):
  21. signal = pyqtSignal(str)
  22. def __init__(self):
  23. super().__init__()
  24. self.running = False
  25. def run(self):
  26. self.running = True
  27. count = 0
  28. while self.running:
  29. self.signal.emit(str(count))
  30. count += 1
  31. time.sleep(1)
  32. def stop(self):
  33. self.running = False
  34. # 线程3
  35. class Thread3(QThread):
  36. signal = pyqtSignal(str)
  37. def __init__(self):
  38. super().__init__()
  39. self.running = False
  40. def run(self):
  41. self.running = True
  42. while self.running:
  43. self.signal.emit('Thread 3 running')
  44. time.sleep(1)
  45. def stop(self):
  46. self.running = False
  47. # GUI界面
  48. class MainWindow(QWidget):
  49. def __init__(self):
  50. super().__init__()
  51. self.thread1 = Thread1()
  52. self.thread2 = Thread2()
  53. self.thread3 = Thread3()
  54. self.init_ui()
  55. def init_ui(self):
  56. # 线程1控制按钮
  57. self.thread1_start_button = QPushButton('启动线程1')
  58. self.thread1_stop_button = QPushButton('停止线程1')
  59. self.thread1_start_button.clicked.connect(self.thread1_start)
  60. self.thread1_stop_button.clicked.connect(self.thread1_stop)
  61. # 线程2控制按钮
  62. self.thread2_start_button = QPushButton('启动线程2')
  63. self.thread2_stop_button = QPushButton('停止线程2')
  64. self.thread2_start_button.clicked.connect(self.thread2_start)
  65. self.thread2_stop_button.clicked.connect(self.thread2_stop)
  66. # 线程3控制按钮
  67. self.thread3_start_button = QPushButton('启动线程3')
  68. self.thread3_stop_button = QPushButton('停止线程3')
  69. self.thread3_start_button.clicked.connect(self.thread3_start)
  70. self.thread3_stop_button.clicked.connect(self.thread3_stop)
  71. # 显示结果的标签
  72. self.thread1_label = QLabel('Thread 1:')
  73. self.thread1_display_label = QLabel('')
  74. self.thread2_label = QLabel('Thread 2:')
  75. self.thread2_display_label = QLabel('')
  76. self.thread3_label = QLabel('Thread 3:')
  77. self.thread3_display_label = QLabel('')
  78. layout1 = QHBoxLayout()
  79. layout1.addWidget(self.thread1_start_button)
  80. layout1.addWidget(self.thread1_stop_button)
  81. layout1.addWidget(self.thread1_label)
  82. layout1.addWidget(self.thread1_display_label)
  83. layout2 = QHBoxLayout()
  84. layout2.addWidget(self.thread2_start_button)
  85. layout2.addWidget(self.thread2_stop_button)
  86. layout2.addWidget(self.thread2_label)
  87. layout2.addWidget(self.thread2_display_label)
  88. layout3 = QHBoxLayout()
  89. layout3.addWidget(self.thread3_start_button)
  90. layout3.addWidget(self.thread3_stop_button)
  91. layout3.addWidget(self.thread3_label)
  92. layout3.addWidget(self.thread3_display_label)
  93. main_layout = QVBoxLayout()
  94. main_layout.addLayout(layout1)
  95. main_layout.addLayout(layout2)
  96. main_layout.addLayout(layout3)
  97. self.setLayout(main_layout)
  98. self.show()
  99. # 线程1控制方法
  100. def thread1_start(self):
  101. self.thread1.signal.connect(self.update_thread1_display)
  102. self.thread1.start()
  103. def thread1_stop(self):
  104. self.thread1.signal.disconnect()
  105. self.thread1.stop()
  106. # 线程2控制方法
  107. def thread2_start(self):
  108. self.thread2.signal.connect(self.update_thread2_display)
  109. self.thread2.start()
  110. def thread2_stop(self):
  111. self.thread2.signal.disconnect()
  112. self.thread2.stop()
  113. # 线程3控制方法
  114. def thread3_start(self):
  115. self.thread3.signal.connect(self.update_thread3_display)
  116. self.thread3.start()
  117. def thread3_stop(self):
  118. self.thread3.signal.disconnect()
  119. self.thread3.stop()
  120. # 更新显示的槽
  121. def update_thread1_display(self, text):
  122. self.thread1_display_label.setText(text)
  123. def update_thread2_display(self, text):
  124. self.thread2_display_label.setText(text)
  125. def update_thread3_display(self, text):
  126. self.thread3_display_label.setText(text)
  127. app = QApplication(sys.argv)
  128. window = MainWindow()
  129. sys.exit(app.exec())

        在这个代码中,创建了三个继承自QThread的线程类Thread1、Thread2和Thread3,并分别实现了run()方法和stop()方法。run()方法是在其启动之后,执行的核心代码;stop()方法则用于停止线程。

        在GUI界面中,创建了六个按钮,分别控制三个线程的启动与停止,并将其connect到相应的槽函数上。每个线程的启动与停止通过signal进行连接并实现。

        同时,创建了MainWindow类,并在其中定义了三个线程的实例。在GUI界面创建完毕之后,当用户点击线程启动按钮时,对应的线程会被启动,并将该线程的信号(signal)连接到主窗口的槽(slot)函数,从而更新相应的线程结果展示。

        最后,当用户点击线程停止按钮时,对应的线程会通过disconnect and stop()方法停止,并在主窗口的槽函数中进行反馈。

以上三个线程都没有传递参数,现在将每个线程都传递一个lineedit组件的text参数进去:

  1. import sys
  2. import time
  3. from PyQt5.QtCore import pyqtSignal, QObject, QThread
  4. from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton
  5. # 线程1
  6. class Thread1(QThread):
  7. signal = pyqtSignal(str)
  8. def __init__(self, text):
  9. super().__init__()
  10. self.text = text
  11. self.running = False
  12. def run(self):
  13. self.running = True
  14. while self.running:
  15. current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  16. self.signal.emit(f"{current_time} ({self.text})")
  17. time.sleep(1)
  18. def stop(self):
  19. self.running = False
  20. # 线程2
  21. class Thread2(QThread):
  22. signal = pyqtSignal(str)
  23. def __init__(self, text):
  24. super().__init__()
  25. self.text = text
  26. self.running = False
  27. def run(self):
  28. self.running = True
  29. count = 0
  30. while self.running:
  31. self.signal.emit(f"{str(count)} ({self.text})")
  32. count += 1
  33. time.sleep(1)
  34. def stop(self):
  35. self.running = False
  36. # 线程3
  37. class Thread3(QThread):
  38. signal = pyqtSignal(str)
  39. def __init__(self, text):
  40. super().__init__()
  41. self.text = text
  42. self.running = False
  43. def run(self):
  44. self.running = True
  45. while self.running:
  46. self.signal.emit(f"Thread 3 running ({self.text})")
  47. time.sleep(1)
  48. def stop(self):
  49. self.running = False
  50. # GUI界面
  51. class MainWindow(QWidget):
  52. def __init__(self):
  53. super().__init__()
  54. self.thread1 = None
  55. self.thread2 = None
  56. self.thread3 = None
  57. self.init_ui()
  58. def init_ui(self):
  59. # 输入参数的文本框
  60. self.text_edit = QLineEdit()
  61. # 线程1控制按钮
  62. self.thread1_start_button = QPushButton('启动线程1')
  63. self.thread1_stop_button = QPushButton('停止线程1')
  64. self.thread1_start_button.clicked.connect(self.thread1_start)
  65. self.thread1_stop_button.clicked.connect(self.thread1_stop)
  66. # 线程2控制按钮
  67. self.thread2_start_button = QPushButton('启动线程2')
  68. self.thread2_stop_button = QPushButton('停止线程2')
  69. self.thread2_start_button.clicked.connect(self.thread2_start)
  70. self.thread2_stop_button.clicked.connect(self.thread2_stop)
  71. # 线程3控制按钮
  72. self.thread3_start_button = QPushButton('启动线程3')
  73. self.thread3_stop_button = QPushButton('停止线程3')
  74. self.thread3_start_button.clicked.connect(self.thread3_start)
  75. self.thread3_stop_button.clicked.connect(self.thread3_stop)
  76. # 显示结果的标签
  77. self.thread1_label = QLabel('Thread 1:')
  78. self.thread1_display_label = QLabel('')
  79. self.thread2_label = QLabel('Thread 2:')
  80. self.thread2_display_label = QLabel('')
  81. self.thread3_label = QLabel('Thread 3:')
  82. self.thread3_display_label = QLabel('')
  83. layout1 = QHBoxLayout()
  84. layout1.addWidget(self.thread1_start_button)
  85. layout1.addWidget(self.thread1_stop_button)
  86. layout1.addWidget(self.thread1_label)
  87. layout1.addWidget(self.thread1_display_label)
  88. layout2 = QHBoxLayout()
  89. layout2.addWidget(self.thread2_start_button)
  90. layout2.addWidget(self.thread2_stop_button)
  91. layout2.addWidget(self.thread2_label)
  92. layout2.addWidget(self.thread2_display_label)
  93. layout3 = QHBoxLayout()
  94. layout3.addWidget(self.thread3_start_button)
  95. layout3.addWidget(self.thread3_stop_button)
  96. layout3.addWidget(self.thread3_label)
  97. layout3.addWidget(self.thread3_display_label)
  98. main_layout = QVBoxLayout()
  99. main_layout.addWidget(self.text_edit)
  100. main_layout.addLayout(layout1)
  101. main_layout.addLayout(layout2)
  102. main_layout.addLayout(layout3)
  103. self.setLayout(main_layout)
  104. self.show()
  105. # 线程1控制方法
  106. def thread1_start(self):
  107. if not self.thread1:
  108. self.thread1 = Thread1(self.text_edit.text())
  109. self.thread1.signal.connect(self.update_thread1_display)
  110. self.thread1.start()
  111. def thread1_stop(self):
  112. if self.thread1:
  113. self.thread1.signal.disconnect()
  114. self.thread1.stop()
  115. self.thread1 = None
  116. # 线程2控制方法
  117. def thread2_start(self):
  118. if not self.thread2:
  119. self.thread2 = Thread2(self.text_edit.text())
  120. self.thread2.signal.connect(self.update_thread2_display)
  121. self.thread2.start()
  122. def thread2_stop(self):
  123. if self.thread2:
  124. self.thread2.signal.disconnect()
  125. self.thread2.stop()
  126. self.thread2 = None
  127. # 线程3控制方法
  128. def thread3_start(self):
  129. if not self.thread3:
  130. self.thread3 = Thread3(self.text_edit.text())
  131. self.thread3.signal.connect(self.update_thread3_display)
  132. self.thread3.start()
  133. def thread3_stop(self):
  134. if self.thread3:
  135. self.thread3.signal.disconnect()
  136. self.thread3.stop()
  137. self.thread3 = None
  138. # 更新显示的槽
  139. def update_thread1_display(self, text):
  140. self.thread1_display_label.setText(text)
  141. def update_thread2_display(self, text):
  142. self.thread2_display_label.setText(text)
  143. def update_thread3_display(self, text):
  144. self.thread3_display_label.setText(text)
  145. app = QApplication(sys.argv)
  146. window = MainWindow()
  147. sys.exit(app.exec())

在这个代码中,我们在创建每个线程实例时,增加了一个参数text来接收lineEdit组件的文本输入。然后在对应的run()方法中,在信号(signal)发射的文本信息后,再加一段括号内容对该参数进行拼接。

GUI界面中,新增了一个QLineEdit组件用于用户输入文本参数,并将其传递给对应的线程实例。同时,为避免重复创建线程实例,我们在启动线程之前检查该线程实例是否已经存在。如果存在,则不再创建新的线程实例。而在停止线程时,我们需要将该线程实例置空。

最后,在更新显示的槽函数中,同样也需要在展示该线程的结果时,将lineedit组件的文本内容显示出来。

PS:以上代码为多线程示例代码,不足之处请引用后自行完善。文章引用请注明出处,谢谢~

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号