无法从PyQt Designer中的代码构建GUI

Hit*_*aya 5 python pyqt qt-designer pyqt5

我从pyqt设计器制作了一个GUI文件,然后将其转换为.py文件。但是,当我在IDE中加载该.py代码(Pycharm和sublime文本)时,我尝试运行该文件,它运行时没有错误,但是物理没有加载GUI的方面,我尝试了来自互联网的自定义代码,效果很好,当我运行该代码时会显示GUI。我会提供比我目前正在使用的代码更简单的代码,因为从pyqt designer生成的所有代码似乎在物理方面对我来说根本不起作用。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(170, 200, 91, 30))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(30, 40, 113, 30))
        self.lineEdit.setObjectName("lineEdit")

        self.retranslateUi(Form)
        self.pushButton.clicked.connect(self.lineEdit.clear)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))
Run Code Online (Sandbox Code Playgroud)

eyl*_*esc 7

您有 2 个选择:

1.假设您使用了以下命令:

pyuic5 your_filename.ui -o your_filename.py
# or 
# pyuic5 your_filename.ui > your_filename.py
Run Code Online (Sandbox Code Playgroud)

该命令不会生成窗口对象或调用 show 方法因此不显示窗口,您必须使用选项-x

pyuic5 your_filename.ui -o your_filename.py -x
Run Code Online (Sandbox Code Playgroud)

2.添加调用对象的代码:

pyuic5 your_filename.ui -o your_filename.py
# or 
# pyuic5 your_filename.ui > your_filename.py
Run Code Online (Sandbox Code Playgroud)

  • @Hitsugaya 不要使用`>`,它会给你带来问题,而是使用`-o` (2认同)