如何在每次选择检查按钮时阻止菜单关闭

时间:2016-06-14 15:46:35

标签: tcl tk

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

#!/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

enter image description here

1 个答案:

答案 0 :(得分:0)

也许一种不同的方法可以更好地用于这个特定的用户界面?

这将显示一个新的顶层窗口,其中包含一个选项列表 可以选择。全局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
相关问题