2017-04-10 91 views
0

我是初學者,運行tcl/tk腳本。在我的腳本中,我創建了一個彈出窗口來選擇要打開的文件,然後將此文件路徑提供給源函數。我期待這個腳本逐步運行,但是在我選擇任何文件之前,源代碼函數正在運行。我也嘗試使用vwait函數。不幸的是,它在第​​一次運行中沒有運行。但在第二次運行中,腳本正在按照願望工作。任何人都可以幫我運行這個腳本嗎?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

看起來你缺少傳統知識的事件驅動編程的想法。 讓我們試着找出腳本中發生了什麼。當你運行它時,唯一應該做的事情是:爲用戶構建帶有小部件的窗口並將腳本綁定到小部件事件。就這些。之後,該程序什麼都不做,只是等待用戶操作。您綁定到按鈕的命令不會立即進行評估。

在你的情況下,所有使用選定文件的工作都應該在用戶選擇之後進行。您應該從按鈕的命令運行文件讀取。嘗試使用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