0
votes

I am beginner with running a tcl/tk script. In my script i have created a popup window to select a file to open and then this file path is given to the source function. I was expecting this script to run stepwise but instead source function is running before i select any file. I also tried using vwait function. Unfortunately it is not running in the 1st run. But in the 2nd run script is working as desire. Can anybody help me to run this script?

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"
							
							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"
1
Are you getting any errors?Adam
@Adam No but the script is not running as i was expecting it to run. I was expecting that first script will ask me to select a file and after i select a file, the selected file will be considered as a source. But here source and the pop up window are running at the same time. That i do not want.S.Gajjar

1 Answers

0
votes

Looks like you are missing the idea of event-driving programming in Tk. Lets try to find out what is going on in your script. When you run it, the only things are should be done: construct window with widgets for user and bind scripts to widgets events. That is all. After that program is doing nothing but waiting for users action. The command that you bind to a button does not evaluated instantly.

In you case, all work with selected file should be after user have chose it. You should run file reading from button's command. Try to run this script with 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