Advertisement
Najeebsk

SEARCH-OPEN-FILES2.0.pyw

Jun 19th, 2024
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.05 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.         # Label and entry for file title
  38.         self.title_label = ttk.Label(self.control_frame, text="File Title:")
  39.         self.title_label.pack(side=tk.LEFT, padx=5)
  40.  
  41.         self.title_var = tk.StringVar()
  42.         self.title_entry = ttk.Entry(self.control_frame, textvariable=self.title_var)
  43.         self.title_entry.pack(side=tk.LEFT, padx=5, fill=tk.X, expand=True)
  44.  
  45.         # Save button
  46.         self.save_button = ttk.Button(self.control_frame, text="Save List to File", command=self.save_file_list, style="Save.TButton")
  47.         self.save_button.pack(side=tk.LEFT, padx=5)
  48.  
  49.         # Frame for search results
  50.         self.result_frame = ttk.Frame(self.root)
  51.         self.result_frame.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
  52.  
  53.         # Listbox to display search results
  54.         self.listbox = tk.Listbox(self.result_frame, selectmode=tk.SINGLE, bg='#262626', fg='white', highlightbackground='#333333', font=('Helvetica', 12))
  55.         self.listbox.pack(padx=10, pady=5, fill=tk.BOTH, expand=True)
  56.  
  57.         # Add a scrollbar to the listbox
  58.         self.scrollbar = ttk.Scrollbar(self.listbox, orient=tk.VERTICAL, command=self.listbox.yview)
  59.         self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  60.         self.listbox.config(yscrollcommand=self.scrollbar.set)
  61.  
  62.         # Bind double-click event to open the file
  63.         self.listbox.bind("<Double-1>", self.open_file)
  64.  
  65.         # Trace changes to the extension and title entry fields
  66.         self.extension_var.trace("w", lambda name, index, mode: self.search_files())
  67.         self.title_var.trace("w", lambda name, index, mode: self.search_files())
  68.  
  69.     def search_files(self):
  70.         """Search for files with the specified extension and title in the current directory."""
  71.         extension = self.extension_entry.get().strip()
  72.         title = self.title_entry.get().strip().lower()
  73.  
  74.         self.listbox.delete(0, tk.END)
  75.         current_directory = os.getcwd()
  76.         for root, _, files in os.walk(current_directory):
  77.             for file in files:
  78.                 if extension and not file.endswith(extension):
  79.                     continue
  80.                 if title and title not in file.lower():
  81.                     continue
  82.                 self.listbox.insert(tk.END, os.path.join(root, file))
  83.  
  84.     def open_file(self, event):
  85.         """Open the selected file with the default application."""
  86.         selected_file = self.listbox.get(self.listbox.curselection())
  87.         try:
  88.             if os.name == 'nt':  # Windows
  89.                 os.startfile(selected_file)
  90.             elif os.name == 'posix':  # macOS, Linux
  91.                 subprocess.call(('open' if sys.platform == 'darwin' else 'xdg-open', selected_file))
  92.         except Exception as e:
  93.             messagebox.showerror("Error", f"Failed to open file: {e}")
  94.  
  95.     def save_file_list(self):
  96.         """Save the list of files to File-list.txt."""
  97.         save_path = asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
  98.         if save_path:
  99.             try:
  100.                 with open(save_path, 'w') as file_list:
  101.                     for i in range(self.listbox.size()):
  102.                         file_list.write(f"{self.listbox.get(i)}\n")
  103.                 messagebox.showinfo("Success", f"File list saved to {save_path}")
  104.             except Exception as e:
  105.                 messagebox.showerror("Error", f"Failed to save file list: {e}")
  106.  
  107. if __name__ == "__main__":
  108.     root = tk.Tk()
  109.     app = AdvancedSearchApp(root)
  110.     root.geometry("800x500")
  111.     root.mainloop()
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement