Python 是否有将数据从UI窗口接收到变量的选项?

Python 是否有将数据从UI窗口接收到变量的选项?,python,scripting,window,maya,autodesk,Python,Scripting,Window,Maya,Autodesk,我看到可以打印在用户界面窗口中写入的数据,但是,当我在互联网上搜索时,没有一个选项是这样的,将数据检索到变量中 这是一个非常简单的窗口代码- " " 我想在按下确认按钮后将文本字段中的数据检索到变量中 在cmds.button行中,您可以在命令中看到-“print TxtField”。 我知道,如果有一个选项可以打印在文本字段中写入的内容,那么必须有一个选项将其放入变量中。但是,我没有找到它。有人知道怎么做吗 对不起,刚才的事。问题。您必须使用部分模块通过按钮命令(或lambda函数)传递变量

我看到可以打印在用户界面窗口中写入的数据,但是,当我在互联网上搜索时,没有一个选项是这样的,将数据检索到变量中

这是一个非常简单的窗口代码- "

"

我想在按下确认按钮后将文本字段中的数据检索到变量中

在cmds.button行中,您可以在命令中看到-“print TxtField”。 我知道,如果有一个选项可以打印在文本字段中写入的内容,那么必须有一个选项将其放入变量中。但是,我没有找到它。有人知道怎么做吗


对不起,刚才的事。问题。

您必须使用部分模块通过按钮命令(或lambda函数)传递变量

这是同样的问题:

来自:

print
依次计算每个表达式并写入结果 对象转换为标准输出。如果对象不是字符串, 首先使用字符串的规则将其转换为字符串 转换。然后写入(结果或原始)字符串


用几句话来概括:

print
语句在编写后需要一个表达式。这可以是字符串、int、对象

在您的情况下:

def printTxtField ( fieldID ):
    #here is the modification made
    myVar = cmds.textField( fieldID, query=True, text=True)
import maya.cmds as cmds

KEVS_TEXTFIELD_VALUE = None

####################################################
# This function retrieve the value of Kevs_TextField
# and set this value to KEVS_TEXTFIELD_VALUE
####################################################
def retrieveData():
    # We use the query flag on the text attribute
    KEVS_TEXTFIELD_VALUE = cmds.textField("Kevs_TextField", query=True, text=True)
    print u"KEVS_TEXTFIELD_VALUE =" + KEVS_TEXTFIELD_VALUE

####################################################
# This function will create a show a new UI
####################################################
def drawUI():
    winID = 'kevsUI'

    if cmds.window(winID, exists=True): #If the window exists
        cmds.deleteUI(winID)            #Delete it
    cmds.window(winID)                  #Then create a new one

    cmds.columnLayout("Kevs_ColLayout") #Create a columnLayout to sort our widgets  

    # Now let's add some widgets in our columnLayout
    cmds.textField("Kevs_TextField")    #Add a textfied in the columnLayout
    #Then create a button that calls the retrieveData function when pressed
    cmds.button("Kevs_Button", label="Confirm", command=retrieveData)
    cmds.showWindow() #Show the window


