Advertisement
Najeebsk

SCRIPTSDATA.pyw

Oct 25th, 2024 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.15 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox, Text, Scrollbar
  3. import sqlite3
  4.  
  5. # Connect to the SQLite database
  6. def connect_db():
  7.     conn = sqlite3.connect("ScriptsData.db")
  8.     cursor = conn.cursor()
  9.     cursor.execute("""
  10.    CREATE TABLE IF NOT EXISTS scripts (
  11.        id INTEGER PRIMARY KEY AUTOINCREMENT,
  12.        name TEXT UNIQUE,
  13.        content TEXT
  14.    )
  15.    """)
  16.     conn.commit()
  17.     conn.close()
  18.  
  19. # Add a new script to the database
  20. def add_script():
  21.     name = name_entry.get().strip()
  22.     content = content_text.get("1.0", tk.END).strip()
  23.     if name and content:
  24.         try:
  25.             conn = sqlite3.connect("ScriptsData.db")
  26.             cursor = conn.cursor()
  27.             cursor.execute("INSERT INTO scripts (name, content) VALUES (?, ?)", (name, content))
  28.             conn.commit()
  29.             conn.close()
  30.             messagebox.showinfo("Success", "Script added successfully!")
  31.             name_entry.delete(0, tk.END)
  32.             content_text.delete("1.0", tk.END)
  33.             show_all_scripts()
  34.         except sqlite3.IntegrityError:
  35.             messagebox.showerror("Error", "Script name already exists.")
  36.     else:
  37.         messagebox.showwarning("Input Error", "Please enter both name and content.")
  38.  
  39. # Search for a script by name
  40. def search_script():
  41.     name = search_entry.get().strip()
  42.     if name:
  43.         conn = sqlite3.connect("ScriptsData.db")
  44.         cursor = conn.cursor()
  45.         cursor.execute("SELECT content FROM scripts WHERE name = ?", (name,))
  46.         result = cursor.fetchone()
  47.         conn.close()
  48.         if result:
  49.             content_text.config(state="normal")
  50.             content_text.delete("1.0", tk.END)
  51.             content_text.insert(tk.END, result[0])
  52.             content_text.config(state="normal")  # Keep editable
  53.         else:
  54.             messagebox.showinfo("Not Found", "Script not found.")
  55.     else:
  56.         messagebox.showwarning("Input Error", "Please enter a script name to search.")
  57.  
  58. # Show all scripts
  59. def show_all_scripts():
  60.     conn = sqlite3.connect("ScriptsData.db")
  61.     cursor = conn.cursor()
  62.     cursor.execute("SELECT name FROM scripts")
  63.     results = cursor.fetchall()
  64.     conn.close()
  65.     script_list.delete(0, tk.END)
  66.     for row in results:
  67.         script_list.insert(tk.END, row[0])
  68.  
  69. # Display selected script content
  70. def display_script_content(event):
  71.     try:
  72.         selected_name = script_list.get(script_list.curselection())
  73.         conn = sqlite3.connect("ScriptsData.db")
  74.         cursor = conn.cursor()
  75.         cursor.execute("SELECT content FROM scripts WHERE name = ?", (selected_name,))
  76.         result = cursor.fetchone()
  77.         conn.close()
  78.         content_text.config(state="normal")
  79.         content_text.delete("1.0", tk.END)
  80.         if result:
  81.             content_text.insert(tk.END, result[0])
  82.         content_text.config(state="normal")  # Keep editable
  83.     except tk.TclError:
  84.         pass  # No item selected
  85.  
  86. # Save the edited content back to the database
  87. def save_script():
  88.     if script_list.curselection():
  89.         selected_name = script_list.get(script_list.curselection())
  90.         new_content = content_text.get("1.0", tk.END).strip()
  91.         if new_content:
  92.             conn = sqlite3.connect("ScriptsData.db")
  93.             cursor = conn.cursor()
  94.             cursor.execute("UPDATE scripts SET content = ? WHERE name = ?", (new_content, selected_name))
  95.             conn.commit()
  96.             conn.close()
  97.             messagebox.showinfo("Success", "Script updated successfully!")
  98.         else:
  99.             messagebox.showwarning("Input Error", "Content cannot be empty.")
  100.     else:
  101.         messagebox.showwarning("Selection Error", "Please select a script to save changes.")
  102.  
  103. # Delete the selected script from the database
  104. def delete_script():
  105.     if script_list.curselection():
  106.         selected_name = script_list.get(script_list.curselection())
  107.         confirm = messagebox.askyesno("Confirm Delete", f"Are you sure you want to delete '{selected_name}'?")
  108.         if confirm:
  109.             conn = sqlite3.connect("ScriptsData.db")
  110.             cursor = conn.cursor()
  111.             cursor.execute("DELETE FROM scripts WHERE name = ?", (selected_name,))
  112.             conn.commit()
  113.             conn.close()
  114.             messagebox.showinfo("Success", "Script deleted successfully!")
  115.             show_all_scripts()  # Refresh the script list
  116.             content_text.config(state="normal")
  117.             content_text.delete("1.0", tk.END)  # Clear the content field
  118.             content_text.config(state="normal")  # Keep editable
  119.     else:
  120.         messagebox.showwarning("Selection Error", "Please select a script first to delete.")
  121.  
  122. # Copy content_text field to clipboard
  123. def copy_to_clipboard():
  124.     content = content_text.get("1.0", tk.END).strip()
  125.     if content:
  126.         root.clipboard_clear()  # Clear the clipboard
  127.         root.clipboard_append(content)  # Append content to the clipboard
  128.         messagebox.showinfo("Success", "Content copied to clipboard!")
  129.     else:
  130.         messagebox.showwarning("Clipboard Error", "No content to copy.")
  131.        
  132. # Clear content_text field
  133. def clear_content():
  134.     content_text.delete("1.0", tk.END)
  135.    
  136. # GUI Setup
  137. connect_db()
  138. root = tk.Tk()
  139. root.title("Najeeb Scripts Database")
  140. root.geometry("900x620")
  141. root.configure(bg="#f0f0f5")
  142.  
  143. # Top Row: Search, Add, Show All in One Line
  144. style_label = {"bg": "#f0f0f5", "fg": "#333333", "font": ("Arial", 11)}
  145. style_button = {"bg": "#4CAF50", "fg": "white", "font": ("Arial", 10, "bold"), "width": 14, "padx": 2, "pady": 2}
  146.  
  147. tk.Label(root, text="Search Script Name:", **style_label).grid(row=0, column=3, padx=5, pady=5, sticky="w")
  148. search_entry = tk.Entry(root, width=15, font=("Arial", 11))
  149. search_entry.grid(row=0, column=4, padx=5, pady=5, sticky="w")
  150.  
  151. search_button = tk.Button(root, text="Search", command=search_script, **style_button)
  152. search_button.grid(row=0, column=5, padx=5, pady=5, sticky="w")
  153.  
  154. tk.Label(root, text="Add New Script DataBase:", **style_label).grid(row=0, column=0, padx=5, pady=5, sticky="w")
  155. name_entry = tk.Entry(root, width=15, font=("Arial", 11))
  156. name_entry.grid(row=0, column=1, padx=5, pady=5, sticky="w")
  157.  
  158. add_button = tk.Button(root, text="Add Script", command=add_script, **style_button)
  159. add_button.grid(row=0, column=2, padx=5, pady=5, sticky="w")
  160.  
  161. show_all_button = tk.Button(root, text="Show All Scripts", command=show_all_scripts, **style_button)
  162. show_all_button.grid(row=1, column=0, padx=5, pady=5, sticky="w")
  163.  
  164. # Save Button
  165. save_button = tk.Button(root, text="Save", command=save_script, **style_button)
  166. save_button.grid(row=1, column=1, padx=5, pady=5, sticky="w")
  167.  
  168. # Second Row: Delete
  169. delete_button = tk.Button(root, text="Delete", command=delete_script, **style_button)
  170. delete_button.grid(row=1, column=2, padx=5, pady=5, sticky="w")
  171.  
  172. # Copy to Clipboard Button
  173. copy_button = tk.Button(root, text="Copy to Clipboard", command=copy_to_clipboard, **style_button)
  174. copy_button.grid(row=1, column=3, padx=3, pady=5, sticky="w")
  175.  
  176. # Clear Content Button
  177. clear_button = tk.Button(root, text="Clear Content", command=clear_content, **style_button)
  178. clear_button.grid(row=1, column=4, padx=3, pady=5, sticky="w")
  179.  
  180. # Script List
  181. list_frame = tk.Frame(root)
  182. list_frame.grid(row=2, column=0, rowspan=6, padx=5, pady=5, sticky="n")
  183. script_list = tk.Listbox(list_frame, width=20, height=29, font=("Arial", 11))
  184. script_list.pack(side="left", fill="y")
  185. scrollbar = Scrollbar(list_frame, orient="vertical", command=script_list.yview)
  186. scrollbar.pack(side="right", fill="y")
  187. script_list.config(yscrollcommand=scrollbar.set)
  188. script_list.bind("<<ListboxSelect>>", display_script_content)
  189.  
  190. # Content Field
  191. content_frame = tk.Frame(root)
  192. content_frame.grid(row=2, column=1, rowspan=6, columnspan=6, padx=5, pady=5, sticky="n")
  193. content_text = Text(content_frame, width=85, height=30, font=("Arial", 11), wrap="word", state="normal")  # Editable
  194. content_text.pack(side="left", fill="both", expand=True)
  195. content_scrollbar = Scrollbar(content_frame, orient="vertical", command=content_text.yview)
  196. content_scrollbar.pack(side="right", fill="y")
  197. content_text.config(yscrollcommand=content_scrollbar.set)
  198.  
  199. # Run main loop
  200. root.mainloop()
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement