SHARE
    TWEET
    Najeebsk

    ADD-RUN-DATABASE.pyw

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

    AltStyle によって変換されたページ (->オリジナル) /