Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import tkinter as tk
- from tkinter import ttk, messagebox
- from tkinter.filedialog import asksaveasfilename
- import subprocess
- class AdvancedSearchApp:
- def __init__(self, root):
- self.root = root
- self.root.title("Advanced Search in Current Folder")
- self.root.configure(bg='#2e2e2e') # Set background color
- # Style configuration
- self.style = ttk.Style()
- self.style.theme_use('clam')
- # Configure styles
- self.style.configure("TFrame", background='#2e2e2e')
- self.style.configure("TLabel", background='#2e2e2e', foreground='white', font=('Helvetica', 12))
- self.style.configure("TButton", font=('Helvetica', 10, 'bold'))
- self.style.configure("Save.TButton", background='#32CD32', foreground='white')
- self.style.configure("TEntry", foreground='black', font=('Helvetica', 12))
- self.style.configure("TListbox", foreground='white', background='#262626', font=('Helvetica', 12))
- # Frame for search controls
- self.control_frame = ttk.Frame(self.root)
- self.control_frame.pack(padx=10, pady=10, fill=tk.X)
- # Label and entry for file extension
- self.extension_label = ttk.Label(self.control_frame, text="File Extension:")
- self.extension_label.pack(side=tk.LEFT, padx=5)
- self.extension_var = tk.StringVar()
- self.extension_entry = ttk.Entry(self.control_frame, textvariable=self.extension_var)
- self.extension_entry.pack(side=tk.LEFT, padx=5, fill=tk.X, expand=True)
- # Label and entry for file title
- self.title_label = ttk.Label(self.control_frame, text="File Title:")
- self.title_label.pack(side=tk.LEFT, padx=5)
- self.title_var = tk.StringVar()
- self.title_entry = ttk.Entry(self.control_frame, textvariable=self.title_var)
- self.title_entry.pack(side=tk.LEFT, padx=5, fill=tk.X, expand=True)
- # Save button
- self.save_button = ttk.Button(self.control_frame, text="Save List to File", command=self.save_file_list, style="Save.TButton")
- self.save_button.pack(side=tk.LEFT, padx=5)
- # Frame for search results
- self.result_frame = ttk.Frame(self.root)
- self.result_frame.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
- # Listbox to display search results
- self.listbox = tk.Listbox(self.result_frame, selectmode=tk.SINGLE, bg='#262626', fg='white', highlightbackground='#333333', font=('Helvetica', 12))
- self.listbox.pack(padx=10, pady=5, fill=tk.BOTH, expand=True)
- # Add a scrollbar to the listbox
- self.scrollbar = ttk.Scrollbar(self.listbox, orient=tk.VERTICAL, command=self.listbox.yview)
- self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- self.listbox.config(yscrollcommand=self.scrollbar.set)
- # Bind double-click event to open the file
- self.listbox.bind("<Double-1>", self.open_file)
- # Trace changes to the extension and title entry fields
- self.extension_var.trace("w", lambda name, index, mode: self.search_files())
- self.title_var.trace("w", lambda name, index, mode: self.search_files())
- def search_files(self):
- """Search for files with the specified extension and title in the current directory."""
- extension = self.extension_entry.get().strip()
- title = self.title_entry.get().strip().lower()
- self.listbox.delete(0, tk.END)
- current_directory = os.getcwd()
- for root, _, files in os.walk(current_directory):
- for file in files:
- if extension and not file.endswith(extension):
- continue
- if title and title not in file.lower():
- continue
- self.listbox.insert(tk.END, os.path.join(root, file))
- def open_file(self, event):
- """Open the selected file with the default application."""
- selected_file = self.listbox.get(self.listbox.curselection())
- try:
- if os.name == 'nt': # Windows
- os.startfile(selected_file)
- elif os.name == 'posix': # macOS, Linux
- subprocess.call(('open' if sys.platform == 'darwin' else 'xdg-open', selected_file))
- except Exception as e:
- messagebox.showerror("Error", f"Failed to open file: {e}")
- def save_file_list(self):
- """Save the list of files to File-list.txt."""
- save_path = asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
- if save_path:
- try:
- with open(save_path, 'w') as file_list:
- for i in range(self.listbox.size()):
- file_list.write(f"{self.listbox.get(i)}\n")
- messagebox.showinfo("Success", f"File list saved to {save_path}")
- except Exception as e:
- messagebox.showerror("Error", f"Failed to save file list: {e}")
- if __name__ == "__main__":
- root = tk.Tk()
- app = AdvancedSearchApp(root)
- root.geometry("800x500")
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement