Handling file selection

When a user needs to enter a file or directory path, the preferred way to do this is to display a dialog containing a miniature file browser, commonly called a file dialog. Like most toolkits, Tkinter provides us with dialogs for opening files, saving files, and selecting a directory. These are all part of the filedialog module.

Just like messagebox, filedialog is a Tkinter submodule that needs to be explicitly imported to be used. Also like messagebox, it contains a set of convenience functions that create file dialogs that are appropriate for different scenarios.

The following table lists the functions, what they return, and their UI features:

Function
Return value
Features
askdirectory Directory path ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.