How to use Tkinter Filedialog in Python [3 Examples]

In this Tkinter tutorial, we will learn how to handle files in Python Tkinter and how to use Tkinter Filedialog in Python applications.

A module in Tkinter called filedialog provides a set of tools to implement file-related actions like selecting a file to open, choosing a location to save the file, and picking a directory.

Python tkinter filedialog

File handling in Tkinter Filedialog in Python

I will also explain the below-mentioned things for a better understanding of Tkinter filedialog in Python.

  • Select and Open the file
  • Users can save the file.
  • Selecting a Directory

Open Files in Tkinter

To open the file in Tkinter, one built-in method is called askopenfilename in Python. It will accept two parameters named title and filetypes.

Let’s see one example of how to Open the file using the filedialog in Python Tkinter.

import tkinter as tk
from tkinter import filedialog

def upload_file():
    file_path = filedialog.askopenfilename(title="Select Profile Picture", filetypes=[("Text File", ('*.txt')), ("All files", "*.*")])
    print("Selected File:", file_path)

root = tk.Tk()
open_button = tk.Button(root, text="Open File", command= upload_file)
open_button.pack(pady=10)
root.mainloop()

Here is a screenshot that represents the working of the above-mentioned Python code:

Tkinter Filedialog in Python

Let’s take another example where users can select only a particular file for which we gave permission. We will take one scenario where the user has two options to select the file type in Python Tkinter.

import tkinter as tk
from tkinter import filedialog

def upload_profile_picture():
    file_path = filedialog.askopenfilename(title="Select Profile Picture", filetypes=[("Jpeg files",('*.jpeg')), ("Png file",('*.png'))])
    print("Selected File:", file_path)

root = tk.Tk()

open_button = tk.Button(root, text="Open File", command=upload_profile_picture)
open_button.pack(pady=10)

root.mainloop()

After execution of the Python code, I took the below screenshot:

How to upload file in tkinter python

Saving Files with Tkinter

To save the file using Python Tkinter, there is a function called asksaveasfilename in Python. This function accepts three parameters, which are title, default extension, and file types.

Here is a practical example of how you can save the file using Tkinter in Python:

import tkinter as tk
from tkinter import filedialog

def save_file():
    file_path = filedialog.asksaveasfilename(title="Save As", defaultextension=".txt", filetypes=[("Text files", "*.txt"), ("All files", "*.*")])

    if file_path:
        content = "Welcome to Python Guides!"

        try:
            with open(file_path, 'w') as file:
                file.write(content)
            print(f"File saved successfully at: {file_path}")
        except Exception as e:
            print(f"Error saving file: {e}")

root = tk.Tk()

save_button = tk.Button(root, text="Save File", command=save_file)
save_button.pack(pady=10)

root.mainloop()

Here is the screenshot of the Python code execution:

file handling in tkinter python

3. Choosing Directories with Tkinter

To choose the directory, there is one function called askdirectory in Python Tkinter filedialog. This method accepts one parameter, which is “title“.

Here is an example of choosing a directory manually in Python with the help of the title parameter:

import tkinter as tk
from tkinter import filedialog

def choose_directory():
    directory_path = filedialog.askdirectory(title="Choose a Directory")
    print("Selected Directory:", directory_path)

root = tk.Tk()

# Create a button to trigger the directory dialog
directory_button = tk.Button(root, text="Choose Directory", command=choose_directory)
directory_button.pack(pady=10)

root.mainloop()

Down is the screenshot of the code execution:

file dialog in tkinter python

Conclusion

This article covers file-handling topics like opening a file using the askopenfilename method, saving the file using the asksaveasfilename method, and selecting a directory using the askdirectory methods in Python.

These methods are part of the Tkinter filedialog in Python. I hope you understand how to use the Tkinter filedialog in Python.

You may also like to read these Python Tkinter articles: