In this tutorial, I will explain how to upload a file in Python Tkinter library. As a developer working on a project that required users to select and upload files, I faced the challenge of implementing this functionality. After researching and experimenting, I discovered that Tkinter provides a simple way to create a file uploader and will share my experience and guide you through the process of building a file uploader using Tkinter.
Upload a File in Python Tkinter
Let us learn step by step how to upload a file in Python TKinter.
Read How to Create Python Tkinter Text Editor?
1. Create the Main Tkinter Window
To get started, we need to create the main Tkinter window that will host our file uploader. Here’s an example of how to create a basic window:
import tkinter as tk
window = tk.Tk()
window.title("File Uploader")
window.geometry("400x200")
window.mainloop()In this code snippet, we import the Tkinter module and create an instance of the Tk() class, which represents the main window. We set the window title to “File Uploader” using the title() method and specify its dimensions using the geometry() method. Finally, we start the Tkinter event loop mainloop() to display the window.
Check out How to Create Animations in Python with Tkinter?
2. Implement the File Uploader
Now that we have our main window set up, let’s dive into the implementation of the file uploader. Tkinter provides the filedialog module, which offers various file dialog functions. For our purpose, we’ll use the askopenfilename() function to allow users to select a file.
Here’s an example of how to create a file uploader button and handle the file selection:
import tkinter as tk
from tkinter import filedialog
def upload_file():
file_path = filedialog.askopenfilename()
if file_path:
print("Selected file:", file_path)
# Process the selected file here
window = tk.Tk()
window.title("File Uploader")
window.geometry("400x200")
upload_button = tk.Button(window, text="Upload File", command=upload_file)
upload_button.pack(pady=20)
window.mainloop()In this code, we define a function called upload_file() that will be triggered when the user clicks the “Upload File” button. Inside this function, we call filedialog.askopenfilename() to open the file dialog and allow the user to select a file. The selected file path is stored in the file_path variable.
We then create a button widget using tk.Button() and set its text to “Upload File”. The command parameter is set to upload_file, indicate that the upload_file() function should be called when the button is clicked. The button is positioned using the pack() method with some vertical padding (pady=20).
Read How to Master Python Tkinter Events?
3. Process the Selected File
Once the user selects a file, you can process it according to your application’s requirements. For example, you might want to read the contents of a text file or display an image file. Here’s an example of how to read the contents of a text file:
def upload_file():
file_path = filedialog.askopenfilename()
if file_path:
with open(file_path, "r") as file:
content = file.read()
print("File content:")
print(content)In this code, we use the open() function to open the selected file in read mode (“r”). We then read the contents of the file using the read() method and store it in the content variable. Finally, we print the file content.
Read How to Create Tabbed Interfaces in Python with Tkinter Notebook Widget?
4. Handle Different File Types
Depending on your application, you may need to handle different types of files, such as text files, images, or documents. Tkinter’s file dialogs allow you to specify the file types you want to support. Here’s an example of how to restrict the file selection to specific file types:
def upload_file():
file_types = [("Text Files", "*.txt"), ("Image Files", "*.jpg;*.png"), ("All Files", "*.*")]
file_path = filedialog.askopenfilename(filetypes=file_types)
if file_path:
print("Selected file:", file_path)
# Process the selected file based on its typeIn this code, we define a list called file_types that contains tuples specifying the file types and their corresponding extensions. Each tuple consists of a description and the file extensions are separated by semicolons. We pass this list to the filetypes parameter of askopenfilename() to restrict the file selection to the specified types.
Check out How to Create an On/Off Toggle Switch in Python Tkinter?
5. Handle File Upload Errors
When working with file uploads, it’s important to handle potential errors gracefully. For example, the user may cancel the file selection or select an unsupported file type. Here’s an example of how to handle such cases:
def upload_file():
file_path = filedialog.askopenfilename()
if file_path:
try:
# Process the selected file
print("Selected file:", file_path)
except Exception as e:
print("Error processing file:", str(e))
else:
print("No file selected.")In this code, we use a try-except block to handle any exceptions that may occur during file processing. If an exception is raised, we print an error message along with the exception details. If the user cancels the file selection or no file is selected, we print a message indicating that no file was selected.
You can refer to the screenshot below to see the output.

Read How to Validate User Input in Python Tkinter?
Conclusion
In this tutorial, I explained how to upload a file in Python Tkinter. I discussed some steps like creating the main window, implementing the file uploader, processing the selected file, handling different file types, and handling file upload errors.
You may like to read:
- How to Create a Search Box with Autocomplete in Python Tkinter?
- How to Create Message Boxes with Python Tkinter?
- How to Save Text to a File Using Python Tkinter?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.