简体   繁体   English

Maya Python:从内部函数访问外部函数变量

[英]Maya Python: Accessing outer function variable from inner function

I want to write a UI window using python, the following is my code, the function is running correctly, but there is a problem, when I choose an item in the textScrollList, it should call the inner function 'update()' and highlight the corresponding object in the scene. 我想使用python编写UI窗口,以下是我的代码,该函数运行正确,但是有一个问题,当我在textScrollList中选择一个项目时,它应该调用内部函数“ update()”并突出显示场景中相应的对象。 However, the object cannot be chosen correctly and it shows an error message like this: 但是,无法正确选择对象,并且显示如下错误消息:

"Object 'alertWindow|formLayout164|textScrollList27' not found." “找不到对象'alertWindow | formLayout164 | textScrollList27'。”

I think this happens because the inner function update() cannot access the variable tsl in the outer function, does anyone know how I can revise my code? 我认为发生这种情况是因为内部函数update()无法访问外部函数中的变量tsl,有人知道我可以修改我的代码吗?

def alertWindow():
    if(cmds.window('mainWindow', q =True, exists = True,)):
        cmds.deleteUI('mainWindow')
    UI = cmds.window('mainWindow', title = 'Alert!', maximizeButton = False,   minimizeButton = False, resizeToFitChildren = True, widthHeight = (250, 300), sizeable = False)
    myForm=cmds.formLayout(  )

    txt = cmds.text(label = 'Please check the following objects :')
    tsl = cmds.textScrollList(width = 200, height = 200, enable = True, allowMultiSelection = True, selectCommand = 'update()') 

    count = len(obj)
    for i in range(count):
        cmds.textScrollList(tsl, edit=True, append = obj[i])

    delete = cmds.button(label = 'delete', width = 100, command = 'remove()')
    clz = cmds.button(label = 'Close', width = 100, command = 'cmds.deleteUI("mainWindow")')

    cmds.formLayout(myForm, edit = True, attachForm = [(txt, 'top', 20),(txt, 'left', 65),(tsl, 'left', 25),(tsl, 'top', 50),(delete,'bottom',10),(delete,'left',15),(clz,'bottom',10),(clz,'right',20)])
    cmds.showWindow(UI)

    def update():
        cmds.select(cmds.textScrollList(tsl, q=True, selectItem = True))

    def remove():
        cmds.DeleteHistory()
        cmds.textScrollList(tsl, edit=True, removeItem = cmds.ls(sl=True))

You can try with a global variable "global tsl" at the top of your code. 您可以在代码顶部尝试使用全局变量“ global tsl”。 That isn't the most beautiful however, it works :) 那不是最漂亮的,它可以工作:)

You need to define your inner functions first, then just reference them, no need to use a string for command= : 您需要先定义内部函数,然后才引用它们,而无需在command=使用字符串:

def alertWindow(obj):
    def update():
        cmds.select(cmds.textScrollList(tsl, q=True, selectItem = True))

    def remove():
        cmds.DeleteHistory()
        cmds.textScrollList(tsl, edit=True, removeItem = cmds.ls(sl=True))

    if(cmds.window('mainWindow', q =True, exists = True,)):
        cmds.deleteUI('mainWindow')
    UI = cmds.window('mainWindow', title = 'Alert!', maximizeButton = False,   minimizeButton = False, resizeToFitChildren = True, widthHeight = (250, 300), sizeable = False)
    myForm=cmds.formLayout(  )

    txt = cmds.text(label = 'Please check the following objects :')
    tsl = cmds.textScrollList(width = 200, height = 200, enable = True, allowMultiSelection = True, selectCommand = update) 

    count = len(obj)
    for i in range(count):
        cmds.textScrollList(tsl, edit=True, append = obj[i])

    delete = cmds.button(label = 'delete', width = 100, command = remove)
    clz = cmds.button(label = 'Close', width = 100, command = 'cmds.deleteUI("mainWindow")')

    cmds.formLayout(myForm, edit = True, attachForm = [(txt, 'top', 20),(txt, 'left', 65),(tsl, 'left', 25),(tsl, 'top', 50),(delete,'bottom',10),(delete,'left',15),(clz,'bottom',10),(clz,'right',20)])
    cmds.showWindow(UI)




cmds.polyCube()
alertWindow(['pCube1'])

You could also use a class to keep the state of things. 您还可以使用一个类来保持事物的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python中,是否可以使用内部函数体内的外部函数变量? - Is there a way to use variable from outer function in the inner function body, in Python? 从内部函数访问外部函数的变量 - Access variable of outer function from inner function 在python中将相同的参数从外部函数传递给内部函数 - passing same argument from outer function to inner function in python 从python中的内部函数对象获取外部函数的名称 - Get name of outer function from inner function object in python 在内部函数内部分配外部变量的python闭包 - python closure with assigning outer variable inside inner function 从内部python访问外部类 - Accessing outer class from inner python 在 Python 中将多个关键字参数从外部函数传递到内部函数? - relay multiple keyword arguments from outer function to inner functions in Python? 如何通过在python中实现'inner'函数来更改用'outer'函数定义定义的变量值? - How to change the variable value defined with 'outer' function definition through implementing 'inner' function in python? 要从返回的内部函数引用外部函数范围内的变量 - Want to refer to variable in the outer function's scope from an inner function being returned 在外部函数中定义的局部变量在内部函数中不起作用? - local variable defined in outer function don't work in inner function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM