简体   繁体   English

将Pyside qt4脚本转换为pyside2 qt5:我的导入在哪里失败?

[英]Converting a Pyside qt4 script to pyside2 qt5: where are my imports failing?

I'm trying to convert an old Maya python script to Maya 2017. In 2017 they made some changes including switching from PySide to PySide 2 and Qt4 to Qt5. 我正在尝试将旧的Maya python脚本转换为Maya2017。在2017年,他们进行了一些更改,包括从PySide切换到PySide 2,从Qt4切换到Qt5。 I have no experience with either of these libraries or even python. 我没有使用这些库甚至python的经验。

The first thing I did was try to run it through pyqt4topyqt5 with no necessary changes detected. 我做的第一件事是尝试通过pyqt4topyqt5运行它,没有检测到任何必要的更改。

I believe the core functionality of the script is the same in both version however the GUI loading is failing because of these changes. 我相信脚本的核心功能在两个版本中都是相同的,但是由于这些更改,导致GUI加载失败。 The original script importing the libraries is the following: 导入库的原始脚本如下:

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

I changed all instances of PySide to PySide2, shiboken to shiboken2 (another change in maya 2017), and pysideuic to pyside2uic. 我将PySide的所有实例更改为PySide2,将shiboken更改为shiboken2(在Maya 2017中进行了另一项更改),并将pysideuic更改为pyside2uic。 When testing the script, I got the error 测试脚本时,出现错误

 Error: line 1: AttributeError: file <string> line 1: 'module' object has no attribute 'QMainWindow' # 

(line 1 refers to the line in another script: (第1行是指另一个脚本中的行:

from JUM.core.loadUIFile import get_maya_window, load_ui_type

which calls this file) 调用此文件)

After a look through the Qt5 documentation, I determined that QMainWindow is now a part of QtWidgets, contained in PyQt5, rather than QtGui so I substituted that as well. 浏览Qt5文档后,我确定QMainWindow现在是PyQt5中包含的QtWidgets的一部分,而不是QtGui,因此我也替换了它。 Currently the script code is 当前脚本代码是

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

Yet I am still getting the exact same error, so I think something is wrong with my module importing. 但是我仍然遇到完全相同的错误,因此我认为模块导入有问题。 Can anyone with knowledge of Qt5 in python chime in? 谁能在python chime中了解Qt5的人?

simple answer would be this. 简单的答案就是这样。 QTWidgets not QTGui . QTWidgets不是QTGui In Pyside2 not only do you import shiboken2 and wrapinstance2 but everything good has gone into QTWidgets . 在Pyside2中,不仅可以导入shiboken2和wrapinstance2,而且所有好的东西都已经放入QTWidgets

def get_maya_window():

    ptr = apiUI.MQtUtil.mainWindow()
    if ptr is not None:
        return shiboken2.wrapInstance2(long(ptr), QtWidgets.QWidget)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM