DEV Community

Mate Technologies
Mate Technologies

Posted on

๐Ÿš€ FileMate Pro: A Python GUI File Manager with Tkinter

Managing files directly from a GUI in Python doesnโ€™t have to be complicated. Meet FileMate Pro, a lightweight, user-friendly file manager built with Tkinter and sv_ttk for modern themed widgets. It supports folder navigation, creating, deleting, renaming, copy/paste, and even a dark mode toggle.

Whether you're automating file tasks or building your first GUI project, this script is a great starting point!

๐Ÿ“Œ Features

Browse directories with folder and file icons

Open files with a double-click

Create, delete, and rename files/folders

Copy & paste functionality

Dark/Light mode toggle

Status bar for live feedback

๐Ÿ–ฅ๏ธ Screenshot Preview

(You can add a screenshot of your GUI here if you want.)

๐Ÿ› ๏ธ Full Script

import sys
import os
import shutil
import tkinter as tk
from tkinter import ttk, messagebox, simpledialog
import sv_ttk

def resource_path(file_name):
    """Get the absolute path to a resource, works for PyInstaller."""
    base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, file_name)

# =========================
# App Setup
# =========================
root = tk.Tk()
root.title("FileMate Pro")
root.geometry("1000x680")
root.minsize(1000, 680)
sv_ttk.set_theme("light")

# =========================
# Globals
# =========================
dark_mode_var = tk.BooleanVar(value=False)
current_path_var = tk.StringVar(value=os.path.expanduser("~"))
status_var = tk.StringVar(value="Ready")
clipboard = None

# =========================
# Helpers
# =========================
def set_status(msg):
    status_var.set(msg)
    root.update_idletasks()

def toggle_theme():
    sv_ttk.set_theme("dark" if dark_mode_var.get() else "light")
    set_status(f"Theme switched to {'Dark' if dark_mode_var.get() else 'Light'} mode")

def refresh_file_list(path=None):
    path = path or current_path_var.get()
    if not os.path.exists(path):
        messagebox.showerror("Error", "Path does not exist!")
        return
    current_path_var.set(path)
    file_listbox.delete(0, tk.END)
    try:
        for item in os.listdir(path):
            full_path = os.path.join(path, item)
            display = "๐Ÿ“ " + item if os.path.isdir(full_path) else "๐Ÿ“„ " + item
            file_listbox.insert(tk.END, display)
        set_status(f"Listing files in {path}")
    except PermissionError:
        messagebox.showerror("Error", "Permission denied.")

def open_selected():
    selection = file_listbox.curselection()
    if not selection:
        return
    item_text = file_listbox.get(selection[0])
    item_name = item_text[2:]
    path = os.path.join(current_path_var.get(), item_name)
    if os.path.isdir(path):
        refresh_file_list(path)
    else:
        try:
            os.startfile(path)
            set_status(f"Opened {item_name}")
        except Exception as e:
            messagebox.showerror("Error", f"Cannot open file: {e}")

def go_up():
    parent = os.path.dirname(current_path_var.get())
    refresh_file_list(parent)

def create_folder():
    folder_name = simpledialog.askstring("Create Folder", "Enter folder name:")
    if folder_name:
        path = os.path.join(current_path_var.get(), folder_name)
        try:
            os.makedirs(path)
            refresh_file_list()
            set_status(f"Folder '{folder_name}' created")
        except FileExistsError:
            messagebox.showerror("Error", "Folder already exists.")
        except Exception as e:
            messagebox.showerror("Error", str(e))

def delete_item(path=None):
    if not path:
        selection = file_listbox.curselection()
        if not selection:
            return
        item_text = file_listbox.get(selection[0])
        item_name = item_text[2:]
        path = os.path.join(current_path_var.get(), item_name)
    if messagebox.askyesno("Confirm Delete", f"Are you sure you want to delete '{os.path.basename(path)}'?"):
        try:
            if os.path.isdir(path):
                shutil.rmtree(path)
            else:
                os.remove(path)
            refresh_file_list()
            set_status(f"Deleted '{os.path.basename(path)}'")
        except Exception as e:
            messagebox.showerror("Error", str(e))

def rename_item():
    selection = file_listbox.curselection()
    if not selection:
        return
    item_text = file_listbox.get(selection[0])
    old_name = item_text[2:]
    old_path = os.path.join(current_path_var.get(), old_name)
    new_name = simpledialog.askstring("Rename", f"Enter new name for '{old_name}':")
    if new_name:
        new_path = os.path.join(current_path_var.get(), new_name)
        try:
            os.rename(old_path, new_path)
            refresh_file_list()
            set_status(f"Renamed '{old_name}' to '{new_name}'")
        except Exception as e:
            messagebox.showerror("Error", str(e))

def copy_item():
    global clipboard
    selection = file_listbox.curselection()
    if not selection:
        return
    item_text = file_listbox.get(selection[0])
    clipboard = os.path.join(current_path_var.get(), item_text[2:])
    set_status(f"Copied '{os.path.basename(clipboard)}'")

def paste_item():
    global clipboard
    if not clipboard:
        return
    dest = os.path.join(current_path_var.get(), os.path.basename(clipboard))
    try:
        if os.path.isdir(clipboard):
            shutil.copytree(clipboard, dest)
        else:
            shutil.copy2(clipboard, dest)
        refresh_file_list()
        set_status(f"Pasted '{os.path.basename(clipboard)}'")
    except Exception as e:
        messagebox.showerror("Error", str(e))

# =========================
# Context Menu
# =========================
context_menu = tk.Menu(root, tearoff=0)
context_menu.add_command(label="Open", command=open_selected)
context_menu.add_command(label="Delete", command=lambda: delete_item())
context_menu.add_command(label="Rename", command=rename_item)
context_menu.add_separator()
context_menu.add_command(label="Copy", command=copy_item)
context_menu.add_command(label="Paste", command=paste_item)

def show_context_menu(event):
    try:
        selection = file_listbox.nearest(event.y)
        file_listbox.selection_clear(0, tk.END)
        file_listbox.selection_set(selection)
        context_menu.post(event.x_root, event.y_root)
    finally:
        context_menu.grab_release()

# =========================
# Status Bar
# =========================
ttk.Label(root, textvariable=status_var, anchor="w", font=("Segoe UI", 10)).pack(side=tk.BOTTOM, fill="x")

# =========================
# Main Frame
# =========================
main_frame = ttk.Frame(root, padding=20)
main_frame.pack(expand=True, fill="both")

# Toolbar
toolbar = ttk.Frame(main_frame)
toolbar.pack(fill="x", pady=(0, 10))
ttk.Button(toolbar, text="Up", command=go_up).pack(side="left", padx=5)
ttk.Button(toolbar, text="New Folder", command=create_folder).pack(side="left", padx=5)
ttk.Button(toolbar, text="Delete", command=delete_item).pack(side="left", padx=5)
ttk.Button(toolbar, text="Copy", command=copy_item).pack(side="left", padx=5)
ttk.Button(toolbar, text="Paste", command=paste_item).pack(side="left", padx=5)
ttk.Checkbutton(toolbar, text="Dark Mode", variable=dark_mode_var, command=toggle_theme).pack(side="right", padx=5)

# Current Path
path_frame = ttk.Frame(main_frame)
path_frame.pack(fill="x", pady=(0, 10))
ttk.Label(path_frame, text="Current Path:").pack(side="left")
ttk.Entry(path_frame, textvariable=current_path_var, width=80).pack(side="left", padx=(5, 0))
ttk.Button(path_frame, text="Go", command=lambda: refresh_file_list(current_path_var.get())).pack(side="left", padx=5)

# File List
file_frame = ttk.Frame(main_frame)
file_frame.pack(expand=True, fill="both")
file_listbox = tk.Listbox(file_frame, font=("Segoe UI", 11))
file_listbox.pack(side="left", expand=True, fill="both")
file_listbox.bind("<Double-Button-1>", lambda e: open_selected())
file_listbox.bind("<Button-3>", show_context_menu)
scrollbar = ttk.Scrollbar(file_frame, orient="vertical", command=file_listbox.yview)
scrollbar.pack(side="right", fill="y")
file_listbox.config(yscrollcommand=scrollbar.set)

# Initialize file list
refresh_file_list()

# =========================
# Run App
# =========================
root.mainloop()
Enter fullscreen mode Exit fullscreen mode

โœจ How to Run

Install dependencies:

pip install sv_ttk

Run the script:

python FileMat_v3.py

โ€œFileMate Pro โ€“ Your friendly Python file manager with dark mode, copy-paste, and all the essentials to browse and organize files effortlessly!โ€

Top comments (0)