编程语言
首页 > 编程语言> > python – 无法从PyQt Designer的代码构建GUI

python – 无法从PyQt Designer的代码构建GUI

作者:互联网

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

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"))

解决方法:

你有2个选择:

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

pyuic5 your_filename.ui -o your_filename.py
# or 
# pyuic5 your_filename.ui > your_filename.py

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

pyuic5 your_filename.ui -o your_filename.py -x

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

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"))

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(w)
    w.show()
    sys.exit(app.exec_())

标签:qt-designer,pyqt5,python,pyqt
来源: https://codeday.me/bug/20190929/1830947.html