How to Use Tkinter Filedialog in Python

In this Tkinter tutorial, we will learn how to handle files in Python Tkinter and how to use the 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 following things for a better understanding of the Tkinter filedialog in Python.

Check out the Registration Form in Python Using Tkinter With Database SQLite3

1. 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 following screenshot:

How to upload file in tkinter python

Read BMI Calculator Using Python Tkinter

2. Save 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 a 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

Check out Create a Countdown Timer using Python Tkinter

3. Choose Directories with Tkinter

To choose the directory, there is a function called askdirectory in the 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

Read Python Tkinter Todo List

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 method 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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.