Advertisement
Najeebsk

SEARCH-OPEN-FILES.pyw

Jun 18th, 2024
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import ttk, messagebox
  4. from tkinter.filedialog import asksaveasfilename
  5. import subprocess
  6.  
  7. class AdvancedSearchApp:
  8.     def __init__(self, root):
  9.         self.root = root
  10.         self.root.title("Advanced Search in Current Folder")
  11.         self.root.configure(bg='#2e2e2e')  # Set background color
  12.  
  13.         # Style configuration
  14.         self.style = ttk.Style()
  15.         self.style.theme_use('clam')
  16.  
  17.         # Configure styles
  18.         self.style.configure("TFrame", background='#2e2e2e')
  19.         self.style.configure("TLabel", background='#2e2e2e', foreground='white', font=('Helvetica', 12))
  20.         self.style.configure("TButton", font=('Helvetica', 10, 'bold'))
  21.         self.style.configure("Save.TButton", background='#32CD32', foreground='white')
  22.         self.style.configure("TEntry", foreground='black', font=('Helvetica', 12))
  23.         self.style.configure("TListbox", foreground='white', background='#262626', font=('Helvetica', 12))
  24.  
  25.         # Frame for search controls
  26.         self.control_frame = ttk.Frame(self.root)
  27.         self.control_frame.pack(padx=10, pady=10, fill=tk.X)
  28.  
  29.         # Label and entry for file extension
  30.         self.extension_label = ttk.Label(self.control_frame, text="File Extension:")
  31.         self.extension_label.pack(side=tk.LEFT, padx=5)
  32.  
  33.         self.extension_var = tk.StringVar()
  34.         self.extension_entry = ttk.Entry(self.control_frame, textvariable=self.extension_var)
  35.         self.extension_entry.pack(side=tk.LEFT, padx=5, fill=tk.X, expand=True)
  36.  
  37.         # Save button
  38.         self.save_button = ttk.Button(self.control_frame, text="Save List to File", command=self.save_file_list, style="Save.TButton")
  39.         self.save_button.pack(side=tk.LEFT, padx=5)
  40.  
  41.         # Frame for search results
  42.         self.result_frame = ttk.Frame(self.root)
  43.         self.result_frame.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
  44.  
  45.         # Listbox to display search results
  46.         self.listbox = tk.Listbox(self.result_frame, selectmode=tk.SINGLE, bg='#262626', fg='white', highlightbackground='#333333', font=('Helvetica', 12))
  47.         self.listbox.pack(padx=10, pady=5, fill=tk.BOTH, expand=True)
  48.  
  49.         # Add a scrollbar to the listbox
  50.         self.scrollbar = ttk.Scrollbar(self.listbox, orient=tk.VERTICAL, command=self.listbox.yview)
  51.         self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  52.         self.listbox.config(yscrollcommand=self.scrollbar.set)
  53.  
  54.         # Bind double-click event to open the file
  55.         self.listbox.bind("<Double-1>", self.open_file)
  56.  
  57.         # Trace changes to the extension entry field
  58.         self.extension_var.trace("w", lambda name, index, mode: self.search_files())
  59.  
  60.     def search_files(self):
  61.         """Search for files with the specified extension in the current directory."""
  62.         extension = self.extension_entry.get().strip()
  63.         if not extension:
  64.             self.listbox.delete(0, tk.END)
  65.             return
  66.  
  67.         self.listbox.delete(0, tk.END)
  68.         current_directory = os.getcwd()
  69.         for root, _, files in os.walk(current_directory):
  70.             for file in files:
  71.                 if file.endswith(extension):
  72.                     self.listbox.insert(tk.END, os.path.join(root, file))
  73.  
  74.     def open_file(self, event):
  75.         """Open the selected file with the default application."""
  76.         selected_file = self.listbox.get(self.listbox.curselection())
  77.         try:
  78.             if os.name == 'nt':  # Windows
  79.                 os.startfile(selected_file)
  80.             elif os.name == 'posix':  # macOS, Linux
  81.                 subprocess.call(('open' if sys.platform == 'darwin' else 'xdg-open', selected_file))
  82.         except Exception as e:
  83.             messagebox.showerror("Error", f"Failed to open file: {e}")
  84.  
  85.     def save_file_list(self):
  86.         """Save the list of files to File-list.txt."""
  87.         save_path = asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
  88.         if save_path:
  89.             try:
  90.                 with open(save_path, 'w') as file_list:
  91.                     for i in range(self.listbox.size()):
  92.                         file_list.write(f"{self.listbox.get(i)}\n")
  93.                 messagebox.showinfo("Success", f"File list saved to {save_path}")
  94.             except Exception as e:
  95.                 messagebox.showerror("Error", f"Failed to save file list: {e}")
  96.  
  97. if __name__ == "__main__":
  98.     root = tk.Tk()
  99.     app = AdvancedSearchApp(root)
  100.     root.geometry("800x500")
  101.     root.mainloop()
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement