我无法使用PySide加载和识别按钮

时间:2015-10-16 17:57:39

标签: python pyside maya

这是我得到的错误,我真的很困惑。我正在加载的UI文件具有此按钮名称并且匹配。但由于某种原因,它似乎无法识别和加载它。我只是尝试将此代码转换为PySide(它最初是PyQt)。我在翻译时做错了吗?

错误:AttributeError:文件第25行:' swapRefGUI'对象没有属性' swapRefBtn' #

from PySide import QtCore, QtGui, QtUiTools
import maya.cmds as cmds

class swapRefGUI(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)

        loader = QtUiTools.QUiLoader()
        uifile = QtCore.QFile('C:\Scripts\swapRef.ui')
        uifile.open(QtCore.QFile.ReadOnly)
        ui = loader.load(uifile, parent)
        uifile.close()

        self.setFixedSize(400, 300)

        self.swapRefBtn.clicked.connect(self.swapRefBtn_clicked)
        self.closeBtn.clicked.connect(self.close)               

    def swapRefBtn_clicked(self):
        pass                          

if __name__ == "__main__": 
    #app = QApplication(sys.argv)
    app = QApplication.instance()
    if app is None:
        app = QApplication(sys.argv)    
    myGUI = swapRefGUI(None)
    myGUI.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

目前您正在尝试通过类实例swapRefBtn访问swapRefGUI,但实际上您需要通过加载的ui变量访问它。 loader.load的第二个参数也应该是self以在窗口中显示qt gui。还有一些例子,你试图从PySide访问对象,如QDialog,它应该是QtGui.QDialog(因为你导入PySide模块的方式)。

这里有一些使用ui文件的代码。

from PySide import QtCore, QtGui, QtUiTools
import maya.cmds as cmds

class swapRefGUI(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)

        loader = QtUiTools.QUiLoader()
        uifile = QtCore.QFile('C:\Scripts\swapRef.ui')
        uifile.open(QtCore.QFile.ReadOnly)
        self.ui = loader.load(uifile, self) # Qt objects are inside ui, so good idea to save the variable to the class, 2nd arg should refer to self
        uifile.close()

        self.setFixedSize(400, 300)

        self.ui.swapRefBtn.clicked.connect(self.swapRefBtn_clicked) # Need to access button through self.ui
        #self.ui.closeBtn.clicked.connect(self.close) # This needs to have an existing function in the class or it will crash when executing

    def swapRefBtn_clicked(self):
        print 'WORKS'  

myGUI = swapRefGUI()
myGUI.show()