2017-09-28 5 views
0

Ich beginne QtCreator mit einer Benutzeroberfläche für ein kleines Werkzeug zu schaffen für Maya 2017. QtCreator mir diese .ui Datei gab:Maya PySide2 UI Get QLineEdit Wert

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>DockWidget</class> 
<widget class="QDockWidget" name="DockWidget"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>550</width> 
    <height>251</height> 
    </rect> 
    </property> 
    <property name="floating"> 
    <bool>true</bool> 
    </property> 
    <property name="windowTitle"> 
    <string>Attr Editor</string> 
    </property> 
    <widget class="QWidget" name="dockWidgetContents"> 
    <widget class="QLineEdit" name="attr_value_textfield"> 
    <property name="geometry"> 
    <rect> 
     <x>130</x> 
     <y>128</y> 
     <width>391</width> 
     <height>20</height> 
    </rect> 
    </property> 
    <property name="toolTip"> 
    <string>attr_value</string> 
    </property> 
    </widget> 
    <widget class="QLabel" name="label"> 
    <property name="geometry"> 
    <rect> 
     <x>227</x> 
     <y>20</y> 
     <width>97</width> 
     <height>25</height> 
    </rect> 
    </property> 
    <property name="font"> 
    <font> 
     <family>Century Gothic</family> 
     <pointsize>16</pointsize> 
     <weight>75</weight> 
     <bold>true</bold> 
    </font> 
    </property> 
    <property name="text"> 
    <string>Attr Editor</string> 
    </property> 
    </widget> 
    <widget class="QPushButton" name="pushButton_2"> 
    <property name="geometry"> 
    <rect> 
     <x>240</x> 
     <y>180</y> 
     <width>75</width> 
     <height>23</height> 
    </rect> 
    </property> 
    <property name="text"> 
    <string>Submit</string> 
    </property> 
    <property name="+command" stdset="0"> 
    <string>submitCommand</string> 
    </property> 
    </widget> 
    <widget class="QLabel" name="label_3"> 
    <property name="geometry"> 
    <rect> 
     <x>20</x> 
     <y>130</y> 
     <width>80</width> 
     <height>16</height> 
    </rect> 
    </property> 
    <property name="font"> 
    <font> 
     <family>Century Gothic</family> 
    </font> 
    </property> 
    <property name="text"> 
    <string>Attribute Value</string> 
    </property> 
    </widget> 
    <widget class="QLineEdit" name="attr_name_textfield"> 
    <property name="geometry"> 
    <rect> 
     <x>130</x> 
     <y>78</y> 
     <width>391</width> 
     <height>20</height> 
    </rect> 
    </property> 
    <property name="toolTip"> 
    <string>attr_name</string> 
    </property> 
    <property name="whatsThis"> 
    <string/> 
    </property> 
    </widget> 
    <widget class="QPushButton" name="pushButton_3"> 
    <property name="geometry"> 
    <rect> 
     <x>440</x> 
     <y>180</y> 
     <width>75</width> 
     <height>23</height> 
    </rect> 
    </property> 
    <property name="text"> 
    <string>Cancel</string> 
    </property> 
    <property name="+command" stdset="0"> 
    <string>cancelCommand</string> 
    </property> 
    </widget> 
    <widget class="QPushButton" name="display_button"> 
    <property name="geometry"> 
    <rect> 
     <x>30</x> 
     <y>180</y> 
     <width>75</width> 
     <height>23</height> 
    </rect> 
    </property> 
    <property name="text"> 
    <string>Display</string> 
    </property> 
    <property name="+command" stdset="0"> 
    <string>displayCommand</string> 
    </property> 
    </widget> 
    <widget class="QLabel" name="label_2"> 
    <property name="geometry"> 
    <rect> 
     <x>20</x> 
     <y>80</y> 
     <width>82</width> 
     <height>16</height> 
    </rect> 
    </property> 
    <property name="font"> 
    <font> 
     <family>Century Gothic</family> 
    </font> 
    </property> 
    <property name="text"> 
    <string>Attribute Name</string> 
    </property> 
    </widget> 
    </widget> 
