选择复选按钮时如何防止每次关闭菜单

How to prevent menu to be closed each time when selecting checkbutton

以下是我的代码示例。 每次需要 select 多个选项时,都要重新打开菜单,这很烦人。由于每次复选框 selected,菜单都会自动关闭。 我该如何预防?

#!/usr/bin/env wish


frame .top
pack .top -expand yes -fill both 
wm title . TEST

menubutton .top.fillmet -text "select fill metals" -menu .top.fillmet.mtls  

set m .top.fillmet.mtls
menu $m

$m add checkbutton -label "fill m2" -variable fillm2 -onvalue "fillm2" -offvalue ""

$m add checkbutton -label  "fill m3"  -variable fillm3  -onvalue "fillm3"  -offvalue ""
$m add checkbutton -label  "fill m4"  -variable fillm4  -onvalue "fillm4"  -offvalue ""
$m add checkbutton -label  "fill m5"  -variable fillm5  -onvalue "fillm5"  -offvalue ""
$m add checkbutton -label  "fill m6"  -variable fillm6  -onvalue "fillm6"  -offvalue ""
$m add checkbutton -label  "fill m7"  -variable fillm7  -onvalue "fillm7"  -offvalue ""
$m add checkbutton -label  "fill m8"  -variable fillm8  -onvalue "fillm8"  -offvalue ""
$m add checkbutton -label  "fill m9"  -variable fillm9  -onvalue "fillm9"  -offvalue ""
$m add checkbutton -label  "fill m10" -variable fillm10 -onvalue "fillm10" -offvalue ""
$m add checkbutton -label  "fill m11" -variable fillm11 -onvalue "fillm11" -offvalue ""
$m add checkbutton -label  "fill m12" -variable fillm12 -onvalue "fillm12" -offvalue ""


pack .top.fillmet

对于这个特定的用户界面,也许不同的方法会更好?

这将显示一个新的顶层 window,其中包含一个选项列表 可以选择。全局 fillm 数组将填充 结果。

proc doOptions { } {
  global fillm

  set w .optfillmet
  toplevel $w
  for {set i 2} {$i < 13} {incr i} {
     checkbutton $w.cb$i -text "fill m$i" -variable fillm($i) \
          -onvalue fillm$i -offvalue ""
     pack $w.cb$i -in $w -side top -anchor w
  }
  button $w.b -text close -command [list destroy $w]
  pack $w.b -in $w -side top -anchor e
}

frame .top
pack .top -expand yes -fill both 
wm title . TEST

# .top.fillmet.mtls ; # redundant line
menu .top.fillmet 
.top.fillmet add command -label "select fill metals" -command doOptions
. configure -menu .top.fillmet