python-3.x - PyQt5 窗口在终止线程时冻结
问题描述
在 pyqt5 中,当我尝试为我的程序关闭踏板时,Ui 冻结并停止响应。我是 pyqt5 的新手,无法找到答案。
这些是主窗口(GUI)文件中的方法
def clickButton_on(self):
print("clicked on/off")
ControlPanel.start()
def clickButton_Off(self):
print("clicked on/off")
ControlPanel.stop()
这是 controlPanel 类的方法
@classmethod
def start(cls):
print('start method fired')
cls.th = threading.Thread(target=cls.test,args=(cls.attr,))
cls.th.start()
@classmethod
def stop(cls):
print('stop method fired')
cls.th.join()
重新创建问题的完整代码
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.uic import loadUi
import sys
import threading
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow,self).__init__()
self.b1 = QtWidgets.QPushButton(self)
self.b1.setText("on")
self.b2 = QtWidgets.QPushButton(self)
self.b2.setText("off")
self.b2.move(50,50)
self.b1.clicked.connect(self.clickButton_on)
self.b2.clicked.connect(self.clickButton_Off)
def clickButton_on(self):
print("clicked on/off")
ControlPanel.start()
def clickButton_Off(self):
print("clicked on/off")
ControlPanel.stop()
class ControlPanel:
n=2
@classmethod
def start(cls):
print('start method fired')
cls.th = threading.Thread(target=cls.test,args=(cls.n,))
cls.th.start()
@classmethod
def stop(cls):
print('stop method fired')
cls.th.join()
@classmethod
def test(cls,n):
print('test method fired')
n= 0
while True:
n += 0.1
print(f'thread running n = {n}')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
窗户完全冻结。它不会使用 .join() 关闭线程,它线程会继续运行。
解决方案
推荐阅读
- python - 如何为 matplotlib 安装草书字体:找不到字体系列 ['cursive']
- javascript - 当搜索字符串在 typescript/javascript 中包含通配符时搜索数组?
- spring-boot - Spring Boot 应用程序未使用 application-test.properties 中定义的 DB 驱动程序
- javascript - 如何设置div的原点?
- javascript - socket.io 3.0 根据用户显示不同的房间视图/模板
- javascript - 在 JS 中切换...大小写
- python - 如何计算两个不同能力的玩家在 PARS 壁球比赛中获胜的概率?(Python)
- java - 根据作为参数传递的标志执行各种操作的设计模式
- reactjs - React Query useMutation 动态设置mutationKey
- javascript - 当 jQuery 添加子项时转换容器的大小