什么存储在那个Tcl变量中?

时间:2008-12-24 22:10:21

标签: tcl

根据this question中给出的建议,我写了一些gui来获取命令行C程序的选项,并将它们传递给已经设置为处理它们的C程序。它显示就像我想要的那样。

但是,我想验证存储在变量中的值是否正确。 获取打印出的值让我感到非常悲伤(由于某些硬件问题,我现在无法测试 in vivo )。我错过了什么?

  1. 使用'$'添加变量名称会给我'$ variableName'而不是变量的
  2. 将这些变量添加到数组并调用array get arr应该打印索引和数组值;我得到了变量名。
  3. 我尝试了 pathName cget 选项,但显然-value不是一个选项,并且不让我选择有效选项列表。
  4. 以下是所有代码中包含各种不起作用的代码(来自选项#1,这是最直接的方式;其他代码只是我尝试解决方法)。他们都生产 错误沿线:“无法读取”::“:没有这样的变量”或“无法读取” “比色”:没有这样的变量“。

    #!/opt/ActiveTcl-8.5/bin/wish8.5
    
    wm title . "Gretag"
    
    ttk::frame .f -borderwidth 5 -relief sunken -padding "5 10"
    
    # next line part of the "puts" tests at the bottom
    global colorimetric
    ttk::label .f.dataLabel -text "Data Type"
    ttk::label .f.colorimetricLabel -text "Colorimetric"
    ttk::checkbutton .f.colorimetric -onvalue "-c" -offvalue "" -command getFilename1
    ttk::label .f.spectralLabel -text "Spectral"
    ttk::checkbutton .f.spectral -onvalue "-s" -offvalue "" -command getFilename2 
    
    ttk::label .f.gretagNumLabel -text "Gretag #"
    ttk::label .f.gretagLabel0 -text "1"
    ttk::radiobutton .f.gretagRadio0 -variable gretagNum -value "/dev/ttyS0" 
    ttk::label .f.gretagLabel1 -text "2"
    ttk::radiobutton .f.gretagRadio1 -variable gretagNum -value "/dev/ttyS1"
    ttk::label .f.gretagLabel2 -text "3"
    ttk::radiobutton .f.gretagRadio2 -variable gretagNum -value "/dev/ttyS2" 
    ttk::label .f.gretagLabel3 -text "4"
    ttk::radiobutton .f.gretagRadio3 -variable gretagNum -value "/dev/ttyS3" 
    ttk::label .f.gretagLabel4 -text "5"
    ttk::radiobutton .f.gretagRadio4 -variable gretagNum -value "/dev/ttyS4" 
    
    ttk::label .f.sampleSize -text "Sample Size"
    ttk::label .f.samplex -text "X"
    ttk::label .f.sampley -text "Y"
    ttk::entry .f.x -textvariable x -width 5 
    ttk::entry .f.y -textvariable y -width 5 
    
    ttk::label .f.filterLabel -text "Filter Type"
    ttk::label .f.filterLabel0 -text "D50"
    ttk::radiobutton .f.filterRadio0 -variable filter -value "-d50" 
    ttk::label .f.filterLabel1 -text "D65"
    ttk::radiobutton .f.filterRadio1 -variable filter -value "-d65" 
    ttk::label .f.filterLabel2 -text "Unfiltered"
    ttk::radiobutton .f.filterRadio2 -variable filter -value "-U" 
    ttk::label .f.filterLabel3 -text "Polarized"
    ttk::radiobutton .f.filterRadio3 -variable filter -value "-p" 
    
    ttk::label .f.baudLabel -text "Baud Rate"
    ttk::label .f.baudLabel0 -text "4800"
    ttk::radiobutton .f.baudRadio0 -variable baud -value "B4800" 
    ttk::label .f.baudLabel1 -text "9600"
    ttk::radiobutton .f.baudRadio1 -variable baud -value "B9600" 
    ttk::label .f.baudLabel2 -text "19200"
    ttk::radiobutton .f.baudRadio2 -variable baud -value "B19200" 
    ttk::label .f.baudLabel3 -text "38400"
    ttk::radiobutton .f.baudRadio3 -variable baud -value "B38400" 
    ttk::label .f.baudLabel4 -text "57600"
    ttk::radiobutton .f.baudRadio4 -variable baud -value "B57600" 
    
    ttk::button .f.submitBtn -text "Submit" -command finish
    
    grid columnconfigure . 0 -weight 1
    grid rowconfigure . 0 -weight 1
    grid .f -column 0 -row 0 -columnspan 11 -rowspan 5
    
    grid .f.dataLabel -column 0 -row 0 -sticky we
    grid .f.colorimetricLabel -column 1 -row 0 -sticky e
    grid .f.colorimetric -column 2 -row 0 -sticky w
    grid .f.spectralLabel -column 3 -row 0 -sticky e
    grid .f.spectral -column 4 -row 0 -sticky w
    
    grid .f.gretagNumLabel -column 0 -row 1 -sticky we
    grid .f.gretagLabel0 -column 1 -row 1 -sticky e
    grid .f.gretagRadio0 -column 2 -row 1 -sticky w
    grid .f.gretagLabel1 -column 3 -row 1 -sticky e
    grid .f.gretagRadio1 -column 4 -row 1 -sticky w
    grid .f.gretagLabel2 -column 5 -row 1 -sticky e
    grid .f.gretagRadio2 -column 6 -row 1 -sticky w
    grid .f.gretagLabel3 -column 7 -row 1 -sticky e
    grid .f.gretagRadio3 -column 8 -row 1 -sticky w
    grid .f.gretagLabel4 -column 9 -row 1 -sticky e
    grid .f.gretagRadio4 -column 10 -row 1 -sticky w
    
    grid .f.sampleSize -column 0 -row 2 -sticky we
    grid .f.samplex -column 1 -row 2 -sticky e
    grid .f.x -column 2 -row 2 -sticky w
    grid .f.sampley -column 3 -row 2 -sticky e
    grid .f.y -column 4 -row 2 -sticky w
    
    grid .f.filterLabel -column 0 -row 3 -sticky we
    grid .f.filterLabel0 -column 1 -row 3 -sticky e
    grid .f.filterRadio0 -column 2 -row 3 -sticky w
    grid .f.filterLabel1 -column 3 -row 3 -sticky e
    grid .f.filterRadio1 -column 4 -row 3 -sticky w
    grid .f.filterLabel2 -column 5 -row 3 -sticky e
    grid .f.filterRadio2 -column 6 -row 3 -sticky w
    grid .f.filterLabel3 -column 7 -row 3 -sticky e
    grid .f.filterRadio3 -column 8 -row 3 -sticky w
    
    grid .f.baudLabel -column 0 -row 4 -sticky we
    grid .f.baudLabel0 -column 1 -row 4 -sticky e
    grid .f.baudRadio0 -column 2 -row 4 -sticky w
    grid .f.baudLabel1 -column 3 -row 4 -sticky e
    grid .f.baudRadio1 -column 4 -row 4 -sticky w
    grid .f.baudLabel2 -column 5 -row 4 -sticky e
    grid .f.baudRadio2 -column 6 -row 4 -sticky w
    grid .f.baudLabel3 -column 7 -row 4 -sticky e
    grid .f.baudRadio3 -column 8 -row 4 -sticky w
    grid .f.baudLabel4 -column 9 -row 4 -sticky e
    grid .f.baudRadio4 -column 10 -row 4 -sticky w
    
    grid .f.submitBtn -column 1 -row 5 -columnspan 7 -sticky we
    
    foreach w [winfo children .f] {grid configure $w -padx 5 -pady 5}
    focus .f.colorimetric
    .f.colorimetric state selected
    .f.filterRadio1 state selected
    .f.baudRadio1 state selected
    bind . <Return> {finish}
    
    proc getFilename1 {} {
    set filename1 [tk_getSaveFile]
    }
    
    proc getFilename2 {} {
    set filename2 [tk_getSaveFile]
    }
    
    proc finish {} {
    .f.x insert 0 "-x"
    .f.y insert 0 "-y"
    # Pick one
    # puts $colorimetric
    # puts colorimetric
    # puts "$colorimetric"
    # puts $::colorimetric
    # puts .f.colorimetric
    # puts $.f.colorimetric
    # puts $::.f.colorimetric
    # puts "$::colorimetric"
    exec ./gretag .f.colorimetric filename1 .f.spectral filename2 .f.gretagNum .f.x .f.y .f.filter .f.baud
    }
    

    修改 我已经发布了所有代码而不仅仅是part,并且在倒数第二行中是来自选项#1的各种语法,我尝试过这些语法,以便在它们传递给下一个程序之前查看变量的值。这些都不起作用,我不明白为什么或如何解决它。我希望另一双眼睛会发现错误。

6 个答案:

答案 0 :(得分:5)

答案 1 :(得分:2)

您尚未将任何变量分配给色度检查按钮。 将 - 变量色度添加到检查按钮,然后完成 您可以使用: put $ :: colorimetric

另外,首先设置:: colorimetric以选择默认值。那 比试图弄乱小部件的状态更容易。

我看到colorimetric的值可以是“”和“-c” 我假设您将在exec行中使用该值。 请注意[exec something yada $ :: colorimetric something]会 那可能不行。你可能需要{*} $ :: colorimetric exec行使参数消失,如果它是空的。

答案 2 :(得分:0)

这是一个小tcl片段 - 从tclsh或wish

运行
[nigel@rose ~]$ wish
% set foo /dev/ttys0
/dev/ttys0
% puts $foo
/dev/ttys0
% puts "$foo"
/dev/ttys0
% puts [$foo]
invalid command name "/dev/ttys0"
% puts ($foo)
(/dev/ttys0)
% puts {$foo}
$foo
% 

在Tcl中引用:

  • ""(双引号):评估替换($ variable)

  • {} {Squiggly bracket):将整个字符串视为没有替换的文字

  • [](方括号):执行字符串作为带替换的命令

作为替代方案,您可以在对话框中弹出诊断信息:

% set mb [tk_messageBox -message "Port is $foo" -type ok -icon info]
ok
% 

答案 3 :(得分:0)

在您的“#Where here”评论中,尝试添加

放置$ :: gretagNum

::表示全局变量,以及窗口小部件的-variable选项 永远是全球性的。

答案 4 :(得分:0)

我不确定你想做什么,如果你结束打印变量名而不是变量内容,请使用设置命令作为功能

设置内容“hello”

设置blah内容

如果你这样做:

puts $blah 

..你得到了blah变量的内容,这是内容。

要通过blah获取变量内容的内容,请使用以下命令:

puts [set $blah]

答案 5 :(得分:0)

记住这条规则,你将永远做对:

$foo is shorthand for [set foo]

尝试使用[set foo]重写代码,它会起作用。

特别是

$$foo  ;# not what you think

替换为

[set [set foo]]   ;# does what you think