【问题标题】:optionMenu is not returning valueoptionMenu 没有返回值
【发布时间】:2016-06-28 07:56:55
【问题描述】:

我编写了一个简单的用户界面,要求用户从下拉列表中选择一些内容,然后使用该选择,代码将执行其余的内容

现在,我有 2 个问题.. 1. 只要选择了格式并点击了“确定”按钮,“值”就不会完全返回......我错过了什么吗? 2. 如何让我的 UI 在选择 OK 按钮后关闭?

import maya.cmds as cmds

def mainCode():
    ...
    ...
    print "UI popping up"
    showUI()

    print "A format has been selected"
    cmds.optionMenu('filmbackMenu', edit = True, value = xxx ) # <-- I want to grab the value from the menu selection and input into this 'value' flag
    ...
    ...

def showUI():

    if cmds.window("UI_MainWindow", exists = True):
        cmds.deleteUI("UI_MainWindow")

    cmds.window("UI_MainWindow", title = "User Interface Test", w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
    cmds.columnLayout("UI_MainLayout", w = 300, h =500)

    cmds.optionMenu("UI_FormatMenu", w = 250, label = "Select a Format")

    list01 = ['itemA-01', 'itemB-02', 'itemC-02', 'itemD-01', 'itemE-01', 'itemF-03']
    for x in list01:
        cmds.menuItem(label = str(x))

    cmds.button("UI_SelectButton", label = "OK", w = 200, command=ObjectSelection)

    cmds.showWindow("UI_MainWindow") #shows window

def ObjectSelection(*args):
    currentFormat = cmds.optionMenu("UI_FormatMenu", query=True, value=True)
    print currentFormat
    return currentFormat

【问题讨论】:

    标签: python selection maya optionmenu


    【解决方案1】:

    使用 python 字典或类在脚本中传递数据。 我真的不明白问题是什么。

    当你说:“'价值'并没有完全返回”,你是什么意思?你能告诉我们你得到了什么,你期望什么?

    def mainCode():
        ...
        ...
        showUI()
        cmds.optionMenu('filmbackMenu', edit = True, value = xxx ) # <-- I want to grab the value from the menu selection and input into this 'value' flag
        ...
    

    这里是从“输入值”中选择的值,我猜是:

    cmds.optionMenu('filmbackMenu', edit = True, value = ObjectSelection())
    

    但由于您的代码中没有filmbackMenu,我不确定。

    贾斯汀已在 Google 群组中回答了您的第二个问题。你只需要这样做:

    def ObjectSelection(*args):
        currentFormat = cmds.optionMenu("UI_FormatMenu", query=True, value=True)
        cmds.deleteUI("UI_MainWindow")#close the UI
        print currentFormat
        return currentFormat
    

    或者可能是“在选择了确定按钮后?”不意味着“按下确定按钮”?

    如果您想了解如何使用字典,您可以阅读我已回答的其他帖子:Maya Python - Using data from UI 您可以很好地使用 partial,我建议您阅读一下:Calling back user input values inside maya UI

    --- 编辑 ---

    我尝试创建一个功能齐全的示例:

    import maya.cmds as cmds
    
    uiDic = {}
    uiDic['this']= 1
    
    def ui_refresh(*args):
        uiDic['this'] = cmds.optionMenu("UI_FormatMenu", query=True, value=True)
        return uiDic['this']
    
    def showUI():
    
        if cmds.window("UI_MainWindow", exists = True):
            cmds.deleteUI("UI_MainWindow")
    
        cmds.window("UI_MainWindow", title = "User Interface Test", w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
        cmds.columnLayout("UI_MainLayout", w = 300, h =500)
    
        cmds.optionMenu("UI_FormatMenu", w = 250, label = "Select a Format")
    
        list01 = ['itemA-01', 'itemB-02', 'itemC-02', 'itemD-01', 'itemE-01', 'itemF-03']
        for x in list01:
            cmds.menuItem(label = str(x))
    
        cmds.button("UI_SelectButton", label = "OK", w = 200, command=ObjectSelection)
    
        uiDic['om_filmback'] = cmds.optionMenu('filmbackMenu' )
        list01 = ['itemA-01', 'itemB-02', 'itemC-02', 'itemD-01', 'itemE-01', 'itemF-03']
        for x in list01:
            cmds.menuItem(label = str(x))
    
    
        cmds.showWindow("UI_MainWindow") #shows window
    
    def ObjectSelection(*args):
        cmds.optionMenu(uiDic['om_filmback'], edit = True, value=ui_refresh())
    
    showUI()
    

    【讨论】:

    • 通过值不返回,我的意思是在用户选择格式并点击确定后,ObjectSelection 函数中不会返回所选格式。然而,在使用cmds.optionMenu('filmbackMenu', edit = True, value = ObjectSelection()) 时,UI 不会弹出,而只是使用菜单中的第一项作为选择。因此我的困境
    • 我修改了您的代码以进行测试。我已经编辑了我的答案。当你点击 OK 时,它会刷新第二个。不知道是不是你问的。如果不是,请尝试更精确。
    • 如果我不清楚,请原谅我,我对我的帖子做了一点修改。在我的 mainCode() 函数中,在包含 showUIprint "A format has been selected" 的行之间,我希望我的代码在此处“停止”并等到单击 UI 上的 OK 按钮(意味着已选择格式)在执行下一行代码之前。而不是那种情况,我的代码从头到尾运行而不关心 UI 选择,并且可以在代码执行之前完成选择。结束。它也在对你的代码做同样的事情,从头到尾运行而不考虑 UI 选择
    • 简单地说,它采用列表中的第一种格式作为选择,而不采用用户选择。希望这有意义吗?
    猜你喜欢
    • 1970-01-01
    • 2017-12-17
    • 2016-12-12
    • 2017-02-28
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多