file and directory selector with gui for tcl – tk

Quick Code: Here is a sample file and directory selector procedure written in tcl – tk. The code is added in my public git repo at git://github.com/rvguiman/tcl-tk.git or https://github.com/rvguiman/tcl-tk.git. Or check the code below.

#-------------------------- gFileDirSelector ------------------------------------------------#
# what: procedure for selecting a file or directory, run it using wish/tk
# arg: optional arg type, valid values none/anything for file selection or "dir" if you want to choose directory instead of file
# return: if type is file, return the full path of file or 0 if cancelled
#--------------------------------------------------------------------------------------------#

proc gFileDirSelector { { type "file" } } {
     if { $type ne "dir" } {
          set types {
          {{text files} {.txt} TEXT}
          {{tcl scripts} {.tcl} }
          {{all files} * }
          }
          set filename [tk_getOpenFile -filetypes $types]
          if {$filename ne ""} {
               puts "file selected -> $filename"
               return $filename
               } else {
               puts "file selection cancelled"
               return 0
               }
          } else {
          set dirSel [tk_chooseDirectory]
          if {$dirSel ne ""} {
              puts "dir selected -> $dirSel"
              return $dirSel
              } else {
              puts "directory selection cancelled"
              return 0
              }
          }
     }


Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.