我是编码的新手,我正试图找到一种方法让搜索字段只过滤我想要的按钮。我在窗口中创建了一个图标文本按钮,并希望根据按钮标签名称对其进行过滤
这是一个显示我想要做的事情的示例。此外,所有这些圈子标签名称都是circleA,circleB,circleC,circleD。如果我输入圆圈,我想显示具有该名称的按钮
我发现这个页面完全符合我的要求但是,如何更改以便查找标签名称并仅显示我输入的那个。另外,默认情况下我要显示所有图标
http://melscriptingfordummies.blogspot.in/2011/02/mel-script-example-keyword-search.html
答案 0 :(得分:0)
以下是您想要使用的策略的简单示例。这使用按钮,而不是icontext按钮,但想法是一样的。结构是你想要复制的部分;整个事情包含在columnLayout中以便扩展,过滤器是带有文本字段和按钮的RowLayout,然后所有按钮都在第二个columnLayout中,可以在显示 - 隐藏按钮时展开和收缩。
import maya.cmds as cmds
# some dummy commands. Not the use of "_", which is
# a lazy way to ignore the argument which Maya will
# add to all callback functions
def apple_command(_):
cmds.polyCube()
def orange_command(_):
cmds.polySphere()
def banana_command(_):
cmds.polyPlane()
window = cmds.window(title = "filter")
root = cmds.columnLayout()
# filter button and text field
header = cmds.rowLayout(nc=2)
filter_field = cmds.textField()
filter_button = cmds.button(label = 'filter')
cmds.setParent("..")
# hideable butttons
button_list = cmds.columnLayout()
# create a bunch of buttons, storing the name in a dictionary for easier filtering
# this is a dictionary with the label of the button and the commands you want to
# attach to the buttons
button_names = {
'apples': apple_command,
'oranges': orange_command,
'bananas': banana_comand,
'mangoes': apple_command,
'coconuts': orange_command,
'durians': banana_command
}
button_widgets = {}
for button_name, button_command in button_names.items():
button_widgets[button_name] = cmds.button(label = button_name, width = 160, c= button_command)
# in a real application you'd also make the buttons do something....
# this makes the filter button apply the filter
# defining it down here lets it 'remember' the name of the filter
# field and the contents of the button dictionary
def apply_filter(*_):
# get the current text in the filter field
filter_text = cmds.textField(filter_field, q=True, text=True)
# loop over the dictionary, setting the visiblity of the button
# based on the key and the filter
for key, value in button_widgets.items():
viz = True
if len(filter_text):
viz = filter_text.lower() in key.lower()
cmds.button(value, edit=True, visible = viz)
# now hook it too the button
cmds.button(filter_button, e=True, c= apply_filter)
cmds.showWindow(window)
基本上,您所做的是在将按钮创建到将其映射到实际GUI小部件的字典中时收集可过滤的名称。然后你可以循环整个过程,根据过滤器设置可见性。在columnLayout
中,当您更改其可见性时,它们会自动缩小差距。
您可以通过创建布局并以相同的方式显示/隐藏它们而不是管理单个控件来在控件集上使用此技巧。