In the following example, we will see how simple it is to combine the functionality of the tk_chooseDirectory
dialog (which returns only a directory listing) with the tk_getOpenFile
dialog (which returns a full file path) to assign the directory in use and the file selected to separate variables. This will be accomplished by the Tcl file command in combination with the tk_getOpenFile
dialog. I use this to allow a user to select or define a configuration file and default directory without requiring the use of two separate dialogs.
In the following example we display a File Selection dialog box and assign the return value to a named variable as well as strip out the directory and assign this to a second named variable. Enter the following commands:
1 % set response [tk_getOpenFile -filetypes $types]
You should now see the following dialog displayed.
Note that in your shell window the input line is no longer active. This is due to the fact that the application (in this case the wish shell) is awaiting a response. Select a file and click on the Open button and you will notice that the full path for the file selected is displayed. Enter the following command to assign the directory path to the second named variable:
2 % set my_directory [file dirname $response]
C:/Documents and Settings/Bert
3 % puts "$response $my_directory"
C:/Documents and Settings/Bert/break.tcl C:/Documents and Settings/Bert
3.15.26.221