Selecting a directory and file

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.

How to do it…

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.

How to do it…

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

How it works…

Based on the option value pairs provided, Tk has displayed a tk_getOpenFile with the file types desired, as in a previous example. On completion, the return value was assigned to a named variable and by utilizing the file command, we were able to isolate the directory at the same time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.26.221