Tcl / Tk:使用带expr的数组(Tcl/Tk: using array with expr)

编程入门 行业动态 更新时间:2024-10-25 11:27:54
Tcl / Tk:使用带expr的数组(Tcl/Tk: using array with expr)

我在尝试访问expr array时遇到了问题。 下面显示了重现错误的代码,该代码改编自Donal Fellows回答我之前的问题 。

namespace eval Ns {
}

proc Ns::main_routine {} {
  global cb

  array set cb {
    c1 0
    c2 0
    c3 0
    c4 0
  }

  checkbutton .c1 -text "C1" -variable     cb(c1)
  checkbutton .c2 -text "C2" -variable     cb(c2)
  checkbutton .c3 -text "C3" -variable     cb(c3)
  checkbutton .c4 -text "C4" -variable     cb(c4)

  grid .c1 -sticky w
  grid .c2 -sticky w
  grid .c3 -sticky w
  grid .c4 -sticky w

  # _After_ initializing the state...
  trace add variable cb(c1) write Ns::reconfigureButtons
  trace add variable cb(c2) write Ns::reconfigureButtons
  trace add variable cb(c3) write Ns::reconfigureButtons
  trace add variable cb(c4) write Ns::reconfigureButtons
}

proc Ns::reconfigureButtons args {
  global cb

  # this one works
  set state "normal"
  if { $cb(c1) } {
    set state "disabled"
  }
  .c2 configure -state $state

  # this one does not
  #.c2 configure -state [expr $cb(c1) ? "disabled" : "normal"]
  #.c4 configure -state [expr $cb(c1)||$cb(c3) ? "disabled" : "normal"]
}

Ns::main_routine
 

我想在上面的代码中修复以下行

.c2 configure -state [expr $cb(c1) ? "disabled" : "normal"]

当我使用上面的行时,我收到以下错误:

can't set "cb(c1)": invalid bareword "disabled" in expression "1? disabled : normal";

I am running into problems while trying to access array inside an expr. The code which reproduces the error is shown below which is adapted from Donal Fellows answer to my earlier question.

namespace eval Ns {
}

proc Ns::main_routine {} {
  global cb

  array set cb {
    c1 0
    c2 0
    c3 0
    c4 0
  }

  checkbutton .c1 -text "C1" -variable     cb(c1)
  checkbutton .c2 -text "C2" -variable     cb(c2)
  checkbutton .c3 -text "C3" -variable     cb(c3)
  checkbutton .c4 -text "C4" -variable     cb(c4)

  grid .c1 -sticky w
  grid .c2 -sticky w
  grid .c3 -sticky w
  grid .c4 -sticky w

  # _After_ initializing the state...
  trace add variable cb(c1) write Ns::reconfigureButtons
  trace add variable cb(c2) write Ns::reconfigureButtons
  trace add variable cb(c3) write Ns::reconfigureButtons
  trace add variable cb(c4) write Ns::reconfigureButtons
}

proc Ns::reconfigureButtons args {
  global cb

  # this one works
  set state "normal"
  if { $cb(c1) } {
    set state "disabled"
  }
  .c2 configure -state $state

  # this one does not
  #.c2 configure -state [expr $cb(c1) ? "disabled" : "normal"]
  #.c4 configure -state [expr $cb(c1)||$cb(c3) ? "disabled" : "normal"]
}

Ns::main_routine
 

I want to fix the following line in the above code

.c2 configure -state [expr $cb(c1) ? "disabled" : "normal"]

When I use the above line I get the following error:

can't set "cb(c1)": invalid bareword "disabled" in expression "1? disabled : normal";

最满意答案

你应该总是把{ braces }放在表达式周围,因为它会停止双重替换,留下"引号"来达到正确的目的。 通过这样做,您还可以提高代码的安全性速度,至少从Tcl 8.0开始(十多年前); 在此之前,性能原因意味着许多人省略了括号,但那是一个非常糟糕的习惯。

总之,不要使用:

.c2 configure -state [expr $cb(c1) ? "disabled" : "normal" ]

而是使用这个:

.c2 configure -state [expr {$cb(c1) ? "disabled" : "normal"}]

You should always put {braces} around expressions, as that stops double substitution, leaving the "quotes" to do their proper purpose. You also boost the security and speed of your code by doing that, and have done since at least Tcl 8.0 (over a decade ago); before that, performance reasons meant that many people omitted braces, but that was a truly terrible habit.

In short, don't use:

.c2 configure -state [expr $cb(c1) ? "disabled" : "normal" ]

But rather use this:

.c2 configure -state [expr {$cb(c1) ? "disabled" : "normal"}]

更多推荐

本文发布于:2023-07-16 07:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125516.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   Tk   Tcl   array   expr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!