[英]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.