drawUI() # Let's create a new UI
printTxtField
函数中打印的是函数的返回值(
cmds.textField(fieldID,query=True,text=True)

写这篇文章时:

cmds.textField(fieldID,query=True,text=True)
您正在告诉Maya:

  • 查找名为fieldID的值的文本字段
  • 对其执行查询(查询标志设置为True
  • 查询它的文本(里面写了什么)
  • 返回此值
  • 总结如下:

    无需打印返回值,您可以轻松地将此值分配给变量:
    myVar=cmds.textField(fieldID,query=True,text=True)


    您修改的代码:

    def printTxtField ( fieldID ):
        #here is the modification made
        myVar = cmds.textField( fieldID, query=True, text=True)
    
    import maya.cmds as cmds
    
    KEVS_TEXTFIELD_VALUE = None
    
    ####################################################
    # This function retrieve the value of Kevs_TextField
    # and set this value to KEVS_TEXTFIELD_VALUE
    ####################################################
    def retrieveData():
        # We use the query flag on the text attribute
        KEVS_TEXTFIELD_VALUE = cmds.textField("Kevs_TextField", query=True, text=True)
        print u"KEVS_TEXTFIELD_VALUE =" + KEVS_TEXTFIELD_VALUE
    
    ####################################################
    # This function will create a show a new UI
    ####################################################
    def drawUI():
        winID = 'kevsUI'
    
        if cmds.window(winID, exists=True): #If the window exists
            cmds.deleteUI(winID)            #Delete it
        cmds.window(winID)                  #Then create a new one
    
        cmds.columnLayout("Kevs_ColLayout") #Create a columnLayout to sort our widgets  
    
        # Now let's add some widgets in our columnLayout
        cmds.textField("Kevs_TextField")    #Add a textfied in the columnLayout
        #Then create a button that calls the retrieveData function when pressed
        cmds.button("Kevs_Button", label="Confirm", command=retrieveData)
        cmds.showWindow() #Show the window
    
    
    drawUI() # Let's create a new UI
    

    我已经对您的代码进行了注释和重新组织,使其更加清晰:

    def printTxtField ( fieldID ):
        #here is the modification made
        myVar = cmds.textField( fieldID, query=True, text=True)
    
    import maya.cmds as cmds
    
    KEVS_TEXTFIELD_VALUE = None
    
    ####################################################
    # This function retrieve the value of Kevs_TextField
    # and set this value to KEVS_TEXTFIELD_VALUE
    ####################################################
    def retrieveData():
        # We use the query flag on the text attribute
        KEVS_TEXTFIELD_VALUE = cmds.textField("Kevs_TextField", query=True, text=True)
        print u"KEVS_TEXTFIELD_VALUE =" + KEVS_TEXTFIELD_VALUE
    
    ####################################################
    # This function will create a show a new UI
    ####################################################
    def drawUI():
        winID = 'kevsUI'
    
        if cmds.window(winID, exists=True): #If the window exists
            cmds.deleteUI(winID)            #Delete it
        cmds.window(winID)                  #Then create a new one
    
        cmds.columnLayout("Kevs_ColLayout") #Create a columnLayout to sort our widgets  
    
        # Now let's add some widgets in our columnLayout
        cmds.textField("Kevs_TextField")    #Add a textfied in the columnLayout
        #Then create a button that calls the retrieveData function when pressed
        cmds.button("Kevs_Button", label="Confirm", command=retrieveData)
        cmds.showWindow() #Show the window
    
    
    drawUI() # Let's create a new UI
    

    这是可能的,但在处理过程中有几个问题。@DrHaze的示例显示了正确的做法,即使用实际的python函数而不是字符串

    您还需要考虑不同功能的可见性:在监听器中很容易让所有代码都在一个地方工作,但是一旦涉及到多个功能或模块,跟踪gui小部件名称就变得更加困难

    对于小型工具,您可以在创建gui小部件的同一位置定义回调函数(在本例中,是按钮使用的回调函数)。这将使用python的规则为您跟踪小部件:

    def window_example(title):
        if cmds.window(title, exists=True):
            cmds.deleteUI(title)
        win = cmds.window(title)
        cmds.columnLayout()
        whatUSay = cmds.textField()
    
        #defining this hear gives it access to 'whatUSay'
        def print_text_contents(ignore):
            print cmds.textField( whatUSay, query=True, text=True)
    
        cmds.button(label='Confirm', command=print_text_contents)
        cmds.showWindow(win)
    window_example('kevsUI')
    
    对于较长的工具,您可能希望了解如何使用类执行此操作


    这里有一些不同的策略要检查。

    我认为你的问题不清楚。就我个人而言,我无法真正理解你想做什么!!!:(作为@mlwn,你的问题一点也不清楚(比如你之前的问题,在那里发布了一些评论以要求澄清)。如果你想要一个快速而好的答案,你需要清楚地解释你想要什么。关于你的问题,数据是什么(字符串、int、图像、猫的视频…)?UI窗口是什么(我在这里只看到一个按钮)?到目前为止你尝试了什么?你能提供一段可运行且有注释的代码吗?你能提供屏幕截图吗?好的,我知道了,非常感谢你的回答!现在,如果我想更改UI窗口,而不是接收文本数据,我将接收数字,我该怎么做?因为我试图操作代码,但没有成功d、 它在命令(从末尾起两行)中写入retrieveData不是声明的全局变量。
    cmds.intField(“Kevs_intField”,query=True,value=True)
    查看@theodox对其答案的评论…我已经编辑了关于错误的答案,我的错。好的,谢谢!那么作为输入而不是字符串的数字呢?您编写的代码中会有什么变化?仅将其更改为intField而不是textField等?每个小部件都会有不同的标志来获取其内容。对于文本fields它是“文本”标志,对于intFields它将是“值”标志。您需要检查maya文档中每个小部件的正确标志