PyQt MainWindow closes after initialization
Question: I wanted to start a new project using PyQt5 and QtDesigner. To start, I just
copied the code I had from previous projects in PyQt4 and tweaked it to the
changes in PyQt5. So, the code to start the Main Window and a Timer which
updates the application looks like this:

    
    
    # ====Python=============================================================
    #                               SticksNStones
    # =======================================================================
    import ...    
    
    FPS = 45
    dt = 1000.0 / FPS
    
    class SNSMainWindow(WindowBaseClass, Ui_Window):
        def __init__(self, parent=None):
            WindowBaseClass.__init__(self, parent)
            Ui_Window.__init__(self)
            self.setupUi(self)
    
            self.paused = False
            self.timer = None
    
            self.init()
    
        def init(self):    
            # Setup Display
            self.display.setup()
    
            # Setup timer
            self.timer = QtCore.QTimer(self)
            self.timer.timeout.connect(self.update_loop)
    
            self.timer.start(self.dt)
    
        def update_loop(self):
            if not self.paused:
                self.display.update(dt)
            else:
                pass
    
    
    # ==================================
    #           Start Application
    # ==================================
    _dialog = None
    
    
    def start_sns():
        global _dialog
    
        # Start App and frame
        app = QtWidgets.QApplication(sys.argv)
        _dialog = SNSMainWindow()
        _dialog.show()
    
        # Exit if window is closed
        sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        start_sns()
    

But as soon as I start the application, it closes after initialization.
Debugging showed that the timer is active, but the update_loop is never
called.

The PyQt4 Code from which I copied works just fine and I just can't get my
head around why this does not work, since all examples I found online have the
same code.

The question being: Why does the application close itself upon start?

## Update

The problem is not the timer, but the usage of a custom .ui. If I run the code
with

    
    
    class SNSMainWindow(QtWidgets.QFrame):
        def __init__(self, parent=None): 
            QtWidgets.QFrame.__init__(self, parent) 
            ...
    

a window opens and it stays open until I close it. But a barebone

    
    
    ui_path = os.path.dirname(os.path.abspath(__file__)) + "/ui/sns_main.ui"
    Ui_Window, WindowBaseClass = uic.loadUiType(ui_path)
    
    class SNSMainWindow(WindowBaseClass, Ui_Window):
        def __init__(self, parent=None):
            WindowBaseClass.__init__(self, parent)
            Ui_Window.__init__(self)
            self.setupUi(self)
    
    # ==================================    
    
    if __name__ == "__main__":
        # Start App and frame
        app = QtWidgets.QApplication(sys.argv)
        _dialog = SNSMainWindow()
        _dialog.show()
    
        # Exit if window is closed
        sys.exit(app.exec_())
    

just disappears within milliseconds after showing. Then again, using the
custom widget in PyQt4 stays open, too. I added the uic.load part, which
operates just fine. Am I missing something when converting to PyQt5?

## Solution

I found the solution of the problem in my custom display class. In case of a
paintEvent, the display would try to get a (yet) undefined property. But
instead of raising an exception that the property was not defined, the window
just closed.

Defining the property while initializing the widget solved the problem. This
just keeps me wondering, why no exceptions are raised in this case, since the
widget clearly tries to call some undefined properties. A simple

    
    
    AttributeError: 'NoneType' object has no attribute 'xxx'
    

would have been enough.


Answer: I'd try to change some lines, first try to change `app` definition to

    
    
    app = QtGui.QApplication(sys.argv)
    

Then remove `Ui_Window` init and set it to `self.ui = Ui_Window()`

    
    
    class SNSMainWindow(WindowBaseClass):
        def __init__(self, parent=None):
            WindowBaseClass.__init__(self, parent)
            self.ui = Ui_Window()
            self.ui.setupUi(self)
    
            self.paused = False
            self.timer = None
    
            self.init()
    

