Creating a file explorer in Python using Tkinter is an exciting way to dive into both GUI (Graphical User Interface) development and file handling. Tkinter is the standard GUI library for Python and provides a fast and easy way to create basic and complex applications.
In this tutorial, we’ll guide you through the steps to build your own file explorer. This tool will allow you to browse directories, open files, and perform various file operations. Whether you’re a beginner looking to enhance your Python skills or an experienced developer seeking a new project, this tutorial will offer valuable insights into using Tkinter for creating functional and user-friendly applications.
Let’s get started on this fun and practical project!
he following steps are involved in creating a tkinter application:
- Importing the Tkinter module.
- Creation of the main window (container).
- Addition of widgets to the main window
- Applying the event Trigger on widgets like buttons, etc.
Creating the File Explorer
To create a file explorer in Python, we need to import the filedialog module from Tkinter. This module allows us to open, save, and navigate files or directories.
To open a file explorer, we use the askopenfilename() method. This function creates a file dialog object that lets users select a file.
The syntax for askopenfilename() is as follows:
tkFileDialog.askopenfilename(initialdir = "/", title = "Select file", filetypes = (("file_type","*.extension"),("all files","*.*")))
Here’s a breakdown of the parameters:
initialdir: Specifies the path of the folder that opens when the file explorer pops up.title: The title of the opened file explorer window.filetypes: Allows specifying different file extensions so users can filter based on file types.

Full Code:
# Python program to create
# a file explorer in Tkinter
# import all components
# from the tkinter library
from tkinter import *
# import filedialog module
from tkinter import filedialog
# Function for opening the
# file explorer window
def browseFiles():
filename = filedialog.askopenfilename(initialdir = "/",
title = "Select a File",
filetypes = (("Text files",
"*.txt*"),
("all files",
"*.*")))
# Change label contents
label_file_explorer.configure(text="File Opened: "+filename)
# Open the selected file and display its contents in the text widget
with open(filename, 'r') as file:
file_content = file.read()
text_widget.delete('1.0', END) # Clear any existing content in the text widget
text_widget.insert(END, file_content)
# Create the root window
window = Tk()
# Set window title
window.title('File Explorer')
# Set window size
window.geometry("500x500")
# Set window background color
window.config(background = "white")
# Create a File Explorer label
label_file_explorer = Label(window,
text = "File Explorer using Tkinter",
width = 100, height = 4,
fg = "blue")
# Create a text widget to display the file content
text_widget = Text(window, wrap='word', width=60, height=20)
button_explore = Button(window,
text = "Browse Files",
command = browseFiles)
button_exit = Button(window,
text = "Exit",
command = exit)
# Grid method is chosen for placing
# the widgets at respective positions
# in a table like structure by
# specifying rows and columns
label_file_explorer.grid(column = 1, row = 1)
button_explore.grid(column = 1, row = 2)
button_exit.grid(column = 1,row = 3)
text_widget.grid(column = 1, row = 4, padx=10, pady=10)
# Let the window wait for any events
window.mainloop()
Output:
Explanation of the above code:
import filedialog module
from tkinter import filedialog
This comment explains that the next line imports the filedialog module from Tkinter, which provides dialogs for file operations like opening and saving files.
# Function for opening the
# file explorer window
def browseFiles():
This comment describes the function browseFiles, which will be used to open the file explorer window.
filename = filedialog.askopenfilename(initialdir = "/",
title = "Select a File",
filetypes = (("Text files",
"*.txt*"),
("all files",
"*.*")))
This line opens a file explorer dialog box for selecting a file.
initialdir="/": Sets the initial directory to the root directory.title="Select a File": Sets the title of the dialog box.filetypes=(("Text files", "*.txt*"), ("all files", "*.*")): Allows filtering of file types, showing only text files and all files.
# Change label contents
label_file_explorer.configure(text="File Opened: "+filename)
This comment describes that the next line changes the text of the label_file_explorer label to show the name of the selected file.
# Open the selected file and display its contents in the text widget
with open(filename, 'r') as file:
file_content = file.read()
text_widget.delete('1.0', END) # Clear any existing content in the text widget
text_widget.insert(END, file_content)
This block of code opens the selected file, reads its content, and displays it in the text widget.
with open(filename, 'r') as file: Opens the file in read mode.file_content = file.read(): Reads the entire content of the file.text_widget.delete('1.0', END): Clears any existing content in the text widget.text_widget.insert(END, file_content): Inserts the read content into the text widget
# Create a File Explorer label
label_file_explorer = Label(window,
text = "File Explorer using Tkinter",
width = 100, height = 4,
fg = "blue")
This block creates a label widget named label_file_explorer.
window: The parent widget.text = "File Explorer using Tkinter": The text displayed on the label.width = 100, height = 4: Sets the width and height of the label.fg = "blue": Sets the text color to blue.
# Create a text widget to display the file content
text_widget = Text(window, wrap='word', width=60, height=20)
This comment explains that the next line creates a text widget for displaying the content of the selected file.
wrap='word': Wraps text by words.width=60, height=20: Sets the width and height of the text widget.
# Grid method is chosen for placing
# the widgets at respective positions
# in a table-like structure by
# specifying rows and columns
label_file_explorer.grid(column = 1, row = 1)
button_explore.grid(column = 1, row = 2)
button_exit.grid(column = 1, row = 3)
text_widget.grid(column = 1, row = 4, padx=10, pady=10)
These comments and lines of code use the grid method to place the widgets in a grid-like structure.
label_file_explorer.grid(column = 1, row = 1): Places the label in the first row and column.button_explore.grid(column = 1, row = 2): Places the “Browse Files” button in the second row and column.button_exit.grid(column = 1, row = 3): Places the “Exit” button in the third row and column.text_widget.grid(column = 1, row = 4, padx=10, pady=10): Places the text widget in the fourth row and column, with padding of 10 pixels on all sides.
In conclusion, creating a File Explorer in Python using Tkinter is a straightforward and efficient way to implement a graphical interface for file browsing and selection. Tkinter’s built-in modules, such as filedialog, provide robust functionality to handle file operations with ease. By following the steps outlined, you can create a user-friendly File Explorer that allows users to open and read files within a custom Tkinter window. This project not only enhances your understanding of Tkinter but also demonstrates how to integrate file handling capabilities into your Python applications.





Leave a Reply