Blog Pages

Command Palette GUI app

pip install fuzzywuzzy python-Levenshtein

import tkinter as tk
from tkinter import messagebox
import subprocess
import os
from fuzzywuzzy import process

# Sample commands/apps to match
COMMANDS = {
    "Open Notepad": "notepad",
    "Open Calculator": "calc",
    "Command Prompt": "cmd",
    "Open Task Manager": "taskmgr",
    "Open Paint": "mspaint",
    "Open Explorer": "explorer",
    "Shutdown": "shutdown /s /t 1",
    "Restart": "shutdown /r /t 1",
    "Open Downloads": os.path.expanduser("~/Downloads"),
    "Open VSCode": r"C:\Users\ASUS\AppData\Local\Programs\Microsoft VS Code\Code.exe",  # Modify as needed
}

class CommandPaletteApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Command Palette")
        self.root.geometry("500x300")
        self.root.configure(bg="#1e1e1e")

        self.entry = tk.Entry(self.root, font=("Segoe UI", 16), bg="#252526", fg="white", insertbackground="white")
        self.entry.pack(pady=20, padx=20, fill="x")
        self.entry.bind("<KeyRelease>", self.update_suggestions)
        self.entry.bind("<Return>", self.execute_selection)

        self.listbox = tk.Listbox(self.root, font=("Segoe UI", 14), bg="#1e1e1e", fg="white", selectbackground="#007acc")
        self.listbox.pack(padx=20, fill="both", expand=True)
        self.listbox.bind("<Double-Button-1>", self.execute_selection)

        self.commands = list(COMMANDS.keys())
        self.update_list(self.commands)

    def update_suggestions(self, event=None):
        typed = self.entry.get()
        if typed == "":
            self.update_list(self.commands)
        else:
            results = process.extract(typed, self.commands, limit=8)
            self.update_list([r[0] for r in results if r[1] > 40])

    def update_list(self, data):
        self.listbox.delete(0, tk.END)
        for item in data:
            self.listbox.insert(tk.END, item)

    def execute_selection(self, event=None):
        selection = self.listbox.get(tk.ACTIVE)
        if selection in COMMANDS:
            command = COMMANDS[selection]
            try:
                if os.path.isfile(command):
                    subprocess.Popen(command)
                elif os.path.isdir(command):
                    os.startfile(command)
                else:
                    subprocess.Popen(command, shell=True)
            except Exception as e:
                messagebox.showerror("Error", f"Failed to execute: {e}")
        else:
            messagebox.showinfo("Not Found", "No matching command to run.")

if __name__ == "__main__":
    root = tk.Tk()
    app = CommandPaletteApp(root)
    root.mainloop()

No comments:

Post a Comment