Dhruv Paul
Dhruv Paul

Reputation: 47

How to create a browse button to select a folder in tcl/tk

How can I select a folder/file using a browse button. I want to know just what to put in the -command of the button?

Upvotes: 1

Views: 2601

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

To ask the user to choose a file to open, you call tk_getOpenFile. That invokes the system dialog box on some platforms, and an internally-defined dialog on others. It returns the fully-qualified filename. (There's also tk_getSaveFile for when you're doing a Save As… action.)

The equivalent for selecting a directory is tk_chooseDirectory.

Here's an example of using it:

button .b -text "Click me!" -command {
    set theFile [tk_getOpenFile]
    puts "You chose $theFile"
}
pack .b

However, for anything much more complicated than that, lots of community experience says that it is easier to create a helper procedure and put a call to that in the -command option. It's not that you must do so, but it is quicker to code and less error-prone. Easier to test as well.

Upvotes: 5

Related Questions