赞
踩
- import sys
- import time
- from PyQt5.QtCore import pyqtSignal, QObject, QThread
- from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton
-
-
- # 线程1
- class Thread1(QThread):
- signal = pyqtSignal(str)
-
- def __init__(self):
- super().__init__()
- self.running = False
-
- def run(self):
- self.running = True
- while self.running:
- current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- self.signal.emit(current_time)
- time.sleep(1)
-
- def stop(self):
- self.running = False
-
-
- # 线程2
- class Thread2(QThread):
- signal = pyqtSignal(str)
-
- def __init__(self):
- super().__init__()
- self.running = False
-
- def run(self):
- self.running = True
- count = 0
- while self.running:
- self.signal.emit(str(count))
- count += 1
- time.sleep(1)
-
- def stop(self):
- self.running = False
-
-
- # 线程3
- class Thread3(QThread):
- signal = pyqtSignal(str)
-
- def __init__(self):
- super().__init__()
- self.running = False
-
- def run(self):
- self.running = True
- while self.running:
- self.signal.emit('Thread 3 running')
- time.sleep(1)
-
- def stop(self):
- self.running = False
-
-
- # GUI界面
- class MainWindow(QWidget):
- def __init__(self):
- super().__init__()
-
- self.thread1 = Thread1()
- self.thread2 = Thread2()
- self.thread3 = Thread3()
-
- self.init_ui()
-
- def init_ui(self):
- # 线程1控制按钮
- self.thread1_start_button = QPushButton('启动线程1')
- self.thread1_stop_button = QPushButton('停止线程1')
- self.thread1_start_button.clicked.connect(self.thread1_start)
- self.thread1_stop_button.clicked.connect(self.thread1_stop)
-
- # 线程2控制按钮
- self.thread2_start_button = QPushButton('启动线程2')
- self.thread2_stop_button = QPushButton('停止线程2')
- self.thread2_start_button.clicked.connect(self.thread2_start)
- self.thread2_stop_button.clicked.connect(self.thread2_stop)
-
- # 线程3控制按钮
- self.thread3_start_button = QPushButton('启动线程3')
- self.thread3_stop_button = QPushButton('停止线程3')
- self.thread3_start_button.clicked.connect(self.thread3_start)
- self.thread3_stop_button.clicked.connect(self.thread3_stop)
-
- # 显示结果的标签
- self.thread1_label = QLabel('Thread 1:')
- self.thread1_display_label = QLabel('')
- self.thread2_label = QLabel('Thread 2:')
- self.thread2_display_label = QLabel('')
- self.thread3_label = QLabel('Thread 3:')
- self.thread3_display_label = QLabel('')
-
- layout1 = QHBoxLayout()
- layout1.addWidget(self.thread1_start_button)
- layout1.addWidget(self.thread1_stop_button)
- layout1.addWidget(self.thread1_label)
- layout1.addWidget(self.thread1_display_label)
-
- layout2 = QHBoxLayout()
- layout2.addWidget(self.thread2_start_button)
- layout2.addWidget(self.thread2_stop_button)
- layout2.addWidget(self.thread2_label)
- layout2.addWidget(self.thread2_display_label)
-
- layout3 = QHBoxLayout()
- layout3.addWidget(self.thread3_start_button)
- layout3.addWidget(self.thread3_stop_button)
- layout3.addWidget(self.thread3_label)
- layout3.addWidget(self.thread3_display_label)
-
- main_layout = QVBoxLayout()
- main_layout.addLayout(layout1)
- main_layout.addLayout(layout2)
- main_layout.addLayout(layout3)
-
- self.setLayout(main_layout)
- self.show()
-
- # 线程1控制方法
- def thread1_start(self):
- self.thread1.signal.connect(self.update_thread1_display)
- self.thread1.start()
-
- def thread1_stop(self):
- self.thread1.signal.disconnect()
- self.thread1.stop()
-
- # 线程2控制方法
- def thread2_start(self):
- self.thread2.signal.connect(self.update_thread2_display)
- self.thread2.start()
-
- def thread2_stop(self):
- self.thread2.signal.disconnect()
- self.thread2.stop()
-
- # 线程3控制方法
- def thread3_start(self):
- self.thread3.signal.connect(self.update_thread3_display)
- self.thread3.start()
-
- def thread3_stop(self):
- self.thread3.signal.disconnect()
- self.thread3.stop()
-
- # 更新显示的槽
- def update_thread1_display(self, text):
- self.thread1_display_label.setText(text)
-
- def update_thread2_display(self, text):
- self.thread2_display_label.setText(text)
-
- def update_thread3_display(self, text):
- self.thread3_display_label.setText(text)
-
-
- app = QApplication(sys.argv)
- window = MainWindow()
- sys.exit(app.exec())
在这个代码中,创建了三个继承自QThread的线程类Thread1、Thread2和Thread3,并分别实现了run()方法和stop()方法。run()方法是在其启动之后,执行的核心代码;stop()方法则用于停止线程。
在GUI界面中,创建了六个按钮,分别控制三个线程的启动与停止,并将其connect到相应的槽函数上。每个线程的启动与停止通过signal进行连接并实现。
同时,创建了MainWindow类,并在其中定义了三个线程的实例。在GUI界面创建完毕之后,当用户点击线程启动按钮时,对应的线程会被启动,并将该线程的信号(signal)连接到主窗口的槽(slot)函数,从而更新相应的线程结果展示。
最后,当用户点击线程停止按钮时,对应的线程会通过disconnect and stop()方法停止,并在主窗口的槽函数中进行反馈。
- import sys
- import time
- from PyQt5.QtCore import pyqtSignal, QObject, QThread
- from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton
-
-
- # 线程1
- class Thread1(QThread):
- signal = pyqtSignal(str)
-
- def __init__(self, text):
- super().__init__()
- self.text = text
- self.running = False
-
- def run(self):
- self.running = True
- while self.running:
- current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- self.signal.emit(f"{current_time} ({self.text})")
- time.sleep(1)
-
- def stop(self):
- self.running = False
-
-
- # 线程2
- class Thread2(QThread):
- signal = pyqtSignal(str)
-
- def __init__(self, text):
- super().__init__()
- self.text = text
- self.running = False
-
- def run(self):
- self.running = True
- count = 0
- while self.running:
- self.signal.emit(f"{str(count)} ({self.text})")
- count += 1
- time.sleep(1)
-
- def stop(self):
- self.running = False
-
-
- # 线程3
- class Thread3(QThread):
- signal = pyqtSignal(str)
-
- def __init__(self, text):
- super().__init__()
- self.text = text
- self.running = False
-
- def run(self):
- self.running = True
- while self.running:
- self.signal.emit(f"Thread 3 running ({self.text})")
- time.sleep(1)
-
- def stop(self):
- self.running = False
-
-
- # GUI界面
- class MainWindow(QWidget):
- def __init__(self):
- super().__init__()
-
- self.thread1 = None
- self.thread2 = None
- self.thread3 = None
-
- self.init_ui()
-
- def init_ui(self):
- # 输入参数的文本框
- self.text_edit = QLineEdit()
-
- # 线程1控制按钮
- self.thread1_start_button = QPushButton('启动线程1')
- self.thread1_stop_button = QPushButton('停止线程1')
- self.thread1_start_button.clicked.connect(self.thread1_start)
- self.thread1_stop_button.clicked.connect(self.thread1_stop)
-
- # 线程2控制按钮
- self.thread2_start_button = QPushButton('启动线程2')
- self.thread2_stop_button = QPushButton('停止线程2')
- self.thread2_start_button.clicked.connect(self.thread2_start)
- self.thread2_stop_button.clicked.connect(self.thread2_stop)
-
- # 线程3控制按钮
- self.thread3_start_button = QPushButton('启动线程3')
- self.thread3_stop_button = QPushButton('停止线程3')
- self.thread3_start_button.clicked.connect(self.thread3_start)
- self.thread3_stop_button.clicked.connect(self.thread3_stop)
-
- # 显示结果的标签
- self.thread1_label = QLabel('Thread 1:')
- self.thread1_display_label = QLabel('')
- self.thread2_label = QLabel('Thread 2:')
- self.thread2_display_label = QLabel('')
- self.thread3_label = QLabel('Thread 3:')
- self.thread3_display_label = QLabel('')
-
- layout1 = QHBoxLayout()
- layout1.addWidget(self.thread1_start_button)
- layout1.addWidget(self.thread1_stop_button)
- layout1.addWidget(self.thread1_label)
- layout1.addWidget(self.thread1_display_label)
-
- layout2 = QHBoxLayout()
- layout2.addWidget(self.thread2_start_button)
- layout2.addWidget(self.thread2_stop_button)
- layout2.addWidget(self.thread2_label)
- layout2.addWidget(self.thread2_display_label)
-
- layout3 = QHBoxLayout()
- layout3.addWidget(self.thread3_start_button)
- layout3.addWidget(self.thread3_stop_button)
- layout3.addWidget(self.thread3_label)
- layout3.addWidget(self.thread3_display_label)
-
- main_layout = QVBoxLayout()
- main_layout.addWidget(self.text_edit)
- main_layout.addLayout(layout1)
- main_layout.addLayout(layout2)
- main_layout.addLayout(layout3)
-
- self.setLayout(main_layout)
- self.show()
-
- # 线程1控制方法
- def thread1_start(self):
- if not self.thread1:
- self.thread1 = Thread1(self.text_edit.text())
- self.thread1.signal.connect(self.update_thread1_display)
- self.thread1.start()
-
- def thread1_stop(self):
- if self.thread1:
- self.thread1.signal.disconnect()
- self.thread1.stop()
- self.thread1 = None
-
- # 线程2控制方法
- def thread2_start(self):
- if not self.thread2:
- self.thread2 = Thread2(self.text_edit.text())
- self.thread2.signal.connect(self.update_thread2_display)
- self.thread2.start()
-
- def thread2_stop(self):
- if self.thread2:
- self.thread2.signal.disconnect()
- self.thread2.stop()
- self.thread2 = None
-
- # 线程3控制方法
- def thread3_start(self):
- if not self.thread3:
- self.thread3 = Thread3(self.text_edit.text())
- self.thread3.signal.connect(self.update_thread3_display)
- self.thread3.start()
-
- def thread3_stop(self):
- if self.thread3:
- self.thread3.signal.disconnect()
- self.thread3.stop()
- self.thread3 = None
-
- # 更新显示的槽
- def update_thread1_display(self, text):
- self.thread1_display_label.setText(text)
-
- def update_thread2_display(self, text):
- self.thread2_display_label.setText(text)
-
- def update_thread3_display(self, text):
- self.thread3_display_label.setText(text)
-
-
- app = QApplication(sys.argv)
- window = MainWindow()
- sys.exit(app.exec())
在这个代码中,我们在创建每个线程实例时,增加了一个参数text来接收lineEdit组件的文本输入。然后在对应的run()方法中,在信号(signal)发射的文本信息后,再加一段括号内容对该参数进行拼接。
GUI界面中,新增了一个QLineEdit组件用于用户输入文本参数,并将其传递给对应的线程实例。同时,为避免重复创建线程实例,我们在启动线程之前检查该线程实例是否已经存在。如果存在,则不再创建新的线程实例。而在停止线程时,我们需要将该线程实例置空。
最后,在更新显示的槽函数中,同样也需要在展示该线程的结果时,将lineedit组件的文本内容显示出来。
PS:以上代码为多线程示例代码,不足之处请引用后自行完善。文章引用请注明出处,谢谢~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。