from PyQt5.Qt import (QApplication, QWidget, QPushButton,QThread,QMutex,pyqtSignal)import sysimport time qmut_1 = QMutex() # 创建线程锁qmut_2 = QMutex()qmut_3 = QMutex()# 继承QThreadclass Thread_1(QThread): # 线程1 def __init__(self): super().__init__() def run(self): qmut_1.lock() # 加锁 values = [1, 2, 3, 4, 5,6,7,8,9,10] print("==== Thread_1 ====") for i in values: print("Thread_1:",i) time.sleep(0.5) # 休眠 qmut_1.unlock() # 解锁 class Thread_2(QThread): # 线程2 _signal =pyqtSignal() def __init__(self): super().__init__() def run(self): # qmut_2.lock() # 加锁 values = ["a", "b", "c", "d", "e","f","g","h","i","j","k"] print("==== Thread_2 ====") for i in values: print("Thread_2:",i) time.sleep(0.5) # qmut_2.unlock() # 解锁 self._signal.emit() class Thread_3(QThread): # 线程2 _signal =pyqtSignal() def __init__(self): super().__init__() def run(self): qmut_3.lock() # 加锁 values = ["a", "b", "c", "d", "e","f","g","h","i","j","k"] print("==== Thread_3 ====") for i in values: print("Thread_3:",i) time.sleep(0.5) qmut_3.unlock() # 解锁 self._signal.emit() #执行完毕后,释放信号 class Thread_01(QThread): # 线程1 def __init__(self): super().__init__() def run(self): values = [1, 2, 3, 4, 5] print("==== Thread_01 ====") for i in values: print("Thread_01:",i) time.sleep(0.5) # 休眠 class Thread_02(QThread): # 线程2 def __init__(self): super().__init__() def run(self): values = ["a", "b", "c", "d", "e"] print("==== Thread_02 ====") for i in values: print("Thread_02:",i) time.sleep(0.5) class MyWin(QWidget): def __init__(self): super().__init__() # 按钮初始化 self.btn_01 = QPushButton('按钮_每点一次运行一次', self) self.btn_01.move(80, 40) self.btn_01.clicked.connect(self.click_01) # 绑定槽函数 self.btn_02 = QPushButton('按钮_每点一次运行一次', self) self.btn_02.move(80, 80) self.btn_02.clicked.connect(self.click_02) # 绑定槽函数 self.btn_1 = QPushButton('按钮_线程锁_多次点击,依次执行(滞后感)', self) self.btn_1.move(80, 120) self.btn_1.clicked.connect(self.click_1) # 绑定槽函数 self.btn_2 = QPushButton('按钮_线程锁_收到完成信号后才能再次点击', self) self.btn_2.move(80, 160) self.btn_2.clicked.connect(self.click_2) # 绑定槽函数 self.btn_3 = QPushButton('按钮_线程锁_收到完成信号后再次执行', self) self.btn_3.move(80, 200) self.btn_3.clicked.connect(self.click_3) # 绑定槽函数 def click_01(self): self.thread_01 = Thread_01() # 创建线程 self.thread_01.start() # 开始线程 def click_02(self): self.thread_02 = Thread_02() # 创建线程 self.thread_02.start() # 开始线程 def click_1(self): self.thread_1 = Thread_1() # 创建线程 self.thread_1.start() # 开始线程 def click_2(self): self.btn_2.setEnabled(False) self.thread_2 = Thread_2() self.thread_2._signal.connect(self.set_btn_2) #信号连接,如果收到信号,就执行对应的函数 self.thread_2.start() def click_3(self): self.btn_3.setEnabled(False) self.thread_3 = Thread_3() self.thread_3._signal.connect(self.set_btn_3) #信号连接,如果收到信号,就执行对应的函数 self.thread_3.start() def set_btn_2(self): self.btn_2.setEnabled(True) def set_btn_3(self): self.btn_3.setEnabled(True) if __name__ == "__main__": app = QApplication(sys.argv) myshow = MyWin() myshow.setWindowTitle("多线程演示") myshow.setMinimumHeight(500) myshow.setMinimumWidth(500) myshow.show() sys.exit(app.exec_())