TCL:单击checkbutton后,变量值不会更新

TCL:单击checkbutton后,变量值不会更新,tcl,tk,Tcl,Tk,我正在尝试设置一系列复选按钮,当单击这些按钮时,会将链接到这些按钮的变量值设置为1。 “运行”按钮将仅运行选中“检查”按钮的检查。 单击按钮时,全局变量值例如:check_H1不会更新 #!/usr/local/bin/wish package require Tk frame .top -width 50 -height 30 -borderwidth 5 -padx 5 -pady 5 -relief raised checkbutton .top.c1 -text H1 -variab

我正在尝试设置一系列复选按钮,当单击这些按钮时,会将链接到这些按钮的变量值设置为1。 “运行”按钮将仅运行选中“检查”按钮的检查。 单击按钮时,全局变量值例如:check_H1不会更新

#!/usr/local/bin/wish

package require Tk

frame .top -width 50 -height 30 -borderwidth 5 -padx 5 -pady 5 -relief raised
checkbutton .top.c1 -text H1 -variable "check_H1" -command {set_h1}
checkbutton .top.c2 -text H2 -variable "check_H2" -command {set_h2} 
checkbutton .top.c3 -text H3 -variable "check_H3" -command {set_h3}
checkbutton .top.c4 -text H4 -variable "check_H4" -command {set_h4}
checkbutton .top.c5 -text H5 -variable "check_H5" -command {set_h5}
checkbutton .top.c6 -text H6 -variable "check_H6" -command {set_h6}

button .top.b1 -text "RUN" -command [list select $check_H1 $check_H2 $check_H3 $check_H4 $check_H5 $check_H6] 
grid .top
grid .top.c1 -row 2 -column 2 
grid .top.c2 -row 2 -column 3 
grid .top.c3 -row 3 -column 2 
grid .top.c4 -row 3 -column 3 
grid .top.c5 -row 4 -column 2 
grid .top.c6 -row 4 -column 3 
grid .top.b1 -row 5 -column 5

proc select {check_H1 check_H2 check_H3 check_H4 check_H5 check_H6} {
    #upvar check_H1 check_H2 check_H3 sa3 check_H4 sa4 check_H5 sa5 check_H6 sa6
    puts "Value of H1 is $check_H1\n"
    puts $check_H2
    puts $check_H3
    puts $check_H4
    puts $check_H5
    puts $check_H6

    if {$check_H1 == 1} {
        run_h1
    }
    if {$check_H2 ==1} {
        run_h2
    }
    if {$check_H3 ==1} {
        run_h3
    }
    if {$check_H4 ==1} {
        run_h4
    }
    if {$check_H5 ==1} {
        run_h5
    }
    if {$check_H6 ==1} {
        run_h6
    }
}

proc set_h1 {} {
    global check_H1
    set check_H1 1
    puts $check_H1
    puts "H1 is set\n"
}
proc set_h2 {} {
    global check_H2
    set check_H2 1
    puts "H2 is set\n"
}
proc set_h3 {} {
    global check_H3
    set check_H3 1
    puts "H3 is set\n"
}
proc set_h4 {} {
    set check_H4 1
    puts "H4 is set\n"
}
proc set_h5 {} {
    set check_H5 1
    puts "H5 is set\n"
}
proc set_h6 {} {
    set check_H6 1
    puts "H6 is set\n"
}

proc run_h1 {} {
    global check_H1 
    puts "this loop is for H1\n"
}
proc run_h2 {} {
    global check_H2
    puts "this loop is for H2\n"
}

当运行脚本时,感觉很多事情并不像我想的那样工作。我更改了一些内容,并从按钮命令中删除了变量,因为变量被传递到命令,以便在开始时而不是在按下按钮时运行

我还删除了所有的
set check\u HX 1
,因为它们将check按钮强制为单一状态。也就是说,我没有接触任何其他功能

package require Tk

