0

我正在尝试将旧的 Maya python 脚本转换为 Maya 2017。在 2017 年,他们进行了一些更改,包括从 PySide 切换到 PySide 2 以及从 Qt4 切换到 Qt5。我对这些库甚至 python 都没有经验。

我做的第一件事是尝试通过 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 现在是 QtWidgets 的一部分,包含在 PyQt5 中,而不是 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 知识的人都可以加入吗?

4

1 回答 1

0

简单的答案就是这个。QTWidgets不是QTGui。在 Pyside2 中,您不仅导入 shiboken2 和 wrapinstance2,而且所有好的东西都进入了QTWidgets.

def get_maya_window():

    ptr = apiUI.MQtUtil.mainWindow()
    if ptr is not None:
        return shiboken2.wrapInstance2(long(ptr), QtWidgets.QWidget)
于 2017-04-08T21:04:20.977 回答