Prerequisites: Introduction to Tkinter
Python offers various modules to create graphics programs. Out of these Tkinter provides the fastest and easiest way to create GUI applications.
The 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.
The GUI would look like below:

Creating the File Explorer
In order to do so, we have to import the filedialog module from Tkinter. The File dialog module will help you open, save files or directories.
In order to open a file explorer, we have to use the method, askopenfilename(). This function creates a file dialog object.
Syntax: tkFileDialog.askopenfilename(initialdir = “/”,title = “Select file”,filetypes = ((“file_type”,”*.extension”),(“all files”,”*.*”)))
Parameters:
- initialdir: We have to specify the path of the folder that is to be opened when the file explorer pops up.
- title: The title of file explorer opened.
- filetypes: Here we can specify different kinds of file extensions so that the user can filter based on different file types
Below is the implementation
Python3
from tkinter import *
from tkinter import filedialog
def browseFiles():
filename = filedialog.askopenfilename(initialdir = "/" ,
title = "Select a File" ,
filetypes = (( "Text files" ,
"*.txt*" ),
( "all files" ,
"*.*" )))
label_file_explorer.configure(text = "File Opened: " + filename)
window = Tk()
window.title( 'File Explorer' )
window.geometry( "500x500" )
window.config(background = "white" )
label_file_explorer = Label(window,
text = "File Explorer using Tkinter" ,
width = 100 , height = 4 ,
fg = "blue" )
button_explore = Button(window,
text = "Browse Files" ,
command = browseFiles)
button_exit = Button(window,
text = "Exit" ,
command = exit)
label_file_explorer.grid(column = 1 , row = 1 )
button_explore.grid(column = 1 , row = 2 )
button_exit.grid(column = 1 ,row = 3 )
window.mainloop()
|
Output:



Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
15 Feb, 2021
Like Article
Save Article