Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sqlite3
- import tkinter as tk
- from tkinter import filedialog, messagebox, scrolledtext
- import subprocess
- class FileDatabaseApp:
- def __init__(self, root):
- self.root = root
- self.root.title("Najeeb Add Folder Files Database and Browse Data And Run")
- self.root.geometry("1000x700")
- self.root.config(bg="lightblue")
- # Font configuration
- self.big_font = ("Arial", 12, "bold")
- # Database connection placeholders
- self.conn = None
- self.cursor = None
- # GUI setup
- self.setup_gui()
- def setup_gui(self):
- """Set up the GUI elements."""
- # Row 0: Database Name and Browse Folder
- self.db_label = tk.Label(self.root, text="Enter Database Name(.db):", font=self.big_font, bg="white")
- self.db_label.grid(row=0, column=0, padx=10, pady=10, sticky='w')
- self.db_entry = tk.Entry(self.root, width=40, font=self.big_font)
- self.db_entry.insert(0, "DATA.db")
- self.db_entry.grid(row=0, column=1, padx=10, pady=10, sticky='w')
- self.folder_label = tk.Label(self.root, text="Select Folder to Add Files:", font=self.big_font, bg="white")
- self.folder_label.grid(row=1, column=0, padx=10, pady=10, sticky='w')
- self.browse_button = tk.Button(self.root, text="Browse Add Folder", font=self.big_font, command=self.browse_folder, bg="#b0ac39", fg="white", width=16)
- self.browse_button.grid(row=1, column=1, padx=10, pady=10, sticky='w')
- # Row 2: Database Selection Button
- self.db_select_label = tk.Label(self.root, text="Select Database File(.db):", font=self.big_font, bg="white")
- self.db_select_label.grid(row=2, column=0, padx=10, pady=10, sticky='w')
- self.db_button = tk.Button(self.root, text="Browse Database", font=self.big_font, command=self.browse_database, bg="#6383d6", fg="white", width=16)
- self.db_button.grid(row=2, column=1, padx=10, pady=10, sticky='w')
- # Row 3: Listbox with Scrollbar for file display
- listbox_frame = tk.Frame(self.root)
- listbox_frame.grid(row=3, column=0, padx=10, pady=2, sticky='w')
- self.file_listbox = tk.Listbox(listbox_frame, width=36, height=24)
- self.file_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
- self.file_listbox.bind("<<ListboxSelect>>", self.display_file_content)
- self.listbox_scrollbar = tk.Scrollbar(listbox_frame)
- self.listbox_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- self.file_listbox.config(yscrollcommand=self.listbox_scrollbar.set)
- self.listbox_scrollbar.config(command=self.file_listbox.yview)
- # Row 3: Display content area
- self.content_display = scrolledtext.ScrolledText(self.root, width=78, height=20, font=self.big_font, wrap=tk.WORD)
- self.content_display.grid(row=3, column=1, padx=10, pady=2)
- # Row 4: Buttons and Command Entry
- self.button_frame = tk.Frame(self.root, bg="lightblue")
- self.button_frame.grid(row=4, column=0, columnspan=2, pady=10)
- self.copy_button = tk.Button(self.button_frame, text="Copy to Clipboard", font=self.big_font, command=self.copy_to_clipboard, bg="#f185f4", fg="white", width=16)
- self.copy_button.grid(row=0, column=0, padx=10, pady=5)
- self.open_button = tk.Button(self.button_frame, text="Open Default Program", font=self.big_font, command=self.open_file, bg="#c34545", fg="white", width=18)
- self.open_button.grid(row=0, column=1, padx=10, pady=5)
- self.command_entry = tk.Entry(self.button_frame, font=self.big_font, width=85)
- self.command_entry.grid(row=1, column=0, padx=10, pady=5)
- self.run_cmd_button = tk.Button(self.button_frame, text="Run Cmd", font=self.big_font, command=self.run_cmd, bg="#b31855", fg="white", width=16)
- self.run_cmd_button.grid(row=1, column=1, padx=10, pady=5)
- # Search Script field and button
- self.search_entry = tk.Entry(self.button_frame, font=self.big_font, width=85)
- self.search_entry.grid(row=2, column=0, padx=10, pady=5)
- self.search_button = tk.Button(self.button_frame, text="Search Script", font=self.big_font, command=self.search_script, bg="#18ab14", fg="white", width=16)
- self.search_button.grid(row=2, column=1, padx=10, pady=5)
- def browse_database(self):
- """Browse for an SQLite database file and populate the file list."""
- db_path = filedialog.askopenfilename(filetypes=[("SQLite Database", "*.db")])
- if db_path:
- self.init_db(db_path)
- self.populate_file_list()
- def init_db(self, db_name):
- """Create or connect to the database."""
- if self.conn:
- self.conn.close()
- self.conn = sqlite3.connect(db_name)
- self.cursor = self.conn.cursor()
- self.cursor.execute('''CREATE TABLE IF NOT EXISTS Scripts (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- name TEXT,
- path TEXT,
- content BLOB)''')
- self.conn.commit()
- def browse_folder(self):
- """Browse for a folder and add all files to the database."""
- db_name = self.db_entry.get().strip() or "data.db"
- self.init_db(db_name)
- folder_path = filedialog.askdirectory()
- if folder_path:
- files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
- for file_name in files:
- file_path = os.path.join(folder_path, file_name)
- try:
- with open(file_path, 'rb') as file:
- content = file.read()
- self.cursor.execute("INSERT INTO Scripts (name, path, content) VALUES (?, ?, ?)", (file_name, file_path, content))
- except Exception as e:
- messagebox.showerror("Error", f"Error reading file '{file_name}': {e}")
- continue
- self.conn.commit()
- self.populate_file_list()
- messagebox.showinfo("Success", "All files added to the database!")
- def populate_file_list(self):
- """Populate the listbox with file names from the database."""
- self.file_listbox.delete(0, tk.END)
- try:
- self.cursor.execute("SELECT name FROM Scripts")
- files = self.cursor.fetchall()
- for file in files:
- self.file_listbox.insert(tk.END, file[0])
- except sqlite3.Error as e:
- messagebox.showerror("Database Error", f"Failed to retrieve files: {e}")
- def display_file_content(self, event):
- """Display content of the selected file in the text area."""
- selected_index = self.file_listbox.curselection()
- if not selected_index:
- return
- file_name = self.file_listbox.get(selected_index)
- try:
- self.cursor.execute("SELECT content FROM Scripts WHERE name=?", (file_name,))
- file_content = self.cursor.fetchone()
- if file_content:
- self.content_display.delete(1.0, tk.END)
- self.content_display.insert(tk.END, file_content[0]) # No decode needed here
- except sqlite3.Error as e:
- messagebox.showerror("Database Error", f"Failed to retrieve file content: {e}")
- def open_file(self):
- """Open the selected file in the default program."""
- selected_index = self.file_listbox.curselection()
- if not selected_index:
- messagebox.showwarning("No Selection", "Please select a file to open.")
- return
- file_name = self.file_listbox.get(selected_index)
- try:
- self.cursor.execute("SELECT path FROM Scripts WHERE name=?", (file_name,))
- file_path = self.cursor.fetchone()
- if file_path and os.path.exists(file_path[0]):
- os.startfile(file_path[0])
- else:
- messagebox.showerror("File Error", f"File '{file_name}' could not be found on disk.")
- except sqlite3.Error as e:
- messagebox.showerror("Database Error", f"Failed to retrieve file path: {e}")
- def run_cmd(self):
- """Run a command from the command entry."""
- command = self.command_entry.get()
- subprocess.Popen(command, shell=True)
- def copy_to_clipboard(self):
- """Copy the displayed content to the clipboard."""
- content = self.content_display.get("1.0", tk.END)
- self.root.clipboard_clear()
- self.root.clipboard_append(content)
- messagebox.showinfo("Success", "Content copied to clipboard.")
- def search_script(self):
- """Search for a script by name."""
- search_term = self.search_entry.get()
- self.file_listbox.delete(0, tk.END)
- self.cursor.execute("SELECT name FROM Scripts WHERE name LIKE ?", ('%' + search_term + '%',))
- for row in self.cursor.fetchall():
- self.file_listbox.insert(tk.END, row[0])
- # Run the application
- if __name__ == "__main__":
- root = tk.Tk()
- app = FileDatabaseApp(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement