PyQT Fullscreen Issue

18,570

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.

Share:
18,570
Jam3sn
Author by

Jam3sn

Updated on June 04, 2022

Comments

  • Jam3sn
    Jam3sn almost 2 years

    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()
    
    • hackyday
      hackyday about 10 years
      I was going to link the zetcode tutorial, when I noticed it contained the same bug you have!
    • DanielSank
      DanielSank about 10 years
      @hackyday: People make this mistake ALL the time.
    • DanielSank
      DanielSank about 10 years
      @Spook811: In general, remove EVERYTHING you don't absolutely need in your minimal working examples.