Advertisement
Najeebsk

ADD-RUN-DATABASE.pyw

Nov 1st, 2024 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.14 KB | None | 0 0
  1. import os
  2. import sqlite3
  3. import tkinter as tk
  4. from tkinter import filedialog, messagebox, scrolledtext
  5. import subprocess
  6.  
  7. class FileDatabaseApp:
  8.     def __init__(self, root):
  9.         self.root = root
  10.         self.root.title("Najeeb Add Folder Files Database and Browse Data And Run")
  11.         self.root.geometry("1000x700")
  12.         self.root.config(bg="lightblue")
  13.  
  14.         # Font configuration
  15.         self.big_font = ("Arial", 12, "bold")
  16.  
  17.         # Database connection placeholders
  18.         self.conn = None
  19.         self.cursor = None
  20.  
  21.         # GUI setup
  22.         self.setup_gui()
  23.  
  24.     def setup_gui(self):
  25.         """Set up the GUI elements."""
  26.         # Row 0: Database Name and Browse Folder
  27.         self.db_label = tk.Label(self.root, text="Enter Database Name(.db):", font=self.big_font, bg="white")
  28.         self.db_label.grid(row=0, column=0, padx=10, pady=10, sticky='w')
  29.  
  30.         self.db_entry = tk.Entry(self.root, width=40, font=self.big_font)
  31.         self.db_entry.insert(0, "DATA.db")
  32.         self.db_entry.grid(row=0, column=1, padx=10, pady=10, sticky='w')
  33.  
  34.         self.folder_label = tk.Label(self.root, text="Select Folder to Add Files:", font=self.big_font, bg="white")
  35.         self.folder_label.grid(row=1, column=0, padx=10, pady=10, sticky='w')
  36.  
  37.         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)
  38.         self.browse_button.grid(row=1, column=1, padx=10, pady=10, sticky='w')
  39.  
  40.         # Row 2: Database Selection Button
  41.         self.db_select_label = tk.Label(self.root, text="Select Database File(.db):", font=self.big_font, bg="white")
  42.         self.db_select_label.grid(row=2, column=0, padx=10, pady=10, sticky='w')
  43.  
  44.         self.db_button = tk.Button(self.root, text="Browse Database", font=self.big_font, command=self.browse_database, bg="#6383d6", fg="white", width=16)
  45.         self.db_button.grid(row=2, column=1, padx=10, pady=10, sticky='w')
  46.  
  47.         # Row 3: Listbox with Scrollbar for file display
  48.         listbox_frame = tk.Frame(self.root)
  49.         listbox_frame.grid(row=3, column=0, padx=10, pady=2, sticky='w')
  50.  
  51.         self.file_listbox = tk.Listbox(listbox_frame, width=36, height=24)
  52.         self.file_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  53.         self.file_listbox.bind("<<ListboxSelect>>", self.display_file_content)
  54.  
  55.         self.listbox_scrollbar = tk.Scrollbar(listbox_frame)
  56.         self.listbox_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  57.         self.file_listbox.config(yscrollcommand=self.listbox_scrollbar.set)
  58.         self.listbox_scrollbar.config(command=self.file_listbox.yview)
  59.  
  60.         # Row 3: Display content area
  61.         self.content_display = scrolledtext.ScrolledText(self.root, width=78, height=20, font=self.big_font, wrap=tk.WORD)
  62.         self.content_display.grid(row=3, column=1, padx=10, pady=2)
  63.  
  64.         # Row 4: Buttons and Command Entry
  65.         self.button_frame = tk.Frame(self.root, bg="lightblue")
  66.         self.button_frame.grid(row=4, column=0, columnspan=2, pady=10)
  67.  
  68.         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)
  69.         self.copy_button.grid(row=0, column=0, padx=10, pady=5)
  70.  
  71.         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)
  72.         self.open_button.grid(row=0, column=1, padx=10, pady=5)
  73.  
  74.         self.command_entry = tk.Entry(self.button_frame, font=self.big_font, width=85)
  75.         self.command_entry.grid(row=1, column=0, padx=10, pady=5)
  76.         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)
  77.         self.run_cmd_button.grid(row=1, column=1, padx=10, pady=5)
  78.  
  79.         # Search Script field and button
  80.         self.search_entry = tk.Entry(self.button_frame, font=self.big_font, width=85)
  81.         self.search_entry.grid(row=2, column=0, padx=10, pady=5)
  82.         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)
  83.         self.search_button.grid(row=2, column=1, padx=10, pady=5)
  84.  
  85.     def browse_database(self):
  86.         """Browse for an SQLite database file and populate the file list."""
  87.         db_path = filedialog.askopenfilename(filetypes=[("SQLite Database", "*.db")])
  88.         if db_path:
  89.             self.init_db(db_path)
  90.             self.populate_file_list()
  91.  
  92.     def init_db(self, db_name):
  93.         """Create or connect to the database."""
  94.         if self.conn:
  95.             self.conn.close()
  96.         self.conn = sqlite3.connect(db_name)
  97.         self.cursor = self.conn.cursor()
  98.         self.cursor.execute('''CREATE TABLE IF NOT EXISTS Scripts (
  99.                                id INTEGER PRIMARY KEY AUTOINCREMENT,
  100.                                name TEXT,
  101.                                path TEXT,
  102.                                content BLOB)''')
  103.         self.conn.commit()
  104.  
  105.     def browse_folder(self):
  106.         """Browse for a folder and add all files to the database."""
  107.         db_name = self.db_entry.get().strip() or "data.db"
  108.         self.init_db(db_name)
  109.  
  110.         folder_path = filedialog.askdirectory()
  111.         if folder_path:
  112.             files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
  113.             for file_name in files:
  114.                 file_path = os.path.join(folder_path, file_name)
  115.                 try:
  116.                     with open(file_path, 'rb') as file:
  117.                         content = file.read()
  118.                     self.cursor.execute("INSERT INTO Scripts (name, path, content) VALUES (?, ?, ?)", (file_name, file_path, content))
  119.                 except Exception as e:
  120.                     messagebox.showerror("Error", f"Error reading file '{file_name}': {e}")
  121.                     continue
  122.             self.conn.commit()
  123.             self.populate_file_list()
  124.             messagebox.showinfo("Success", "All files added to the database!")
  125.  
  126.     def populate_file_list(self):
  127.         """Populate the listbox with file names from the database."""
  128.         self.file_listbox.delete(0, tk.END)
  129.         try:
  130.             self.cursor.execute("SELECT name FROM Scripts")
  131.             files = self.cursor.fetchall()
  132.             for file in files:
  133.                 self.file_listbox.insert(tk.END, file[0])
  134.         except sqlite3.Error as e:
  135.             messagebox.showerror("Database Error", f"Failed to retrieve files: {e}")
  136.  
  137.     def display_file_content(self, event):
  138.         """Display content of the selected file in the text area."""
  139.         selected_index = self.file_listbox.curselection()
  140.         if not selected_index:
  141.             return
  142.         file_name = self.file_listbox.get(selected_index)
  143.         try:
  144.             self.cursor.execute("SELECT content FROM Scripts WHERE name=?", (file_name,))
  145.             file_content = self.cursor.fetchone()
  146.             if file_content:
  147.                 self.content_display.delete(1.0, tk.END)
  148.                 self.content_display.insert(tk.END, file_content[0])  # No decode needed here
  149.         except sqlite3.Error as e:
  150.             messagebox.showerror("Database Error", f"Failed to retrieve file content: {e}")
  151.     def open_file(self):
  152.         """Open the selected file in the default program."""
  153.         selected_index = self.file_listbox.curselection()
  154.         if not selected_index:
  155.             messagebox.showwarning("No Selection", "Please select a file to open.")
  156.             return
  157.         file_name = self.file_listbox.get(selected_index)
  158.         try:
  159.             self.cursor.execute("SELECT path FROM Scripts WHERE name=?", (file_name,))
  160.             file_path = self.cursor.fetchone()
  161.             if file_path and os.path.exists(file_path[0]):
  162.                 os.startfile(file_path[0])
  163.             else:
  164.                 messagebox.showerror("File Error", f"File '{file_name}' could not be found on disk.")
  165.         except sqlite3.Error as e:
  166.             messagebox.showerror("Database Error", f"Failed to retrieve file path: {e}")
  167.  
  168.     def run_cmd(self):
  169.         """Run a command from the command entry."""
  170.         command = self.command_entry.get()
  171.         subprocess.Popen(command, shell=True)
  172.  
  173.     def copy_to_clipboard(self):
  174.         """Copy the displayed content to the clipboard."""
  175.         content = self.content_display.get("1.0", tk.END)
  176.         self.root.clipboard_clear()
  177.         self.root.clipboard_append(content)
  178.         messagebox.showinfo("Success", "Content copied to clipboard.")
  179.  
  180.     def search_script(self):
  181.         """Search for a script by name."""
  182.         search_term = self.search_entry.get()
  183.         self.file_listbox.delete(0, tk.END)
  184.         self.cursor.execute("SELECT name FROM Scripts WHERE name LIKE ?", ('%' + search_term + '%',))
  185.         for row in self.cursor.fetchall():
  186.             self.file_listbox.insert(tk.END, row[0])
  187.        
  188.  
  189. # Run the application
  190. if __name__ == "__main__":
  191.     root = tk.Tk()
  192.     app = FileDatabaseApp(root)
  193.     root.mainloop()
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement