Search code examples
pythonqtpyqtqt-designerpyuic

My pyuic converted file is not opening


I am currently a beginner in learning python and trying to make an application with Qt Designer. In all the tutorials I have followed after they convert the .ui file to .py using pyuic they just click on the new .py and it displays what they made in designer. After I convert using pyuic and click on the file nothing happens. I made a sample Qt project to try and understand what is going wrong, it is attached below.

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 't.ui'
#
# Created by: PyQt5 UI code generator 5.8.2 #
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(800, 600)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setGeometry(QtCore.QRect(180, 450, 68, 19))
    self.label.setObjectName("label")
    self.label_2 = QtWidgets.QLabel(self.centralwidget)
    self.label_2.setGeometry(QtCore.QRect(230, 400, 68, 19))
    self.label_2.setObjectName("label_2")
    self.label_3 = QtWidgets.QLabel(self.centralwidget)
    self.label_3.setGeometry(QtCore.QRect(540, 190, 68, 19))
    self.label_3.setObjectName("label_3")
    self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
    self.lineEdit.setGeometry(QtCore.QRect(500, 430, 171, 41))
    self.lineEdit.setObjectName("lineEdit")
    MainWindow.setCentralWidget(self.centralwidget)
    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"))
    self.label.setText(_translate("MainWindow", "TextLabel"))
    self.label_2.setText(_translate("MainWindow", "TextLabel"))
    self.label_3.setText(_translate("MainWindow", "TextLabel"))

Any help is greatly appreciated, Thanks!


Solution

  • Qt Designer is a tool that serves the interface design, pyuic converts the .ui (design) file to python code, in order to use it you must add it to a PyQt class, this depends on the design you made, in your case, the Class would be QMainWindow or a class that inherits from it. Some of the ways are:

    • Solution 1

    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(w)
        w.show()
        sys.exit(app.exec_())
    
    • Solution 2

    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent=parent)
            self.setupUi(self)
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec_())
    

    Tip: Add any of the solutions to your .py file.