Pergunta

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()
Foi útil?

Solução

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.

Outras dicas

self.showMaximized()

PyQt5 is a Python binding of the Qt toolkit. For QtWidgets there is full documentation.

https://doc.qt.io/qt-5/qwidget.html#showMaximized

For another show*() methods just take a look into documentation, it's complete, nice. And probably it's the most well-documented C++ framework in the world.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top