使用 PySide2 開發 Maya 插件系列一:QT Designer 設計GUI, pyside-uic 把 .ui 文件轉爲 .py 文件
前期準備:html
安裝 python:https://www.python.org/downloads/python
安裝 PySide2:安裝 python 後,在安裝目錄下有 /script 文件夾,裏面有 pip.exe ,cmd執行:pip install PySide,pip install PySide2(注意: python2.x 對應 PySide,python3.x 對應PySide2)python3.x
啓動 QT Designer:ide
1.在 python 安裝目錄下搜尋 designer.exe,發送到桌面快捷鍵ui
2.或者在 Maya(2015以上版本) 安裝目錄下搜尋 designer.exe,發送到桌面快捷鍵this
參考:Maya Max python PySide集成 shiboken版本對應關係spa
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 的安裝目錄下找到code
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
如下是生成的代碼:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
1 # -*- coding: utf-8 -*- 2 3 # Form implementation generated from reading ui file '.\test.ui' 4 # 5 # Created: Tue Oct 30 08:03:48 2018 6 # by: pyside-uic 0.2.15 running on PySide 1.2.4 7 # 8 # WARNING! All changes made in this file will be lost! 9 10 from PySide import QtCore, QtGui 11 12 class Ui_Form(object): 13 def setupUi(self, Form): 14 Form.setObjectName("Form") 15 Form.resize(280, 274) 16 self.verticalLayout = QtGui.QVBoxLayout(Form) 17 self.verticalLayout.setObjectName("verticalLayout") 18 self.pushButton = QtGui.QPushButton(Form) 19 self.pushButton.setObjectName("pushButton") 20 self.verticalLayout.addWidget(self.pushButton) 21 self.pushButton_2 = QtGui.QPushButton(Form) 22 self.pushButton_2.setObjectName("pushButton_2") 23 self.verticalLayout.addWidget(self.pushButton_2) 24 25 self.retranslateUi(Form) 26 QtCore.QMetaObject.connectSlotsByName(Form) 27 28 def retranslateUi(self, Form): 29 Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) 30 self.pushButton.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8)) 31 self.pushButton_2.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
1 # -*- coding: utf-8 -*- 2 3 # Form implementation generated from reading ui file '.\test.ui' 4 # 5 # Created: Thu Nov 01 18:39:09 2018 6 # by: pyside2-uic running on PySide2 2.0.0~alpha0 7 # 8 # WARNING! All changes made in this file will be lost! 9 10 from PySide2 import QtCore, QtGui, QtWidgets 11 12 class Ui_Form(object): 13 def setupUi(self, Form): 14 Form.setObjectName("Form") 15 Form.resize(280, 274) 16 self.verticalLayout = QtWidgets.QVBoxLayout(Form) 17 self.verticalLayout.setObjectName("verticalLayout") 18 self.pushButton = QtWidgets.QPushButton(Form) 19 self.pushButton.setObjectName("pushButton") 20 self.verticalLayout.addWidget(self.pushButton) 21 self.pushButton_2 = QtWidgets.QPushButton(Form) 22 self.pushButton_2.setObjectName("pushButton_2") 23 self.verticalLayout.addWidget(self.pushButton_2) 24 25 self.retranslateUi(Form) 26 QtCore.QMetaObject.connectSlotsByName(Form) 27 28 def retranslateUi(self, Form): 29 Form.setWindowTitle(QtWidgets.QApplication.translate("Form", "Form", None, -1)) 30 self.pushButton.setText(QtWidgets.QApplication.translate("Form", "PushButton", None, -1)) 31 self.pushButton_2.setText(QtWidgets.QApplication.translate("Form", "PushButton", None, -1))
ui 文件轉換成 py 文件的好處是可讀性好,之後方便語言國際化,後續的章節會提到