2017-04-10 6 views
0

私はtcl/tkスクリプトを実行する初心者です。私のスクリプトでは、開くファイルを選択するためのポップアップウィンドウを作成してから、このファイルパスをソース関数に渡しています。私はこのスクリプトが段階的に実行されることを期待していましたが、代わりにソース関数が実行されてからファイルを選択します。また、vwait関数を使ってみました。残念ながら、それは1回目の実行では実行されていません。しかし、2回目のスクリプトでは、スクリプトが欲望として働いています。誰も私にこのスクリプトを実行するのを助けることができますか?tcl/tkスクリプトが希望どおりに動作しない

destroy .buttons 
 
toplevel .buttons -width 400 -height 100 -background red -relief ridge -borderwidth 8 -padx 10 -pady 10 
 
wm title .buttons "Select a file containing nodes coordinates" 
 
wm geometry .buttons 350x81 
 

 
set count 0 
 
proc add_button {title command} { 
 
    global count 
 
    button .buttons.$count -text $title -command $command 
 
    pack .buttons.$count -side top -pady 1 -padx 1 -fill x 
 
    incr count 
 
} 
 

 
set types { {{TCL Scripts} {.tcl}} } 
 
add_button "File name"  {set accept_button [tk_getOpenFile -filetypes $types] 
 
\t \t \t \t \t \t \t puts "the path is: $accept_button" 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t destroy .buttons} 
 

 
add_button "Exit"  {destroy .buttons} 
 
#puts above------------------------ 
 
#vwait [namespace which -variable accept_button] 
 
#puts below----------------------- 
 

 
source "$accept_button" 
 
puts "the src is: $accept_button"

+0

エラーが表示されますか? – Adam

+0

@Adamいいえ、実行する予定であるため、スクリプトは実行されていません。最初のスクリプトでファイルを選択するように求められ、ファイルを選択すると、選択したファイルがソースとみなされます。しかし、ここではソースとポップアップウィンドウが同時に実行されています。それは私が望んでいない。 –

答えて

0

あなたはTkでイベント駆動プログラミングの考え方が欠落しているように見えます。 あなたのスクリプトで何が起こっているのか調べてみましょう。あなたがそれを実行するときには、唯一のことが行われなければなりません:ユーザのためのウィジェットを備えたウィンドウを構築し、ウィジェットのイベントにスクリプトをバインドします。それだけです。そのプログラムの後には何もしませんが、ユーザーの行動を待っています。ボタンにバインドするコマンドは即座に評価されません。

選択したファイルのすべての作業は、ユーザーが選択した後でなければなりません。ボタンのコマンドからファイルの読み込みを実行する必要があります。このスクリプトをtclshで実行してみてください

package require Tk 
destroy .buttons 
toplevel .buttons -width 400 -height 100 -background red -relief ridge -borderwidth 8 -padx 10 -pady 10 
wm title .buttons "Select a file containing nodes coordinates" 
wm geometry .buttons 350x81 

set count 0 
proc add_button {title command} { 
    global count 
    button .buttons.$count -text $title -command $command 
    pack .buttons.$count -side top -pady 1 -padx 1 -fill x 
    incr count 
} 

set types { {{TCL Scripts} {.tcl}} } 
add_button "File name"  {set accept_button [tk_getOpenFile -filetypes $types] 
                 puts "the path is: $accept_button" 
what_program_should_do_after_file_is_chosen $accept_button 

                 destroy .buttons} 
add_button "Exit"  {destroy .buttons} 

proc what_program_should_do_after_file_is_chosen {path} { 
puts "You've chose file: $path" 
} 
vwait forever 
関連する問題