How it works...

We create the first dialog with the askopenfilename function, which returns a string that represents the full path to the chosen file. It accepts the following optional arguments:

  • title: Title displayed in the dialog's title bar.
  • initialdir: Initial directory.
  • filetypes: Sequence of tuples of two strings. The first one is a label indicating the type of the file in a human-readable format, whereas the second one is a pattern to match the filename.
  • multiple: Boolean value to indicate whether users may select multiple files.
  • defaultextension: Extension added to the filename if it is not explicitly given.

In our example, we set the initial directory to the root folder and a custom title. In our tuple of file types, we have the following three valid choices: text files saved with the .txt extension; images with the .jpg, .gif, and .png extensions; and the wildcard ("*") to match all files.

Note that these patterns do not necessarily match the format of the data contained in the file since it is possible to rename a file with a different extension:

filetypes = (("Plain text files", "*.txt"),
("Images", "*.jpg *.gif *.png"),
("All files", "*"))
filename = fd.askopenfilename(title="Open file", initialdir="/",
filetypes=filetypes)

The askdirectory function also takes the title and initialdir parameters, and a mustexist Boolean option to indicate whether users have to pick an existing directory:

directory = fd.askdirectory(title="Open directory", initialdir="/")
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset