2017-08-10 45 views
0

我试图创建一个简单的表单框,最终将数据放入数据库中。现在,我只需用puts语句测试,如下所示:从文本变量(条目窗口小部件)获取价值

package require Tk 

wm title . "Add" 
grid [ttk::frame .c -padding "3 3 12 12"] -column 0 -row 0 -sticky nwes 
grid columnconfigure . 0 -weight 1; grid rowconfigure . 0 -weight 1 

grid [ttk::label .c.idlbl -width 7 -text "id"] -column 1 -row 1 -sticky we 
grid [ttk::entry .c.id -width 7 -textvariable id] -column 2 -row 1 -sticky we 

grid [ttk::label .c.txtlbl -text "text"] -column 1 -row 2 -sticky w 
grid [ttk::entry .c.txt -width 7 -textvariable text] -column 2 -row 2 -sticky we 


grid [ttk::button .c.calc -text "Add!" -command db_add] -column 1 -row 3 -sticky w 

foreach w [winfo children .c] {grid configure $w -padx 5 -pady 5} 
focus .c.id 


proc db_add {} { 
    set id $::id 
    set text $::text 
    puts $id 
    puts $text 
} 

我的问题:为什么我需要做set到另一个变量名,这样我才可以做的任何有价值的东西?为什么我不能只做puts $::id

我试过puts expr{$::id},它给出了一个输出如expr{Whatever Text Was Entered},让我不确定为什么expr不会消失。看来我对TCL变量的概念目前非常模糊。

+0

看起来您对调用命令的知识缺乏。 Tcl只有[12条语法规则](https://tcl.tk/man/tcl8.6/TclCmd/Tcl.htm),可以通读它们。 –

+0

你想'puts [expr $ :: id]' –

回答

0

可以使用puts $::id,但直到您输入文本到输入字段中,变量不存在。您可以在创建小部件的同时初始化它们,或者测试它们是否存在:

if {![info exists ::id]} { 
    set ::id {} 
} 
puts \$::id=$::id