</widget> 
<resources/> 
<connections/> 
</ui> 

Und ich habe diesen Code Wich zeigt meine UI :

import maya.cmds as cmds 
from PySide2.QtWidgets import * 
from PySide2.QtCore import * 

if cmds.window(main_window, ex = True): 
    cmds.deleteUI(main_window) 

main_window = cmds.loadUI(uiFile = "C:/Users/thornydre/Desktop/attreditorui.ui") 
cmds.showWindow(main_window) 

def displayCommand(e): 
    print(attr_name_textfield.text()) 
    print(attr_value_textfield.text()) 

def submitCommand(e): 
    attr_name = attr_name_textfield.text() 
    attr_value = attr_value_textfield.text() 

    is_string = False 

    try: 
     new_attr_value = float(attr_value) 
     if float(attr_value) % 1 == 0: 
      new_attr_value = int(attr_value) 
    except: 
     is_string = True 
     new_attr_value = attr_value 

    print(new_attr_value) 

    for name in cmds.ls(sl = True): 
     if is_string: 
      cmds.setAttr(name + "." + attr_name, new_attr_value, type = "string") 
     else: 
      cmds.setAttr(name + "." + attr_name, new_attr_value) 

def cancelCommand(e): 
    cmds.deleteUI(main_window, window = True) 

Und wenn ich auf meinem display_button klicken, ich habe einen Fehler:

# Result: dockWidgetContents|display_button # 
# Error: AttributeError: file <maya console> line 12: 'bool' object has no attribute 'attr_name_textfield' # 

ich versuchte es mit einer Unterklasse zu tun QtWidgets.QWidget wie ich gefunden irgendwo im Internet, aber ich fand nicht wirklich Anleitung, wie man es richtig bauen:

from PySide2 import QtCore, QtGui, QtWidgets, QtUiTools 

class Interface(QtWidgets.QWidget): 
    def __init__(self, parent = None): 
     super(Interface, self).__init__(parent) 
     ui_filename = "C:/Users/thornydre/Desktop/attreditorui.ui" 
     ui_file = QtCore.QFile(ui_filename) 
     ui_file.open(QtCore.QFile.ReadOnly) 
     self.ui = QtUiTools.QUiLoader().load(ui_file, parentWidget=self) 
     ui_file.close() 

    def connectInterface(self): 
     QtCore.QtObject.connect(self.displayCommand, QtCore.SIGNAL("clicked()"), self.displayCommandWin) 

    def displayCommand(self): 
     print(self.attr_name_textfield.text()) 
     print(self.attr_value_textfield.text()) 

def main(): 
    global ui 
    ui = Interface() 

if __name__ == "__main__": 
    main() 

Das Gleiche gilt hier, die UI zeigt, aber wirklich nichts passiert, mit einem Klick auf die display_button

+0

bitte poste einen minimalen Beispielcode .. wahrscheinlich musst du self.attr_name.text() verwenden – Achayan

+0

Wie genau gibt es nichts zurück? Wird eine Ausnahme ausgelöst? Gibt es 'None' oder eine leere Zeichenfolge (' '' ') zurück? Können Sie nicht auf das QLineEdit-Widget selbst zugreifen? – cpburnz

+0

Sorry, ich habe den Fragen-Post aktualisiert, also sag mir, ob er überhaupt noch nicht klar ist. – Thornydre

Antwort

0

Ihr zweiter Versuch mit der Klasse scheint gut, aber es ist völlig normal, dass Sie displayCommand nicht ausführen können, Ihre Schaltfläche ist nicht mit Ihrer Methode verbunden.

Juste Ihre connectInterface Methode, löschen und direkt nach ui_file.close() Kopie dieses: self.ui.display_button.clicked.connect(self.displayCommand)

Es sollte besser funktionieren :) (und Sie haben die eyllanesc Lösung zu verwenden, auch den Zugang zu den Widgets Werte)