如何使用tk_optionMenu过程?

时间:2018-11-15 19:46:44

标签: tcl tcltk

我正在尝试使用TCL / Tk开发GUI,因为这是Pointwise网格生成软件的“宏语言”。我找到了一篇帖子,其中讨论了here可用的tk_optionMenu过程的使用。问题是我不了解如何调用例程,也不了解如何使各种条目进入选项菜单。我要的选项不是数字,而是不同例程的文本。该例程中的信息指出,参数是“> = 1”,而不是文本。例如,我想要一个选项菜单,以便用户可以选择在网格上使用的平滑方法。在我使用TCL设置网格格式的GUI中,我有多个位置要使用选项菜单。

使用Glenn Jackman的建议,我尝试了以下操作:

#!/usr/bin/wish
# the next line restarts using wish \
#exec wish "$0" "$@"

# interface generated by SpecTcl version 1.2 from /home/hh-eagle/ab/salter/bin/src/GLYPH/PrismEditor/GUI/PE_v01.ui
#   root     is the parent window for this user interface

package require Tk

proc PE_v01_ui {root args} {

    # this treats "." as a special case

    if {$root == "."} {
        set base ""
    } else {
        set base $root
    }

    label $base.solver \
        -text {Solver:}

    set solvers {TriSmooth QuadSmooth VolSmooth K_lineSmooth}
    tk_optionMenu $base.solvers activeSolver {*}$solvers

    # Add contents to menus

#        $base.solvers.menu add radiobutton -label TriSmooth
#        $base.solvers.menu add radiobutton -label QuadSmooth
#        $base.solvers.menu add radiobutton -label VolSmooth
#        $base.solvers.menu add radiobutton -label K_lineSmooth

    # Geometry management

    grid $base.solver -in $root      -row 2 -column 6 
    grid $base.solvers -in $root     -row 2 -column 7  \
            -columnspan 2

    # Resize behavior management

#       grid rowconfigure $root 1 -weight 0 -minsize 30 -pad 0
#       grid rowconfigure $root 2 -weight 0 -minsize 30 -pad 0
#       grid rowconfigure $root 3 -weight 0 -minsize 30 -pad 0
#       grid rowconfigure $root 4 -weight 0 -minsize 30 -pad 0
#       grid rowconfigure $root 5 -weight 0 -minsize 30 -pad 0
#       grid rowconfigure $root 6 -weight 0 -minsize 30 -pad 0
#       grid rowconfigure $root 7 -weight 0 -minsize 30 -pad 0
#       grid rowconfigure $root 8 -weight 0 -minsize 30 -pad 0
#       grid rowconfigure $root 9 -weight 0 -minsize 30 -pad 0
#       grid columnconfigure $root 1 -weight 0 -minsize 30 -pad 0
#       grid columnconfigure $root 2 -weight 0 -minsize 30 -pad 0
#       grid columnconfigure $root 3 -weight 0 -minsize 30 -pad 0
#       grid columnconfigure $root 4 -weight 0 -minsize 30 -pad 0
#       grid columnconfigure $root 5 -weight 0 -minsize 30 -pad 0
#       grid columnconfigure $root 6 -weight 0 -minsize 30 -pad 0
#       grid columnconfigure $root 7 -weight 0 -minsize 2 -pad 0
#       grid columnconfigure $root 8 -weight 0 -minsize 2 -pad 0
#       grid columnconfigure $root 9 -weight 0 -minsize 30 -pad 0
#       grid columnconfigure $root 10 -weight 0 -minsize 30 -pad 0
# additional interface code
# end additional interface code

}

# Allow interface to be run "stand-alone" for testing

catch {
    if [info exists embed_args] {
        # we are running in the plugin
        PE_v01_ui .
    } else {
        # we are running in stand-alone mode
        if {$argv0 == [info script]} {
            wm title . "Testing PE_v01_ui"
            PE_v01_ui .
        }
    }
}

运行此命令时,会出现一个空白框,顶部带有用于图标化或关闭它的选项。取消注释或注释列和行配置信息没有区别。使用注释行而不是{*}solvers来填充optionMenu也没有任何区别。所以,有些事情是不正确的,我也不知道该怎么办。

1 个答案:

答案 0 :(得分:2)

这是一个简短的演示:

#!/usr/bin/env tclsh
package require Tk

set choices {foo bar baz qux "None of the above"}
label .menuLabel -text "Make a selection: "
# "choice" is a global variable
tk_optionMenu .menu choice {*}$choices

label .displayLabel -text "You chose: "
label .display -textvariable choice

grid .menuLabel .menu
grid .displayLabel .display