Tcl/Tk:使用带有 expr 的数组

发布于 2024-11-28 15:06:14 字数 1572 浏览 1 评论 0原文

我在尝试访问 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 :正常”;

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";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

才能让你更想念 2024-12-05 15:06:14

您应该始终在表达式周围放置 { 大括号},因为这样会停止双重替换,而将 "引号" 保留为做他们正确的目的。通过这样做,您还可以提高代码的安全性和速度,并且至少从 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"}]
森林散布 2024-12-05 15:06:14

你尝试过吗
.c2 配置-状态 [expr {$cb(c1) ? “禁用”:“正常”}]

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文