Search code examples
pythonpython-3.xpyqt4

PyQT Fullscreen Issue


Ok, so still fairly new to python, ans just started to use PyQT on my Pi to make a GUI for some code I have. However, the window opens for a split second and closes to a small window. Can anyone tell me where i'm going wrong?

import sys
from PyQt4 import QtGui, QtCore

class mainUI(QtGui.QWidget):
        def __init__(self):
                super(mainUI, self).__init__()
                self.initUI()

        def initUI(self):

                MainWindow = QtGui.QWidget()
                MainWindow.showFullScreen()
                MainWindow.setWindowTitle('TimeBot')
                MainWindow.show()

                qbtn = QtGui.QPushButton('Quit')
                qbtn.clicked.connect(QtCore.QCoreApplication.quit)
                qbtn.move(5,5)
                qbtn.show()

                self.show()            

def main():
        app = QtGui.QApplication(sys.argv)

        window = mainUI()

        sys.exit(app.exec_())

if __name__ == '__main__':
        main()

Solution

  • The problem is that inside initUi you make another QWidget, set it to full screen, show it, and then when that widget goes out of scope it gets garbage collected and disappears. You meant to use self instead of making a new QWidget. Like this:

    import sys
    from PyQt4 import QtGui, QtCore
    
    class mainUI(QtGui.QWidget):
        def __init__(self):
            super(mainUI, self).__init__()
            self.initUI()
    
        def initUI(self):
    
            self.showFullScreen()
            qbtn = QtGui.QPushButton('Quit')
            qbtn.clicked.connect(QtCore.QCoreApplication.quit)
            qbtn.move(5,5)
            self.button = qbtn
            qbtn.show()
    
    
    def main():
        app = QtGui.QApplication(sys.argv)
        window = mainUI()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    

    Note that I keep a reference to qbtn so that it doesn't get garbage collected and disappear.