Tkfiledialog-tkinter Wiki

Source: Internet
Author: User
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 funence provided by the module will serve your needs.

Functions

First you have to decideIf 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 shoshould 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 wocould 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/

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

Get a filename to save

Asksaveasfilename (** options)

N/

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

Choose a directory

Askdirectory (** options)

N/

Initialdir, parent, title, mustexist

Options

-Defaultextension Extension

  • Specifies a string that will be appended to the filename if the user enters a filename without an extension. the default value is the empty string, which means no extension will be appended to the filename in any case. this option
    Is ignored on the Macintosh platform, which does not require extensions to filenames, And the Unix implementation guesses reasonable values for this from the-filetypes option when this is not supplied.

-Filetypes filepatternlist

  • If a file types ListBox exists in the file dialog on the participating platform, this option gives the filetypes in this ListBox. When the user choose a filetype In The ListBox,Only the files of that type are listed. If this
    Option is unspecified, or if it is set to the empty list, or if the file types ListBox is not supported by the particle platform then all files are listed regardless of their types. see the section specifying file patterns below for a discussion on the Contents
    Of filepatternlist.

-Initialdir directory

  • Specifies that the files in directory shocould be displayed when the dialog pops up. if this parameter is not specified, then the files in the current working directory are displayed. if the parameter specifies a relative path,
    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.

-Initialfile filename

  • Specifies a filename to be displayed in the dialog when it pops up. This option is ignored on the Macintosh platform.

-Message string

  • Specifies a message to include in the client area of the diies. This is only available on the Macintosh, and only when navigation services are installed.

-Multiple Boolean

  • Allows the user to choose multiple files from the open dialog. On the Macintosh, this is only available when navigation services are installed.

-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.

-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.

[Source:]

  • Http://www.tcl.tk/man/tcl8.4/TkCmd/getOpenFile.htm

  • Http://www.tcl.tk/man/tcl8.4/TkCmd/chooseDirectory.htm

Example

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

import Tkinter, Tkconstants, tkFileDialogclass TkFileDialogExample(Tkinter.Frame):  def __init__(self, root):    Tkinter.Frame.__init__(self, root)    # options for buttons    button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}    # define buttons    Tkinter.Button(self, text='askopenfile', command=self.askopenfile).pack(**button_opt)    Tkinter.Button(self, text='askopenfilename', command=self.askopenfilename).pack(**button_opt)    Tkinter.Button(self, text='asksaveasfile', command=self.asksaveasfile).pack(**button_opt)    Tkinter.Button(self, text='asksaveasfilename', command=self.asksaveasfilename).pack(**button_opt)    Tkinter.Button(self, text='askdirectory', command=self.askdirectory).pack(**button_opt)    # define options for opening or saving a file    self.file_opt = options = {}    options['defaultextension'] = '.txt'    options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]    options['initialdir'] = 'C:\\'    options['initialfile'] = 'myfile.txt'    options['parent'] = root    options['title'] = 'This is a title'    # This is only available on the Macintosh, and only when Navigation Services are installed.    #options['message'] = 'message'    # if you use the multiple file version of the module functions this option is set automatically.    #options['multiple'] = 1    # defining options for opening a directory    self.dir_opt = options = {}    options['initialdir'] = 'C:\\'    options['mustexist'] = False    options['parent'] = root    options['title'] = 'This is a title'  def askopenfile(self):    """Returns an opened file in read mode."""    return tkFileDialog.askopenfile(mode='r', **self.file_opt)  def askopenfilename(self):    """Returns an opened file in read mode.    This time the dialog just returns a filename and the file is opened by your own code.    """    # get filename    filename = tkFileDialog.askopenfilename(**self.file_opt)    # open file on your own    if filename:      return open(filename, 'r')  def asksaveasfile(self):    """Returns an opened file in write mode."""    return tkFileDialog.asksaveasfile(mode='w', **self.file_opt)  def asksaveasfilename(self):    """Returns an opened file in write mode.    This time the dialog just returns a filename and the file is opened by your own code.    """    # get filename    filename = tkFileDialog.asksaveasfilename(**self.file_opt)    # open file on your own    if filename:      return open(filename, 'w')  def askdirectory(self):    """Returns a selected directoryname."""    return tkFileDialog.askdirectory(**self.dir_opt)if __name__=='__main__':  root = Tkinter.Tk()  TkFileDialogExample(root).pack()  root.mainloop()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.