PySide UI creation for Maya help

Hi,
I´m learning PySide becouse I´m writing an autorig. Now I´m in the UI creation.

I´m having some problems trying to change the button text when I click on the button.
The main reason is to get the seleted join name in the text for this button.

I guess it´s a minor thing, but I´m missing some concept I don´t realise, so a little bit of help It would be very handy.

I appreciate it.
Cheers

from PySide import QtGui
import maya.OpenMayaUI as mui
import shiboken

def getMayaWindow():
	pointer = mui.MQtUtil.mainWindow()
	return shiboken.wrapInstance(long(pointer), QtGui.QWidget)

def createUI():

	objectName = 'testWin'

	if cmds.window('testWin', exists = True):
		cmds.deleteUI('testWin', wnd = True)

	parent = getMayaWindow()
	window = QtGui.QMainWindow(parent)
	window.setObjectName(objectName)
	window.setWindowTitle('Title')

	mainWidget = QtGui.QWidget()
	window.setCentralWidget(mainWidget)

	verticalLayout = QtGui.QVBoxLayout(mainWidget)

	layout2 = QtGui.QHBoxLayout()
	verticalLayout.addLayout(layout2)

	button = QtGui.QPushButton('None')
	verticalLayout.addWidget(button)
	button.clicked.connect(changeName)
	window.show()

def changeName():
	button.setText('22')
	
createUI()

“button” in changeName is out of scope. Make this a class and button a member variable

Could be possible a little illustrative example please? :slight_smile:
Thanks a lot for your reply!!


class MyClass(object):
    def __init__(self):
        self._button = None
    def createUI(self):
        self._button = QtGui.QPushButton('Click me', self)
        self._button.clicked.connect(self.clicked)
        self._button.show()
    def clicked(self):
        self._button.setText('22')

cls = MyClass()
cls.createUI()

If you’re just playing around inside Maya script editor, you can do this as well.

From PySide import QtGui

mybutton = QtGui.QPushButton()
mybutton.show()

Now you can keep referring to mybutton to play with it.

mybutton.setText("Hello")
mybutton.setText("Hello World")
mybutton.hide()
mybutton.show()

def saysomething():
    print "click click click"

mybutton.clicked.connect(saysomething)

And if the button goes behind Maya, you can pull it back to the top with mybutton.raise_()

if you want the method to adjust your button, you’ll have to pass the button as argument and you’ll be able to do something like this.

def changetext(elem, text):
    elem.setText(text)

changetext(mybutton, "Change to this text.")

Of course when you’re developing an actual, functional UI you’ll want to wrap everything in a class.

Thank you so much for the examples! Now I´m starting a see the light! :wink:

from PySide import QtGui
import maya.OpenMayaUI as mui
import shiboken

def getMayaWindow():
	pointer = mui.MQtUtil.mainWindow()
	return shiboken.wrapInstance(long(pointer), QtGui.QWidget)

class create_ui(object):
    def __init__(self):
        self._button = None

    def check_win_exists(self):
        objectName = 'testWin'
        
        if cmds.window('testWin', exists = True):
            cmds.deleteUI('testWin', wnd = True)            
            
    def get_maya_window(self):
        pointer = mui.MQtUtil.mainWindow()
        return shiboken.wrapInstance(long(pointer), QtGui.QWidget)
	
    def UI(self):
        
        objectName = 'testWin'
        parent = getMayaWindow()
        window = QtGui.QMainWindow(parent)
        window.setObjectName(objectName)
        window.setWindowTitle('Title')
        
        mainWidget = QtGui.QWidget()
        window.setCentralWidget(mainWidget)

        verticalLayout = QtGui.QVBoxLayout(mainWidget)
        
        layout = QtGui.QHBoxLayout()
        verticalLayout.addLayout(layout)
        
        self.group01 = QtGui.QGroupBox('ARPG Autorig')
        self.group01.setLayout(verticalLayout)
        
        #self._label1 = QtGui.QLabel('Select ARM Joint:')
        #verticalLayout.addWidget(self._label1)
        self._button = QtGui.QPushButton('Select Arm Root Joint')
        verticalLayout.addWidget(self._button)
        self._button.clicked.connect(self.getRoot)
        
        #self._label2 = QtGui.QLabel('Select LEG Joint:')
        #verticalLayout.addWidget(self._label2)
        self._button2 = QtGui.QPushButton('Select Leg Root Joint')
        verticalLayout.addWidget(self._button2)
        self._button2.clicked.connect(self.getRoot)  
        
        #self._label3 = QtGui.QLabel('Select SPINE Joint:')
        #verticalLayout.addWidget(self._label3)
        self._button3 = QtGui.QPushButton('Select Spine Root Joint')
        verticalLayout.addWidget(self._button3)
        self._button3.clicked.connect(self.getRoot)        
        
   
        
        self._button4 = QtGui.QPushButton('Create Rig')
        verticalLayout.addWidget(self._button4)
        self._button4.setStyleSheet('QPushButton{background-color: red;}')
        
        self.group01.show()
        
                                
        #window.show()
        
    def getRoot(self):
        #check which button is pressed and change the label for this button
        #and return the name of this selected bone
        root_jnt = cmds.ls(sl=True)
        self._button.setText(root_jnt[0])
        self._button.setStyleSheet('QPushButton{background-color: green}')
        return root_jnt

cls = create_ui()

cls.get_maya_window()
cls.check_win_exists()
cls.UI()
t = cls.getRoot()