我試圖將舊的瑪雅python腳本轉換爲Maya 2017.在2017年,他們做了一些更改,包括從PySide切換到PySide 2和Qt4到Qt5。我對這些庫甚至Python都沒有經驗。將Pyside qt4腳本轉換爲pyside2 qt5:我的導入在哪裏失敗?
我做的第一件事就是嘗試通過pyqt4topyqt5運行它,但沒有檢測到必要的更改。
我相信這兩個版本的腳本的核心功能是一樣的,但是由於這些改變,GUI加載失敗。原來的劇本導入庫如下:
import shiboken
from PySide import QtGui
import maya.OpenMayaUI as apiUI
from cStringIO import StringIO
import pysideuic
import xml.etree.ElementTree as xml
def get_maya_window():
ptr = apiUI.MQtUtil.mainWindow()
if ptr is not None:
return shiboken.wrapInstance(long(ptr), QtGui.QMainWindow)
def load_ui_type(ui_file):
parsed = xml.parse(ui_file)
widget_class = parsed.find('widget').get('class')
form_class = parsed.find('class').text
with open(ui_file,'r') as f:
o = StringIO()
frame = {}
pysideuic.compileUi(f, o, indent = 0)
pyc = compile(o.getvalue(), '<string>', 'exec')
exec pyc in frame
# Fetch the base_class and form class based on their type in the xml from design
form_class = frame['Ui_{0}'.format(form_class)]
base_class = eval('QtGui.{0}'.format(widget_class))
return form_class, base_class
我改變PySide的所有實例PySide2,shiboken到shiboken2(在Maya 2017年的另一個變化),並pysideuic到pyside2uic。當測試腳本,我得到了錯誤
Error: line 1: AttributeError: file <string> line 1: 'module' object has no attribute 'QMainWindow' #
(1號線指的是在另一個腳本行:
from JUM.core.loadUIFile import get_maya_window, load_ui_type
調用該文件)
通過QT5文檔後一看,我確定QMainWindow現在是包含在PyQt5中的QtWidgets的一部分,而不是QtGui,所以我也替換了它。目前腳本代碼是
import shiboken2
from PyQt5 import QtWidgets
import maya.OpenMayaUI as apiUI
from cStringIO import StringIO
import pyside2uic
import xml.etree.ElementTree as xml
def get_maya_window():
ptr = apiUI.MQtUtil.mainWindow()
if ptr is not None:
return shiboken2.wrapInstance(long(ptr), QtWidgets.QMainWindow)
def load_ui_type(ui_file):
parsed = xml.parse(ui_file)
widget_class = parsed.find('widget').get('class')
form_class = parsed.find('class').text
with open(ui_file,'r') as f:
o = StringIO()
frame = {}
pyside2uic.compileUi(f, o, indent = 0)
pyc = compile(o.getvalue(), '<string>', 'exec')
exec pyc in frame
# Fetch the base_class and form class based on their type in the xml from design
form_class = frame['Ui_{0}'.format(form_class)]
base_class = eval('QtWidgets.{0}'.format(widget_class))
return form_class, base_class
但是我仍然得到完全相同的錯誤,所以我認爲我的模塊導入有問題。任何人都可以在Python中使用Qt5的知識嗎?
當然,您需要從PySide2導入,而不是PyQt5? – ekhumoro
@ekhumoro我也嘗試過,結果完全一樣。 – mlamp
您可以通過手動丟棄所有不需要的錯誤來顯示。 – Trilarion