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