使用 PySide2 開發 Maya 插件系列一:QT Designer 設計GUI, pyside-uic 把 .ui 文件轉為 .py 文件


使用 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

以下是生成的代碼:

 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))
test_ui_pyside.py
 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))
test_ui_pyside2.py

ui 文件轉換成 py 文件的好處是可讀性好,以后方便語言國際化,后續的章節會提到

回到總覽使用 PySide2 開發 Maya 插件系列 總覽


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM