单击时打印按钮的标签 [英] Printing the label of a button when clicked

查看:81
本文介绍了单击时打印按钮的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试在按下按钮时输出按钮的标签(按钮标签应替换buttonNumber函数打印中的XXX).我不知道如何从按钮中检索标签数据以进行输出.有什么想法吗?预先感谢

So I'm trying to output the label of the button as it's pressed (Button label should replace the XXX in the print of the buttonNumber function). I do not know how to retrieve the label data from the button to output. Any thoughts? Thanks in advance

import maya.cmds as mc

def buttonNumber(*args):
    print 'Button XXX was pressed'

def openWindow():
    if mc.window('windowTest', ex=True):
        mc.deleteUI('windowTest', window=True)

    mc.window('windowTest', title='', s=True, resizeToFitChildren = True,)
    mc.rowColumnLayout(numberOfColumns = 3, columnWidth = [ (1, 150), (2, 150), (3, 150)])
    mc.button(label='1', command=buttonNumber)
    mc.button(label='2', command=buttonNumber)
    mc.button(label='3', command=buttonNumber)
    mc.button(label='4', command=buttonNumber)
    mc.button(label='5', command=buttonNumber)
    mc.button(label='6', command=buttonNumber)
    mc.button(label='7', command=buttonNumber)
    mc.button(label='8', command=buttonNumber)
    mc.button(label='9', command=buttonNumber)
    mc.showWindow('windowTest')

openWindow()

推荐答案

正如@DeWeeny所说,您可以使用functools.partial对象将值绑定到按钮命令.对于这个非常简单的应用程序,您也可以使用函数工厂来实现,也可以使用为每个实例记住一个值的可调用类来实现.

As @DeWeeny says, you can bind a value to the button commands with a functools.partial object. You could also do it with a function factory for this very simple application, or with a callable class that remembered a value for each instance.

def make_callback(value):
    def inner_callback(_):
        print value, "was clicked"
return inner_callback


w = cmds.window()
c = cmds.columnLayout()
for n in range(9):
    cmds.button( label = str(n), command = make_callback(n))

cmds.showWindow(w)

唯一的微妙之处是inner_callback有一个未使用的参数(_),因为按钮回调总是会触发一个无用的参数,所以它需要使用该参数

The only subtlety there is that inner_callback has an unused argument (the _) which it needs because button callbacks always fire a useless parameter

如果数据比函数工厂示例中的数据更复杂或需要更多的计算,这将很有用

This is useful if the data is more complex or needs more calculation than in the function factory example

class NumberCallback(object):
    def __init__(self, id):
        self.id = 
def __call__(self, _):
    print self.id, "was clicked"

w = cmds.window()
c = cmds.columnLayout()
for n in range(9):
    cmds.button( label = str(n), command = NumberCallback(n))

cmds.showWindow(w)

这在功能上是相同的,但是如果您不得不以更复杂的行为来做某事,那么一个类将使它整洁

This is functionally identical but if you had to do something with more complex behavior a class would make it tidy

您可能会在网上找到有关使用lambda进行操作的建议.对于许多应用程序来说,这将是很棒的-但不要循环执行.如果您这样做:

You may find advice on the web to do it with a lambda. For lots of applications that would be great -- but don't do it in a loop. If you do this:

def clicked(num):
    print num, "was clicked"

w = cmds.window()
c = cmds.columnLayout()
for n in range(9):
    cmds.button( label = str(n), command = lambda p: clicked(n))

cmds.showWindow(w)

所有按钮都会响应

8 was clicked

因为lambda都将捕获循环中最后一个不是您想要的变量.

because the lambdas will all capture the last variable in the loop which is not what you want.

更多详细信息此处.以及此处

这篇关于单击时打印按钮的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