Updated 2015-02-20 17:58:50 by Busirane

pops up a dialog box for the user to select a directory.



tk_chooseDirectory ?option value ...?


The procedure tk_chooseDirectory pops up a dialog box for the user to select a directory. The following option-value pairs are possible as command line arguments:

-initialdir dirname

Specifies that the directories in directory should be displayed when the dialog pops up. If this parameter is not specified, then the directories in the current working directory are displayed. If the parameter specifies a relative path, the return value will convert the relative path to an absolute path. This option may not always work on the Macintosh. This is not a bug. Rather, the General Controls control panel on the Mac allows the end user to override the application default directory.

-parent window

Makes window the logical parent of the dialog. The dialog is displayed on top of its parent window.

-title titleString

Specifies a string to display as the title of the dialog box. If this option is not specified, then a default title will be displayed.

-mustexist boolean

Specifies whether the user may specify non-existant directories. If this parameter is true, then the user may only select directories that already exist. The default value is false.


tk_getOpenFile, tk_getSaveFile

See also: chooseDirectory - select

No help from TclHelp on this (it's not in the index) - but we can get some help from Tk itself, by making a deliberate mistake in an interactive wish:
 % tk_chooseDirectory -help
 bad option "-help": must be -initialdir, -mustexist, -parent, or -title

Pops up a directory chooser (toplevel window). On Windows, using an old API - tree widget for a drive, and a drive selector below. On Unix (Tk 8.3), a selection widget (without drive selector).

Pauses execution until 'OK' or 'Cancel' have been selected. Returns the chosen pathname with canonical separators (slashes), or an empty string if canceled. Will on Windows not accept selection of mystic pathnames like

See http://www.purl.org/net/TclTkProgRef for Chris Nelson's tk_chooseDirectory written in pure Tcl.

A little more of a note on this util script: On Microsoft Windows 95 the tk_chooseDirectory fails. Apparently, a helper file or system dll is broken. Therefore, this script comes in real handy. It should also be noted that the tk_chooseDirectory function that comes with tcl/tk distribution is Windows only. So this file will be very helpful on your *nix or other platform computers. Another consideration should be taken with Windows 95. This script does not handle mapped drives. You may have to add that functionality yourself.

Note: tk_chooseDirectory was added to the UNIX Tk distribution in, I think, 8.3; it is an adaptation of the script in my site. -- CLN

Are there issues when choosing new vs existing directories?

Does this routine support the case where one wants to select a subdirectory of a not-yet existing directory?

MG Jan 21 2005 - In many apps on Windows that use a dialog similar to tk_chooseDirectory's, they have a button to create a new directory, when that's allowed. Apart from the addition of that button, though, it looks identical to tk_chooseDirectory; is this the same dialog from the MS Windows API? And if so, would it be possible to alter tk_chooseDirectory so that it shows the 'New' button, when -mustexist is false?

SEH -- 1/21/05 -- Is there a way to call tk_chooseDirectory explicitly on Windows in a manner equivalent to [::tk::dialog::file::] for tk_getOpenFile?

JJS 20150207 There seems to be an inconsistency between how -initialdir is documented to work and how it actually works, at least with Tclkit 8.5.17 under Windows 7. The man page says it "Specifies that the directories in directory should be displayed when the dialog pops up", but it actually displays directory and its siblings; you have to open directory to see its contents. And then it has the annoying behavior that when you open a directory, it positions it at the lowest point in the window so that you can't actually see its contents without scrolling. This means that digging for deeply buried directory means "open, scroll, open, scroll..." ad nauseam. Are these behaviors normal?

Update: I found ::tk::dialog::file::chooseDir::, but it has an odd bug that took me a while to track down. When you select a directory and click OK, it returns the path to the selected directory's parent, not the directory itself. This means that in order to select a directory, you have to open it and then click OK, which is not intuitive. The code comments say it's supposed to work the way I expected it to work, but it doesn't because the IconList widget clears its selection when it loses focus, e.g., when you click OK!. My workaround for this is to redefine its FocusOut procedure as follows:
 source [file join $::tk_library tkfbox.tcl]
 proc ::tk::IconList_FocusOut {w} {}

You can do this in your code, right before you call ::tk::dialog::file::chooseDir::. A more thorough implementation might save the old procedure and restore it afterward.