无法从 PyQt Designer 的代码构建 GUI [英] Unable to build GUI from the code from PyQt Designer

查看:28
本文介绍了无法从 PyQt Designer 的代码构建 GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I made a GUI file from pyqt designer,which i converted to .py file.but when i load that .py code in my IDE (Pycharm and sublime text),and i try to run it,it runs without errors,but the physical aspect of GUI isn't loaded,i tried a custom code from the internet,which worked great,GUI shows up when i run that code. i will give simpler code than the one i'm currently working on,as it seems all of the code generated from pyqt designer doesn't work at all for me regarding its physical aspect.

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. 假设您使用了以下命令:

1. Assuming you have used the following command:

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

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

That command does not generate a window object or call the show method so the window is not displayed, you must use option -x:

pyuic5 your_filename.ui -o your_filename.py -x

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

2. Add the code that invokes an object:

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_())

这篇关于无法从 PyQt Designer 的代码构建 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