Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox, Text, Scrollbar
- import sqlite3
- # Connect to the SQLite database
- def connect_db():
- conn = sqlite3.connect("ScriptsData.db")
- cursor = conn.cursor()
- cursor.execute("""
- CREATE TABLE IF NOT EXISTS scripts (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- name TEXT UNIQUE,
- content TEXT
- )
- """)
- conn.commit()
- conn.close()
- # Add a new script to the database
- def add_script():
- name = name_entry.get().strip()
- content = content_text.get("1.0", tk.END).strip()
- if name and content:
- try:
- conn = sqlite3.connect("ScriptsData.db")
- cursor = conn.cursor()
- cursor.execute("INSERT INTO scripts (name, content) VALUES (?, ?)", (name, content))
- conn.commit()
- conn.close()
- messagebox.showinfo("Success", "Script added successfully!")
- name_entry.delete(0, tk.END)
- content_text.delete("1.0", tk.END)
- show_all_scripts()
- except sqlite3.IntegrityError:
- messagebox.showerror("Error", "Script name already exists.")
- else:
- messagebox.showwarning("Input Error", "Please enter both name and content.")
- # Search for a script by name
- def search_script():
- name = search_entry.get().strip()
- if name:
- conn = sqlite3.connect("ScriptsData.db")
- cursor = conn.cursor()
- cursor.execute("SELECT content FROM scripts WHERE name = ?", (name,))
- result = cursor.fetchone()
- conn.close()
- if result:
- content_text.config(state="normal")
- content_text.delete("1.0", tk.END)
- content_text.insert(tk.END, result[0])
- content_text.config(state="normal") # Keep editable
- else:
- messagebox.showinfo("Not Found", "Script not found.")
- else:
- messagebox.showwarning("Input Error", "Please enter a script name to search.")
- # Show all scripts
- def show_all_scripts():
- conn = sqlite3.connect("ScriptsData.db")
- cursor = conn.cursor()
- cursor.execute("SELECT name FROM scripts")
- results = cursor.fetchall()
- conn.close()
- script_list.delete(0, tk.END)
- for row in results:
- script_list.insert(tk.END, row[0])
- # Display selected script content
- def display_script_content(event):
- try:
- selected_name = script_list.get(script_list.curselection())
- conn = sqlite3.connect("ScriptsData.db")
- cursor = conn.cursor()
- cursor.execute("SELECT content FROM scripts WHERE name = ?", (selected_name,))
- result = cursor.fetchone()
- conn.close()
- content_text.config(state="normal")
- content_text.delete("1.0", tk.END)
- if result:
- content_text.insert(tk.END, result[0])
- content_text.config(state="normal") # Keep editable
- except tk.TclError:
- pass # No item selected
- # Save the edited content back to the database
- def save_script():
- if script_list.curselection():
- selected_name = script_list.get(script_list.curselection())
- new_content = content_text.get("1.0", tk.END).strip()
- if new_content:
- conn = sqlite3.connect("ScriptsData.db")
- cursor = conn.cursor()
- cursor.execute("UPDATE scripts SET content = ? WHERE name = ?", (new_content, selected_name))
- conn.commit()
- conn.close()
- messagebox.showinfo("Success", "Script updated successfully!")
- else:
- messagebox.showwarning("Input Error", "Content cannot be empty.")
- else:
- messagebox.showwarning("Selection Error", "Please select a script to save changes.")
- # Delete the selected script from the database
- def delete_script():
- if script_list.curselection():
- selected_name = script_list.get(script_list.curselection())
- confirm = messagebox.askyesno("Confirm Delete", f"Are you sure you want to delete '{selected_name}'?")
- if confirm:
- conn = sqlite3.connect("ScriptsData.db")
- cursor = conn.cursor()
- cursor.execute("DELETE FROM scripts WHERE name = ?", (selected_name,))
- conn.commit()
- conn.close()
- messagebox.showinfo("Success", "Script deleted successfully!")
- show_all_scripts() # Refresh the script list
- content_text.config(state="normal")
- content_text.delete("1.0", tk.END) # Clear the content field
- content_text.config(state="normal") # Keep editable
- else:
- messagebox.showwarning("Selection Error", "Please select a script first to delete.")
- # Copy content_text field to clipboard
- def copy_to_clipboard():
- content = content_text.get("1.0", tk.END).strip()
- if content:
- root.clipboard_clear() # Clear the clipboard
- root.clipboard_append(content) # Append content to the clipboard
- messagebox.showinfo("Success", "Content copied to clipboard!")
- else:
- messagebox.showwarning("Clipboard Error", "No content to copy.")
- # Clear content_text field
- def clear_content():
- content_text.delete("1.0", tk.END)
- # GUI Setup
- connect_db()
- root = tk.Tk()
- root.title("Najeeb Scripts Database")
- root.geometry("900x620")
- root.configure(bg="#f0f0f5")
- # Top Row: Search, Add, Show All in One Line
- style_label = {"bg": "#f0f0f5", "fg": "#333333", "font": ("Arial", 11)}
- style_button = {"bg": "#4CAF50", "fg": "white", "font": ("Arial", 10, "bold"), "width": 14, "padx": 2, "pady": 2}
- tk.Label(root, text="Search Script Name:", **style_label).grid(row=0, column=3, padx=5, pady=5, sticky="w")
- search_entry = tk.Entry(root, width=15, font=("Arial", 11))
- search_entry.grid(row=0, column=4, padx=5, pady=5, sticky="w")
- search_button = tk.Button(root, text="Search", command=search_script, **style_button)
- search_button.grid(row=0, column=5, padx=5, pady=5, sticky="w")
- tk.Label(root, text="Add New Script DataBase:", **style_label).grid(row=0, column=0, padx=5, pady=5, sticky="w")
- name_entry = tk.Entry(root, width=15, font=("Arial", 11))
- name_entry.grid(row=0, column=1, padx=5, pady=5, sticky="w")
- add_button = tk.Button(root, text="Add Script", command=add_script, **style_button)
- add_button.grid(row=0, column=2, padx=5, pady=5, sticky="w")
- show_all_button = tk.Button(root, text="Show All Scripts", command=show_all_scripts, **style_button)
- show_all_button.grid(row=1, column=0, padx=5, pady=5, sticky="w")
- # Save Button
- save_button = tk.Button(root, text="Save", command=save_script, **style_button)
- save_button.grid(row=1, column=1, padx=5, pady=5, sticky="w")
- # Second Row: Delete
- delete_button = tk.Button(root, text="Delete", command=delete_script, **style_button)
- delete_button.grid(row=1, column=2, padx=5, pady=5, sticky="w")
- # Copy to Clipboard Button
- copy_button = tk.Button(root, text="Copy to Clipboard", command=copy_to_clipboard, **style_button)
- copy_button.grid(row=1, column=3, padx=3, pady=5, sticky="w")
- # Clear Content Button
- clear_button = tk.Button(root, text="Clear Content", command=clear_content, **style_button)
- clear_button.grid(row=1, column=4, padx=3, pady=5, sticky="w")
- # Script List
- list_frame = tk.Frame(root)
- list_frame.grid(row=2, column=0, rowspan=6, padx=5, pady=5, sticky="n")
- script_list = tk.Listbox(list_frame, width=20, height=29, font=("Arial", 11))
- script_list.pack(side="left", fill="y")
- scrollbar = Scrollbar(list_frame, orient="vertical", command=script_list.yview)
- scrollbar.pack(side="right", fill="y")
- script_list.config(yscrollcommand=scrollbar.set)
- script_list.bind("<<ListboxSelect>>", display_script_content)
- # Content Field
- content_frame = tk.Frame(root)
- content_frame.grid(row=2, column=1, rowspan=6, columnspan=6, padx=5, pady=5, sticky="n")
- content_text = Text(content_frame, width=85, height=30, font=("Arial", 11), wrap="word", state="normal") # Editable
- content_text.pack(side="left", fill="both", expand=True)
- content_scrollbar = Scrollbar(content_frame, orient="vertical", command=content_text.yview)
- content_scrollbar.pack(side="right", fill="y")
- content_text.config(yscrollcommand=content_scrollbar.set)
- # Run main loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement