2017-08-10 44 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