pyqt6,ui文件转py两个命令的区别

2024-01-30 16:42:21 浏览数 (1)

在pyqt6中ui文件转py文件有两个命令

第一个命令

代码语言:Python复制
pyuic6 -x input.ui -o output.py

第一个命令把ui文件转化成一个.py直接可执行文件

第二个命令

代码语言:Pyhton复制
pyuic6 input.ui -o output.py

第二个命令转化的是不可以直接执行,缺少main函数的py文件

对比测试

随便生成一个ui文件

用第一个命令转:

代码语言:python代码运行次数:0复制
# Form implementation generated from reading ui file '0001.ui'
#
# Created by: PyQt6 UI code generator 6.6.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(parent=Form)
        self.pushButton.setGeometry(QtCore.QRect(140, 120, 75, 23))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(Form)
        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)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec())

用第二个命令转:

代码语言:python代码运行次数:0复制
# Form implementation generated from reading ui file '0001.ui'
#
# Created by: PyQt6 UI code generator 6.6.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(parent=Form)
        self.pushButton.setGeometry(QtCore.QRect(140, 120, 75, 23))
        self.pushButton.setObjectName("pushButton")

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

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))

总结

可以看见很鲜明的对比,第一种直接运行即可,第二种还需要自己添加main函数才能运行,为了方便起见建议用第一个命令

0 人点赞