pyqt 问题,求助

2018-08-20 17:53:45 +08:00
 just1

环境:windows Python 3.6.5 非常简单的一个 qt 应用,代码如下

from PyQt5 import QtCore
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(985, 635)

class MyWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.setupUi(self)
    def test(self,b):
        print(b)

app = QApplication(sys.argv)
myWin = MyWindow()


class MyThead(QtCore.QThread):
    updated = QtCore.pyqtSignal(str)

    def __init__(self, msg):
        super().__init__()
        self.msg = msg

    def run(self):
        self.updated.emit(self.msg)




#--------------后半部分
_thread = MyThead('123')
_thread.updated.connect(myWin.test)
_thread.start()
myWin.show()
app.exec()

运行起来没问题

但是如果把后面几行

_thread = MyThead('123')
_thread.updated.connect(myWin.test)
_thread.start()

放到方法里面,也就是代码后半部分变成

def _():
    _thread = MyThead('123')
    _thread.updated.connect(myWin.test)
    _thread.start()
_()
myWin.show()
app.exec()

gui 直接崩溃,也没有报错,只有一个因异常导致的 QThread: Destroyed while thread is still running

这里折腾一天了才排查到这个地方,文档也没找到相关内容...

1627 次点击
所在节点    问与答
3 条回复
just1
2018-08-20 18:24:26 +08:00
0 回复惨案...
zone53
2018-08-20 18:36:12 +08:00
出了_()这个函数,线程对象不就释放了么。
just1
2018-08-20 19:24:10 +08:00
@zone53 #2 感谢,就是这个原因

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/481518

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX