tk_chooseDirectory(3tk)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– tk_chooseDirectory β€” the manual, but make it make sense πŸ’–

okay bestie, so you're out here in Tk land and you need the user to pick a directory. Not a file. A whole entire folder. And tk_chooseDirectory is the girl for the job. She's part of the Tk Built-In Commands family, she's been around since forever (Tk 8.3 era, a true vintage icon), and she just wants to pop a lil dialog on screen and let your human choose. πŸŽ€


✨ NAME ✨

tk_chooseDirectory β€” pops up a dialog box for the user to select a directory.

like. that's it. that's the whole gig. she pops up, they pick a folder, everybody goes home. πŸ’…


🌸 SYNOPSIS 🌸

tk_chooseDirectory ?option value ...?

so you call her with a bunch of option value pairs, and the ? means they're optional β€” you can literally just yell tk_chooseDirectory into the void and she'll still show up. periodt.


πŸ¦‹ DESCRIPTION πŸ¦‹

The procedure tk_chooseDirectory pops up a dialog box for the user to select a directory. And ngl, she's customizable β€” here are all the option-value pairs you can hand her as command line arguments:

  • -command string β€” Specifies the prefix of a Tcl command to invoke when the user closes the dialog after having selected an item. Important vibe check: this callback is not called if the user cancelled the dialog. The actual command consists of string followed by a space and the value selected by the user in the dialog. And heads up 🫢 β€” this is only available on macOS.
  • -initialdir dirname β€” Specifies that the directories in dirname should be displayed when the dialog pops up. If this parameter is not specified, the initial directory defaults to the current working directory on non-Windows systems, and on Windows systems prior to Vista. On Vista and later systems, the initial directory defaults to the last user-selected directory for the application. And if the parameter specifies a relative path, the return value will convert the relative path to an absolute path. she said no relative nonsense in MY return value. 😌
  • -message string β€” Specifies a message to include in the client area of the dialog. So you can leave a lil note for your user. Cute! But β€” this is only available on macOS.
  • -mustexist boolean β€” Specifies whether the user may specify non-existent directories. If this parameter is true, then the user may only select directories that already exist. The default value is false. so by default she's like "sure babe, type whatever, live your truth" but flip it to true and she becomes a strict queen. πŸ‘‘
  • -parent window β€” Makes window the logical parent of the dialog. The dialog is displayed on top of its parent window. On macOS, this turns the file dialog into a sheet attached to the parent window. the way it just attaches... iconic behavior fr fr.
  • -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. so name your dialog or don't, she'll cope either way. πŸ’œ
🫢 quick heads up bestie: -command and -message are macOS-only, so if you're writing cross-platform code, don't be out here acting surprised on Linux. iykyk.

🌈 EXAMPLE 🌈

here's the whole vibe in action β€” grab a directory starting at your home folder, with a cute title, then react to whether they picked something or not:

set dir [tk_chooseDirectory \
        -initialdir ~ -title "Choose a directory"]
if {$dir eq ""} {
   label .l -text "No directory selected"
} else {
   label .l -text "Selected $dir"
}

see how if $dir comes back as the empty string "" that means they bailed? and if it's not empty you got yourself a real path. chef's kiss. 😭✨


πŸ‘€ SEE ALSO πŸ‘€

her sisters!! go say hi.


⭐ KEYWORDS ⭐

directory, selection, dialog, platform-specific


and that's the whole page, bestie!! this lil legend lives in the Tk manual as tk_chooseDirectory(3tk), sourced from Tk 8.3 β€” the Tk Built-In Commands corner of the world. no cap, she's been popping up dialogs since before you were born and she's still slaying. go pick a directory. πŸŽ€πŸ’–πŸŒΈπŸ¦‹

πŸ“¦ where this page comes from