tkFileDialog

If you want to open or save a file or to choose a directory using a filedialog you dont need to implement it on your own. The module tkFileDialog is just for you. In most cases the seven convenience functions provided by the module will serve your needs.

Functions

First you have to decide if you want to open a file or just want to get a filename in order to open the file on your own. In the first case you should use tkFileDialog.askopenfile() in the latter case tkFileDialog.askopenfilename(). Both functions come in a multiple file version with just the same parameters as the single file version which allow the user to select multiple files.

The multiple file versions return a list of open files or a list of file names. If no files were selected or the cancle button was pressed an empty list is returned.

Saving files works in a similar way. You also have two variants of the function, one to get an opened file which you can use to save your data and another to get a file name in order to open the file on your own. These functions are only provided in the single file version. A multiple file version would make no sense.

single file

multiple files

available options

open a file

askopenfile(mode='r', **options)

askopenfiles(mode='r', **options)

defaultextension, filetypes, initialdir, initialfile, multiple, message, parent, title

get a filename to open

askopenfilename(**options)

askopenfilenames(**options)

defaultextension, filetypes, initialdir, initialfile, multiple, message, parent, title

save a file

asksaveasfile(mode='w', **options)

n/a

defaultextension, filetypes, initialdir, initialfile, multiple, message, parent, title

get a filename to save

asksaveasfilename(**options)

n/a

defaultextension, filetypes, initialdir, initialfile, multiple, message, parent, title

choose a directory

askdirectory(**options)

n/a

initialdir, parent, title, mustexist

Options

-defaultextension extension

-filetypes filePatternList

-initialdir directory

-initialfile filename

-message string

-multiple boolean

-mustexist boolean

-parent window

-title titleString

[source:]

Example

This example just should give you an idea of the modules use.

   1 import Tkinter, Tkconstants, tkFileDialog
   2 
   3 class TkFileDialogExample(Tkinter.Frame):
   4 
   5   def __init__(self, root):
   6 
   7     Tkinter.Frame.__init__(self, root)
   8 
   9     # options for buttons
  10     button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
  11 
  12     # define buttons
  13     Tkinter.Button(self, text='askopenfile', command=self.askopenfile).pack(**button_opt)
  14     Tkinter.Button(self, text='askopenfilename', command=self.askopenfilename).pack(**button_opt)
  15     Tkinter.Button(self, text='asksaveasfile', command=self.asksaveasfile).pack(**button_opt)
  16     Tkinter.Button(self, text='asksaveasfilename', command=self.asksaveasfilename).pack(**button_opt)
  17     Tkinter.Button(self, text='askdirectory', command=self.askdirectory).pack(**button_opt)
  18 
  19     # define options for opening or saving a file
  20     self.file_opt = options = {}
  21     options['defaultextension'] = '.txt'
  22     options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
  23     options['initialdir'] = 'C:\\'
  24     options['initialfile'] = 'myfile.txt'
  25     options['parent'] = root
  26     options['title'] = 'This is a title'
  27 
  28     # This is only available on the Macintosh, and only when Navigation Services are installed.
  29     #options['message'] = 'message'
  30 
  31     # if you use the multiple file version of the module functions this option is set automatically.
  32     #options['multiple'] = 1
  33 
  34     # defining options for opening a directory
  35     self.dir_opt = options = {}
  36     options['initialdir'] = 'C:\\'
  37     options['mustexist'] = False
  38     options['parent'] = root
  39     options['title'] = 'This is a title'
  40 
  41   def askopenfile(self):
  42 
  43     """Returns an opened file in read mode."""
  44 
  45     return tkFileDialog.askopenfile(mode='r', **self.file_opt)
  46 
  47   def askopenfilename(self):
  48 
  49     """Returns an opened file in read mode.
  50     This time the dialog just returns a filename and the file is opened by your own code.
  51     """
  52 
  53     # get filename
  54     filename = tkFileDialog.askopenfilename(**self.file_opt)
  55 
  56     # open file on your own
  57     if filename:
  58       return open(filename, 'r')
  59 
  60   def asksaveasfile(self):
  61 
  62     """Returns an opened file in write mode."""
  63 
  64     return tkFileDialog.asksaveasfile(mode='w', **self.file_opt)
  65 
  66   def asksaveasfilename(self):
  67 
  68     """Returns an opened file in write mode.
  69     This time the dialog just returns a filename and the file is opened by your own code.
  70     """
  71 
  72     # get filename
  73     filename = tkFileDialog.asksaveasfilename(**self.file_opt)
  74 
  75     # open file on your own
  76     if filename:
  77       return open(filename, 'w')
  78 
  79   def askdirectory(self):
  80 
  81     """Returns a selected directoryname."""
  82 
  83     return tkFileDialog.askdirectory(**self.dir_opt)
  84 
  85 if __name__=='__main__':
  86   root = Tkinter.Tk()
  87   TkFileDialogExample(root).pack()
  88   root.mainloop()

Gotchas

References

TODO

tkinter: tkFileDialog (last edited 2012-08-30 16:46:44 by atomicat)