使用 PySide2 开发 Maya 插件系列一:QT Designer 设计GUI, pyside-uic 把 .ui 文件转为 .py 文件
前期准备:
安装 python:https://www.python.org/downloads/
安装 PySide2:安装 python 后,在安装目录下有 /script 文件夹,里面有 pip.exe ,cmd执行:pip install PySide,pip install PySide2(注意: python2.x 对应 PySide,python3.x 对应PySide2)
启动 QT Designer:
1.在 python 安装目录下搜寻 designer.exe,发送到桌面快捷键
2.或者在 Maya(2015以上版本) 安装目录下搜寻 designer.exe,发送到桌面快捷键
参考:Maya Max python PySide集成 shiboken版本对应关系
QT Designer 设计 GUI
1. 打开 designer,选择 Widget作为例子。注意:也可以选择其它 templete,Widget 和 Main Window 是有区别的,自己查找 QWidget 和 QMainWindow 的区别。
2. 随便添加一些控件,保存为 test.ui
pyside-uic 把 .ui 文件转为 .py 文件
如同 designer.exe, pyside-uic.exe,pyside2-uic.exe 也可以在 python 或者 Maya 的安装目录下找到
Tips:cmd路径 cd 为开发路径(.ui,.py文件所在的路径),把 pyside-uic.exe 拖到 cmd 窗口中即可。
cmd命令 :pyside-uic.exe -o test_ui_pyside.py test.ui (自己注意路径和命名)
查看 pyside-uic 的用法:pyside-uic.exe -h
下面是使用 maya2017 集成的 pyside2 来生成 test_ui_pyside.py
以下是生成的代码:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file '.\test.ui'
#
# Created: Tue Oct 30 08:03:48 2018
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost! from PySide import QtCore, QtGui class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(280, 274)
self.verticalLayout = QtGui.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.verticalLayout.addWidget(self.pushButton_2) self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
test_ui_pyside.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file '.\test.ui'
#
# Created: Thu Nov 01 18:39:09 2018
# by: pyside2-uic running on PySide2 2.0.0~alpha0
#
# WARNING! All changes made in this file will be lost! from PySide2 import QtCore, QtGui, QtWidgets class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(280, 274)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.verticalLayout.addWidget(self.pushButton_2) self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form):
Form.setWindowTitle(QtWidgets.QApplication.translate("Form", "Form", None, -1))
self.pushButton.setText(QtWidgets.QApplication.translate("Form", "PushButton", None, -1))
self.pushButton_2.setText(QtWidgets.QApplication.translate("Form", "PushButton", None, -1))
test_ui_pyside2.py
ui 文件转换成 py 文件的好处是可读性好,以后方便语言国际化,后续的章节会提到