PyQt5 将.ui文件转为.py文件并用另一个.py调用

一、.ui转.py

1.使用终端进入ui文件所在的文件夹

值得注意的是,Anaconda Prompt不能直接cd进其它盘

需要在根目录下进行切换盘符操作,使用 cd ..切进上一级目录,直到进入根目录:

这样就可以了

2.输入以下命令:

pyuic5 -o UI.py UI.ui

代码说明

生成的py文件:

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

# Form implementation generated from reading ui file 'UI.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Gaze_Estimation(object):
    def setupUi(self, Gaze_Estimation):
        Gaze_Estimation.setObjectName("Gaze_Estimation")
        Gaze_Estimation.resize(1319, 862)
        self.verticalLayoutWidget = QtWidgets.QWidget(Gaze_Estimation)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 30, 391, 321))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout_setting = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout_setting.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_setting.setObjectName("verticalLayout_setting")
        self.horizontalLayout_predictor = QtWidgets.QHBoxLayout()
        self.horizontalLayout_predictor.setObjectName("horizontalLayout_predictor")
        self.label_predictor = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_predictor.setTextFormat(QtCore.Qt.AutoText)
        self.label_predictor.setObjectName("label_predictor")
        self.horizontalLayout_predictor.addWidget(self.label_predictor)
        self.lineEdit_predictor = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.lineEdit_predictor.setText("")
        self.lineEdit_predictor.setEchoMode(QtWidgets.QLineEdit.Normal)
        self.lineEdit_predictor.setObjectName("lineEdit_predictor")
        self.horizontalLayout_predictor.addWidget(self.lineEdit_predictor)
        self.pushButton_predictor = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.pushButton_predictor.setAutoDefault(False)
        self.pushButton_predictor.setDefault(False)
        self.pushButton_predictor.setObjectName("pushButton_predictor")
        self.horizontalLayout_predictor.addWidget(self.pushButton_predictor)
        self.verticalLayout_setting.addLayout(self.horizontalLayout_predictor)
        self.horizontalLayout_gaze_model = QtWidgets.QHBoxLayout()
        self.horizontalLayout_gaze_model.setObjectName("horizontalLayout_gaze_model")
        self.label_gaze_model = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_gaze_model.setTextFormat(QtCore.Qt.AutoText)
        self.label_gaze_model.setObjectName("label_gaze_model")
        self.horizontalLayout_gaze_model.addWidget(self.label_gaze_model)
        self.lineEdit_gaze_model = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.lineEdit_gaze_model.setText("")
        self.lineEdit_gaze_model.setEchoMode(QtWidgets.QLineEdit.Normal)
        self.lineEdit_gaze_model.setObjectName("lineEdit_gaze_model")
        self.horizontalLayout_gaze_model.addWidget(self.lineEdit_gaze_model)
        self.pushButton_gaze_model = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.pushButton_gaze_model.setAutoDefault(False)
        self.pushButton_gaze_model.setDefault(False)
        self.pushButton_gaze_model.setObjectName("pushButton_gaze_model")
        self.horizontalLayout_gaze_model.addWidget(self.pushButton_gaze_model)
        self.verticalLayout_setting.addLayout(self.horizontalLayout_gaze_model)
        self.horizontalLayout_pnet_model = QtWidgets.QHBoxLayout()
        self.horizontalLayout_pnet_model.setObjectName("horizontalLayout_pnet_model")
        self.label_pnet_model = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_pnet_model.setTextFormat(QtCore.Qt.AutoText)
        self.label_pnet_model.setObjectName("label_pnet_model")
        self.horizontalLayout_pnet_model.addWidget(self.label_pnet_model)
        self.lineEdit_pnet_model = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.lineEdit_pnet_model.setText("")
        self.lineEdit_pnet_model.setEchoMode(QtWidgets.QLineEdit.Normal)
        self.lineEdit_pnet_model.setObjectName("lineEdit_pnet_model")
        self.horizontalLayout_pnet_model.addWidget(self.lineEdit_pnet_model)
        self.pushButton_pnet_model = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.pushButton_pnet_model.setAutoDefault(False)
        self.pushButton_pnet_model.setDefault(False)
        self.pushButton_pnet_model.setObjectName("pushButton_pnet_model")
        self.horizontalLayout_pnet_model.addWidget(self.pushButton_pnet_model)
        self.verticalLayout_setting.addLayout(self.horizontalLayout_pnet_model)
        self.horizontalLayout_rnet_model = QtWidgets.QHBoxLayout()
        self.horizontalLayout_rnet_model.setObjectName("horizontalLayout_rnet_model")
        self.label_rnet_model = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_rnet_model.setTextFormat(QtCore.Qt.AutoText)
        self.label_rnet_model.setObjectName("label_rnet_model")
        self.horizontalLayout_rnet_model.addWidget(self.label_rnet_model)
        self.lineEdit_rnet_model = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.lineEdit_rnet_model.setText("")
        self.lineEdit_rnet_model.setEchoMode(QtWidgets.QLineEdit.Normal)
        self.lineEdit_rnet_model.setObjectName("lineEdit_rnet_model")
        self.horizontalLayout_rnet_model.addWidget(self.lineEdit_rnet_model)
        self.pushButton_rnet_model = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.pushButton_rnet_model.setAutoDefault(False)
        self.pushButton_rnet_model.setDefault(False)
        self.pushButton_rnet_model.setObjectName("pushButton_rnet_model")
        self.horizontalLayout_rnet_model.addWidget(self.pushButton_rnet_model)
        self.verticalLayout_setting.addLayout(self.horizontalLayout_rnet_model)
        self.horizontalLayout_onet_model = QtWidgets.QHBoxLayout()
        self.horizontalLayout_onet_model.setObjectName("horizontalLayout_onet_model")
        self.label_onet_mobel = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_onet_mobel.setTextFormat(QtCore.Qt.AutoText)
        self.label_onet_mobel.setObjectName("label_onet_mobel")
        self.horizontalLayout_onet_model.addWidget(self.label_onet_mobel)
        self.lineEdit_onet_model = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.lineEdit_onet_model.setText("")
        self.lineEdit_onet_model.setEchoMode(QtWidgets.QLineEdit.Normal)
        self.lineEdit_onet_model.setObjectName("lineEdit_onet_model")
        self.horizontalLayout_onet_model.addWidget(self.lineEdit_onet_model)
        self.pushButton_onet_model = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.pushButton_onet_model.setAutoDefault(False)
        self.pushButton_onet_model.setDefault(False)
        self.pushButton_onet_model.setObjectName("pushButton_onet_model")
        self.horizontalLayout_onet_model.addWidget(self.pushButton_onet_model)
        self.verticalLayout_setting.addLayout(self.horizontalLayout_onet_model)
        self.horizontalLayout_device = QtWidgets.QHBoxLayout()
        self.horizontalLayout_device.setObjectName("horizontalLayout_device")
        self.label_device = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_device.setTextFormat(QtCore.Qt.AutoText)
        self.label_device.setObjectName("label_device")
        self.horizontalLayout_device.addWidget(self.label_device)
        self.lineEdit_device = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.lineEdit_device.setText("")
        self.lineEdit_device.setEchoMode(QtWidgets.QLineEdit.Normal)
        self.lineEdit_device.setObjectName("lineEdit_device")
        self.horizontalLayout_device.addWidget(self.lineEdit_device)
        self.pushButton_device = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.pushButton_device.setAutoDefault(False)
        self.pushButton_device.setDefault(False)
        self.pushButton_device.setObjectName("pushButton_device")
        self.horizontalLayout_device.addWidget(self.pushButton_device)
        self.verticalLayout_setting.addLayout(self.horizontalLayout_device)

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

    def retranslateUi(self, Gaze_Estimation):
        _translate = QtCore.QCoreApplication.translate
        Gaze_Estimation.setWindowTitle(_translate("Gaze_Estimation", "Form"))
        self.label_predictor.setText(_translate("Gaze_Estimation", "<html><head/><body><p><span style=\" font-size:11pt;\">Predictor:</span></p></body></html>"))
        self.pushButton_predictor.setText(_translate("Gaze_Estimation", "选择文件"))
        self.label_gaze_model.setText(_translate("Gaze_Estimation", "<html><head/><body><p><span style=\" font-size:11pt;\">Gaze model:</span></p></body></html>"))
        self.pushButton_gaze_model.setText(_translate("Gaze_Estimation", "选择文件"))
        self.label_pnet_model.setText(_translate("Gaze_Estimation", "<html><head/><body><p><span style=\" font-size:11pt;\">Pnet model:</span></p></body></html>"))
        self.pushButton_pnet_model.setText(_translate("Gaze_Estimation", "选择文件"))
        self.label_rnet_model.setText(_translate("Gaze_Estimation", "<html><head/><body><p><span style=\" font-size:11pt;\">Rnet model:</span></p></body></html>"))
        self.pushButton_rnet_model.setText(_translate("Gaze_Estimation", "选择文件"))
        self.label_onet_mobel.setText(_translate("Gaze_Estimation", "<html><head/><body><p><span style=\" font-size:11pt;\">Onet model:</span></p></body></html>"))
        self.pushButton_onet_model.setText(_translate("Gaze_Estimation", "选择文件"))
        self.label_device.setText(_translate("Gaze_Estimation", "<html><head/><body><p><span style=\" font-size:11pt;\">Device:</span></p></body></html>"))
        self.pushButton_device.setText(_translate("Gaze_Estimation", "选择文件"))

二、用另一个py文件调用展示ui

import sys

from PyQt5.QtWidgets import *

from UI import Ui_Gaze_Estimation


class MyWindow(QWidget, Ui_Gaze_Estimation):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    w = MyWindow()
    # 展示窗口
    w.show()

    app.exec()

运行代码即可成功展示UI界面:

成功展示UI

三、参考

  1. Anaconda Prompt切换路径不能进入D盘_软件安装好难啊的博客-CSDN博客_为什么访问不了d盘 anaconda promote

  1. python下将.ui转为.py文件,并用另一个.py调用显示_郑琳潇的博客-CSDN博客_将ui文件转为py后怎么使用?

  1. PyQt将.ui文件转换为.py文件_跃然实验室的博客-CSDN博客_把ui文件转化为py文件

猜你喜欢

转载自blog.csdn.net/qq_54827663/article/details/128585136