just1
V2EX  ›  问与答

pyqt 问题,求助

  •  
  •   just1 · Aug 20, 2018 · 2294 views
    This topic created in 2854 days ago, the information mentioned may be changed or developed.

    环境: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

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

    3 replies    2018-08-20 19:24:10 +08:00
    just1
        1
    just1  
    OP
       Aug 20, 2018
    0 回复惨案...
    zone53
        2
    zone53  
       Aug 20, 2018 via iPhone   ❤️ 1
    出了_()这个函数,线程对象不就释放了么。
    just1
        3
    just1  
    OP
       Aug 20, 2018
    @zone53 #2 感谢,就是这个原因
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2835 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 04:30 · PVG 12:30 · LAX 21:30 · JFK 00:30
    ♥ Do have faith in what you're doing.