2017-04-10 8 views
0

초보자 인 tcl/tk 스크립트를 실행 중입니다. 내 스크립트에서 나는 열 파일을 선택하는 팝업 창을 만든 다음이 파일 경로가 소스 함수에 주어집니다. 나는이 스크립트가 단계적으로 실행되기를 기대했지만 대신 어떤 파일을 선택하기 전에 소스 기능이 실행 중이다. 나는 또한 vwait 함수를 사용하여 시도했다. 불행히도 그것은 첫 번째 실행에서 실행되지 않습니다. 그러나 두 번째 실행 스크립트는 원하는대로 작동합니다. 아무도 내가이 스크립트를 실행할 수 있도록 도와 줄 수 있습니까? 당신의 Tk의 이벤트 구동 프로그램의 아이디어를 누락과 같은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