2017-08-10 3 views
0

私は最終的にデータベースにデータを配置するために、単純なフォームボックスを作成しようとしています。次のように今私は単純に、puts文でそれをテストしてい:textvariable(エントリウィジェット)から値を取得

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

を使用しますが、入力フィールドにテキストを入力するまで、変数は存在しません。

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