File Explorer IN Python Using Tkinter


Tkinter is a Python toolkit library used to build interfaces, design applications and create Graphical User Interfaces (GUIs). It originated from the Tcl programming language, which Python uses as a wrapper around Tk GUI Toolkit. Now it is used mostly with Python.

It is a versatile tool where you can develop pages, buttons and customize fonts and background colours according to your preference.

A file explorer is a tool that allows the user to navigate through all existing files present in the system for ease of access to files and open them without much difficulty. It offers a centralized database, where all file locations are stored.

By building the explorer in tkinter, we can find files much easier by integrating Python into the file explorer and customising its looks according to our requirements, a task that is not possible in a normal file explorer. Its API makes integration with the OS of the system seamless.

Algorithm

  • Import the library tkinter.

  • Import askopenfilename in tkinter to return file directories.

  • Create a user-defined function browse() to open file names and specify file types.

  • To show that the file is opened, print “File Opened” in the interface to show its success.

  • Build a window where the application will run to showcase the file explorer and start a tkinter instance.

  • Define the title of the window.

  • Define the size of the window so that it doesn’t wrap around the text only.

  • Customize the background.

  • Define a label where the file directory will be printed along with the confirmation message.

  • Create a button to open the file explorer to browse through the system and select the file to be opened.

  • Wrap the button to the browse function defined in step 3.

  • Pack the label.

  • Pack the button.

  • Loop the interface using the mainloop function unless it is closed.

Example

import tkinter as tk
from tkinter.filedialog import askopenfilename

def browse():
   f_path = askopenfilename(initialdir="/",
      title="Select File", filetypes=(("Text files","*.txt*"),("All Files","*.*")))
   file_explorer.configure(text="File Opened: "+f_path)

root = tk.Tk()
root.title("File Explorer")
root.geometry("750x350")

root.config(background="black")

file_explorer = tk.Label(root, text="Explore files",
   font=("Verdana", 14, "bold"),
   width=100,
   height=4, fg="white", bg="gray")

button=tk.Button(root, text="Browse Folder", font =("Roboto", 14),
   command=browse)
file_explorer.pack()
button.pack(pady=10)

root.mainloop()

Output

Conclusion

Tkinter provides a solid foundation for such general-purpose applications and small-time projects with little effort. It has the ability to seamlessly integrate with Python’s ecosystem. It can also work with other libraries making it easy to incorporate GUI functionality for Python projects. Its API is very beginner-friendly, enabling programmers to build applications with relative ease.

But, it has its limitations when it comes to building modern graphics and User-Interface (UI) design despite being able to create GUI applications. It generally has a very outdated look compared to modern UI design and widgets.

Updated on: 23-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements