0

I created a GUI with the hepl of QtCreator-->QtDesigner. The file is called mainwindow.ui. With the help of pyuic5 I created mainwindow.py

pyuic5 mainwindow.ui > mainwindow.py

and this is how it looks like:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(400, 300)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.frame = QtWidgets.QFrame(self.centralWidget)
        self.frame.setGeometry(QtCore.QRect(30, 20, 341, 191))
        self.frame.setFrameShape(QtWidgets.QFrame.Panel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.frame.setObjectName("frame")
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 23))
        self.menuBar.setObjectName("menuBar")
        MainWindow.setMenuBar(self.menuBar)
        self.mainToolBar = QtWidgets.QToolBar(MainWindow)
        self.mainToolBar.setObjectName("mainToolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtWidgets.QStatusBar(MainWindow)
        self.statusBar.setObjectName("statusBar")
        MainWindow.setStatusBar(self.statusBar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

and wanted to import that in my main script main.py:

#!/usr/bin/env python3

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from mainwindow import *
import sys


def main():
    app = QApplication(sys.argv)
    instance = Ui_MainWindow()
    #instance.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

...but nothing happens when I run it. How should my main.py look like to use that gui module?

Hrvoje T
  • 3,365
  • 4
  • 28
  • 41

1 Answers1

0

Ok, I figured it myself. To use a GUI file in Python, created in QtCreator called mainwindow.py, my main.py should look something like this:

#!/usr/bin/env python3

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from mainwindow import *
import sys

class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.show()


def main():
    app = QApplication(sys.argv)
    instance = Main()
    #instance.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

My class Main(QMainWindow) must have a parent QMainWindow and not QWidget because QWidget doesn't have setCentralWidget method which is needed in mainwindow.py.

Hrvoje T
  • 3,365
  • 4
  • 28
  • 41