Create a Mini File Explorer in Python + CTkinter GUI | Modern File Manager Clone
Demo :
Click Video πππ
Featured :
Thumbnail of “Modern File Explorer UI” (with ππ icons + dark theme text overlay: “Mini File Explorer in Python + CTkinter”).
Features:
-
✅ Title + Embed YouTube Short
-
✅ Source Code (full script with highlights)
-
✅ Step-by-step explanation
-
✅ Call to action (Subscribe + Follow on socials)
Code :
import os
import shutil
import tkinter as tk
from tkinter import filedialog, messagebox
import customtkinter as ctk
# ---------------------------
# Modern Mini File Explorer
# ---------------------------
class FileExplorer(ctk.CTk):
def __init__(self):
super().__init__()
self.title("Fuzzu File Explorer")
self.geometry("600x600")
self.minsize(650, 500)
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")
self.current_path = os.path.expanduser("~")
self.selected_item = None
self._build_ui()
self._load_files()
# ---------------------------
# Build User Interface
# ---------------------------
def _build_ui(self):
# Path bar
path_frame = ctk.CTkFrame(self, corner_radius=10)
path_frame.pack(fill="x", padx=10, pady=10)
self.path_entry = ctk.CTkEntry(path_frame)
self.path_entry.pack(side="left", fill="x", expand=True, padx=5, pady=5)
self.path_entry.insert(0, self.current_path)
go_btn = ctk.CTkButton(path_frame, text="Go", width=60, command=self._go_to_path)
go_btn.pack(side="left", padx=5)
back_btn = ctk.CTkButton(path_frame, text="←", width=40, command=self._go_back)
back_btn.pack(side="left")
refresh_btn = ctk.CTkButton(path_frame, text="⟳", width=40, command=self._load_files)
refresh_btn.pack(side="left")
# File list with Scrollbar
list_frame = ctk.CTkFrame(self)
list_frame.pack(fill="both", expand=True, padx=10, pady=5)
self.file_listbox = tk.Listbox(list_frame, font=("Segoe UI", 12))
self.file_listbox.pack(side="left", fill="both", expand=True)
scrollbar = tk.Scrollbar(list_frame, orient="vertical", command=self.file_listbox.yview)
scrollbar.pack(side="right", fill="y")
self.file_listbox.config(yscrollcommand=scrollbar.set)
self.file_listbox.bind("<Double-1>", self._open_item)
self.file_listbox.bind("<Button-3>", self._right_click_menu)
# Status bar
self.status_label = ctk.CTkLabel(self, text="Ready", anchor="w")
self.status_label.pack(fill="x", padx=10, pady=5)
# ---------------------------
# Load Files
# ---------------------------
def _load_files(self):
try:
self.file_listbox.delete(0, tk.END)
items = os.listdir(self.current_path)
for item in items:
full_path = os.path.join(self.current_path, item)
if os.path.isdir(full_path):
self.file_listbox.insert(tk.END, f"π {item}")
else:
self.file_listbox.insert(tk.END, f"π {item}")
self.path_entry.delete(0, tk.END)
self.path_entry.insert(0, self.current_path)
self.status_label.configure(text=f"Loaded {len(items)} items")
except Exception as e:
messagebox.showerror("Error", str(e))
# ---------------------------
# Path Navigation
# ---------------------------
def _go_to_path(self):
path = self.path_entry.get()
if os.path.exists(path):
self.current_path = path
self._load_files()
else:
messagebox.showwarning("Invalid Path", "The entered path does not exist.")
def _go_back(self):
parent = os.path.dirname(self.current_path)
if parent and os.path.exists(parent):
self.current_path = parent
self._load_files()
# ---------------------------
# Item Actions
# ---------------------------
def _open_item(self, event=None):
selection = self.file_listbox.get(tk.ACTIVE)
item_name = selection[2:].strip()
full_path = os.path.join(self.current_path, item_name)
if os.path.isdir(full_path):
self.current_path = full_path
self._load_files()
else:
try:
os.startfile(full_path)
except Exception as e:
messagebox.showerror("Error", str(e))
def _right_click_menu(self, event):
try:
index = self.file_listbox.nearest(event.y)
self.file_listbox.selection_clear(0, tk.END)
self.file_listbox.selection_set(index)
self.file_listbox.activate(index)
selection = self.file_listbox.get(index)
self.selected_item = selection[2:].strip()
menu = tk.Menu(self, tearoff=0)
menu.add_command(label="Open", command=self._open_item)
menu.add_command(label="Delete", command=self._delete_item)
menu.add_command(label="Rename", command=self._rename_item)
menu.post(event.x_root, event.y_root)
except Exception:
pass
def _delete_item(self):
if not self.selected_item:
return
full_path = os.path.join(self.current_path, self.selected_item)
confirm = messagebox.askyesno("Confirm", f"Delete {self.selected_item}?")
if confirm:
try:
if os.path.isdir(full_path):
shutil.rmtree(full_path)
else:
os.remove(full_path)
self._load_files()
except Exception as e:
messagebox.showerror("Error", str(e))
def _rename_item(self):
if not self.selected_item:
return
new_name = filedialog.asksaveasfilename(initialdir=self.current_path, initialfile=self.selected_item)
if new_name:
try:
os.rename(os.path.join(self.current_path, self.selected_item), new_name)
self._load_files()
except Exception as e:
messagebox.showerror("Error", str(e))
if __name__ == "__main__":
app = FileExplorer()
app.mainloop()
Comments
Post a Comment