frame .top -width 50 -height 30 -borderwidth 5 -padx 5 -pady 5 -relief raised
checkbutton .top.c1 -text H1 -variable "check_H1" -command {set_h1}
checkbutton .top.c2 -text H2 -variable "check_H2" -command {set_h2} 
checkbutton .top.c3 -text H3 -variable "check_H3" -command {set_h3}
checkbutton .top.c4 -text H4 -variable "check_H4" -command {set_h4}
checkbutton .top.c5 -text H5 -variable "check_H5" -command {set_h5}
checkbutton .top.c6 -text H6 -variable "check_H6" -command {set_h6}

button .top.b1 -text "RUN" -command select
grid .top
grid .top.c1 -row 2 -column 2 
grid .top.c2 -row 2 -column 3 
grid .top.c3 -row 3 -column 2 
grid .top.c4 -row 3 -column 3 
grid .top.c5 -row 4 -column 2 
grid .top.c6 -row 4 -column 3 
grid .top.b1 -row 5 -column 5

proc select {} {
  global check_H1 check_H2 check_H3 check_H4 check_H5 check_H6
  puts "Value of H1 is $check_H1"
  puts "Value of H2 is $check_H2"
  puts "Value of H3 is $check_H3"
  puts "Value of H4 is $check_H4"
  puts "Value of H5 is $check_H5"
  puts "Value of H6 is $check_H6"
  if {$check_H1 == 1} {run_h1}
  if {$check_H2 == 1} {run_h2}
  if {$check_H3 == 1} {run_h3}
  if {$check_H4 == 1} {run_h4}
  if {$check_H5 == 1} {run_h5}
  if {$check_H6 == 1} {run_h6}
}

proc set_h1 {} {
  global check_H1
  puts "H1 is set to $check_H1"
}

proc set_h2 {} {
  global check_H2
  puts "H2 is set to $check_H2"
}

proc set_h3 {} {
  global check_H3
  puts "H3 is set to $check_H3"
}

proc set_h4 {} {
  global check_H4
  puts "H4 is set to $check_H4"
}

proc set_h5 {} {
  global check_H5
  puts "H5 is set to $check_H5"
}

proc set_h6 {} {
  global check_H6
  puts "H6 is set to $check_H6"
}

proc run_h1 {} {
  global check_H1 
  puts "This loop is for H1 $check_H1"
}

proc run_h2 {} {
  global check_H2
  puts "this loop is for H2 $check_H2"
}

proc run_h3 {} {
  global check_H3
  puts "this loop is for H3 $check_H3"
}

proc run_h4 {} {
  global check_H4
  puts "this loop is for H4 $check_H4"
}

proc run_h5 {} {
  global check_H5
  puts "this loop is for H5 $check_H5"
}

proc run_h6 {} {
  global check_H6
  puts "this loop is for H6 $check_H6"
}

当运行脚本时,感觉很多事情并不像我想的那样工作。我更改了一些内容,并从按钮命令中删除了变量,因为变量被传递到命令,以便在开始时而不是在按下按钮时运行

我还删除了所有的
set check\u HX 1
,因为它们将check按钮强制为单一状态。也就是说,我没有接触任何其他功能

package require Tk

frame .top -width 50 -height 30 -borderwidth 5 -padx 5 -pady 5 -relief raised
checkbutton .top.c1 -text H1 -variable "check_H1" -command {set_h1}
checkbutton .top.c2 -text H2 -variable "check_H2" -command {set_h2} 
checkbutton .top.c3 -text H3 -variable "check_H3" -command {set_h3}
checkbutton .top.c4 -text H4 -variable "check_H4" -command {set_h4}
checkbutton .top.c5 -text H5 -variable "check_H5" -command {set_h5}
checkbutton .top.c6 -text H6 -variable "check_H6" -command {set_h6}

button .top.b1 -text "RUN" -command select
grid .top
grid .top.c1 -row 2 -column 2 
grid .top.c2 -row 2 -column 3 
grid .top.c3 -row 3 -column 2 
grid .top.c4 -row 3 -column 3 
grid .top.c5 -row 4 -column 2 
grid .top.c6 -row 4 -column 3 
grid .top.b1 -row 5 -column 5

proc select {} {
  global check_H1 check_H2 check_H3 check_H4 check_H5 check_H6
  puts "Value of H1 is $check_H1"
  puts "Value of H2 is $check_H2"
  puts "Value of H3 is $check_H3"
  puts "Value of H4 is $check_H4"
  puts "Value of H5 is $check_H5"
  puts "Value of H6 is $check_H6"
  if {$check_H1 == 1} {run_h1}
  if {$check_H2 == 1} {run_h2}
  if {$check_H3 == 1} {run_h3}
  if {$check_H4 == 1} {run_h4}
  if {$check_H5 == 1} {run_h5}
  if {$check_H6 == 1} {run_h6}
}

proc set_h1 {} {
  global check_H1
  puts "H1 is set to $check_H1"
}

proc set_h2 {} {
  global check_H2
  puts "H2 is set to $check_H2"
}

proc set_h3 {} {
  global check_H3
  puts "H3 is set to $check_H3"
}

proc set_h4 {} {
  global check_H4
  puts "H4 is set to $check_H4"
}

proc set_h5 {} {
  global check_H5
  puts "H5 is set to $check_H5"
}

proc set_h6 {} {
  global check_H6
  puts "H6 is set to $check_H6"
}

proc run_h1 {} {
  global check_H1 
  puts "This loop is for H1 $check_H1"
}

proc run_h2 {} {
  global check_H2
  puts "this loop is for H2 $check_H2"
}

proc run_h3 {} {
  global check_H3
  puts "this loop is for H3 $check_H3"
}

proc run_h4 {} {
  global check_H4
  puts "this loop is for H4 $check_H4"
}

proc run_h5 {} {
  global check_H5
  puts "this loop is for H5 $check_H5"
}

proc run_h6 {} {
  global check_H6
  puts "this loop is for H6 $check_H6"
}

select显示的每个变量的表观值固定为按钮按下时的变量值。创建了top.b1。此外,您不需要编写回调来设置checkbutton变量的值。变量将被更新以反映按钮的状态。(除非你想这样做:使按钮无法取消选择。在这种情况下,最好禁用按钮。)为什么所有的
设置
*过程都不同?@Danish,因为你使用
列表
命令来包含对select proc的调用,执行该按钮命令时,所有变量都将被替换。正如Jerry所演示的,只需调用select而不使用参数,并在select过程中将所有变量声明为全局变量。select看到的每个变量的表观值固定为按钮按下时的变量值。创建了top.b1。此外,您不需要编写回调来设置checkbutton变量的值。变量将被更新以反映按钮的状态。(除非你想这样做:使按钮无法取消选择。在这种情况下,最好禁用按钮。)为什么所有的
设置
*过程都不同?@Danish,因为你使用
列表
命令来包含对select proc的调用,执行该按钮命令时,所有变量都将被替换。正如Jerry演示的那样,只需在没有参数的情况下调用select,并在select proc中将所有变量声明为全局变量。看来主要的更改是运行按钮和select proc,是吗?是的,感谢Jerry和Glenn,我最近意识到列表不再是必需的,我能够运行的更改很少,我知道我不需要,所以我可以设置*proc,但是我可以删除checkbutton的-command选项吗?如果是,那么我不需要这些,所以我可以设置*proc,我只能查询variable@glennjackman是的,这些和过程中设置的干扰
。@DanishSheikh是的,您不需要过程来设置变量。复选按钮用于在单击时更改变量的值。看起来主要的更改是运行按钮和选择程序,是吗?是的,谢谢Jerry和Glenn,我最近意识到不再需要列表,并且我能够运行的更改很少,我明白我不需要,所以可以设置程序,但是,我可以删除checkbutton的-command选项吗?如果是,那么我不需要这些选项,因此可以设置*procs,我只能查询variable@glennjackman是的,这些和过程中设置的干扰
。@DanishSheikh是的,您不需要过程来设置变量。复选按钮用于在单击时更改其变量的值。