PyQt window closes immediately after opening

前端 未结 2 1350
北恋
北恋 2020-12-10 07:41

I am getting an issue when trying to open a PyQt window.

The code below is an example of my original code. When I imported the module in import Test and

相关标签:
2条回答
  • 2020-12-10 07:54

    You need to keep a reference to the opened window, otherwise it goes out of scope and is garbage collected, which will destroy the underlying C++ object also. Try:

    def Start():
        m = myWindow()
        m.show()
        return m
    
    
    class myWindow():....
    
    if __name__ == "__main__":
        import sys
        app = QApplication(sys.argv)
        window = Start()
        app.exec_()
    
    0 讨论(0)
  • 2020-12-10 07:54

    You can also do:

    def Start():
        global m
        m = myWindow()
        m.show()
    
    class myWindow():....
    
    if __name__ == "__main__":
        import sys
        app = QApplication(sys.argv)
        window = Start()
        app.exec_()
    
    0 讨论(0)
提交回复
热议问题