Advertisement
Najeebsk

Notes-Management.pyw

Oct 23rd, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.10 KB | None | 0 0
  1. import sys
  2. import sqlite3
  3. import os
  4. import subprocess
  5. from tkinter import *
  6. from tkinter import ttk, scrolledtext, filedialog, messagebox
  7.  
  8. class NotepadsManagementApp:
  9.     def __init__(self, root):
  10.         self.root = root
  11.         self.connection = None
  12.         self.cursor = None
  13.         self.results = []
  14.         self.index = 0
  15.         self.current_note_id = None  # To track the current note ID
  16.  
  17.         # GUI Setup
  18.         self.setup_ui()
  19.  
  20.     def setup_ui(self):
  21.         self.root.title("Najeeb Notes Management")
  22.         self.root.geometry("900x620")
  23.         self.root.resizable(False, False)
  24.  
  25.         self.style = ttk.Style()
  26.         if sys.platform == "win32":
  27.             self.style.theme_use("winnative")
  28.  
  29.         self.notebook = ttk.Notebook(self.root)
  30.         self.notebook.place(relx=0.02, rely=0.02, relheight=0.90, relwidth=0.97)
  31.  
  32.         # Tab 1 - Add
  33.         self.tab_add = Frame(self.notebook)
  34.         self.notebook.add(self.tab_add, text="Add")
  35.         self.setup_add_tab()
  36.  
  37.         # Tab 2 - Display
  38.         self.tab_display = Frame(self.notebook)
  39.         self.notebook.add(self.tab_display, text="Display")
  40.         self.setup_display_tab()
  41.  
  42.         # Tab 3 - Create DB
  43.         self.tab_create = Frame(self.notebook)
  44.         self.notebook.add(self.tab_create, text="Create")
  45.         self.setup_create_tab()
  46.  
  47.         # Exit Button
  48.         self.exit_button = Button(self.root, text="Exit", command=self.exit_app)
  49.         self.exit_button.place(relx=0.4, rely=0.94, height=26, width=117)
  50.  
  51.         self.error_output = Label(self.root, text="", fg="red")
  52.         self.error_output.place(relx=0.03, rely=0.94, height=18, width=206)
  53.  
  54.     def setup_add_tab(self):
  55.         Label(self.tab_add, text="Title Note").place(relx=0.02, rely=0.09)
  56.         self.input_title = Entry(self.tab_add)
  57.         self.input_title.place(relx=0.09, rely=0.08, height=30, relwidth=0.7)
  58.  
  59.         Label(self.tab_add, text="Notice:").place(relx=0.02, rely=0.22)
  60.         self.input_notice = scrolledtext.ScrolledText(self.tab_add)
  61.         self.input_notice.place(relx=0.02, rely=0.28, relheight=0.64, relwidth=0.87)
  62.  
  63.         add_button = Button(self.tab_add, text="Add", command=self.add_note)
  64.         add_button.place(relx=0.90, rely=0.28, height=30, width=80)
  65.  
  66.         clear_button = Button(self.tab_add, text="Clear", command=self.clear_note)
  67.         clear_button.place(relx=0.90, rely=0.39, height=30, width=80)
  68.  
  69.     def setup_display_tab(self):
  70.         Label(self.tab_display, text="Title Note").place(relx=0.02, rely=0.08)
  71.         self.input_search_title = Entry(self.tab_display)
  72.         self.input_search_title.place(relx=0.09, rely=0.07, height=30, relwidth=0.76)
  73.  
  74.         self.output_notice = scrolledtext.ScrolledText(self.tab_display)
  75.         self.output_notice.place(relx=0.02, rely=0.20, relheight=0.76, relwidth=0.87)
  76.  
  77.         search_button = Button(self.tab_display, text="Search", command=self.search_notes)
  78.         search_button.place(relx=0.90, rely=0.07, height=30, width=80)
  79.  
  80.         next_button = Button(self.tab_display, text="Next", command=self.next_note)
  81.         next_button.place(relx=0.90, rely=0.30, height=30, width=80)
  82.  
  83.         back_button = Button(self.tab_display, text="Back", command=self.previous_note)
  84.         back_button.place(relx=0.90, rely=0.38, height=30, width=80)
  85.  
  86.         delete_button = Button(self.tab_display, text="Delete", command=self.delete_note)
  87.         delete_button.place(relx=0.90, rely=0.46, height=30, width=80)
  88.  
  89.         # Run Button to Execute Text as Command
  90.         run_button = Button(self.tab_display, text="Select Run", command=self.run_selected_text)
  91.         run_button.place(relx=0.90, rely=0.22, height=30, width=80)
  92.  
  93.         # New Search Field for Highlighting
  94.         Label(self.tab_display, text="Search in Note:").place(relx=0.02, rely=0.14)
  95.         self.search_in_note = Entry(self.tab_display)
  96.         self.search_in_note.place(relx=0.14, rely=0.14, height=25, relwidth=0.7)
  97.  
  98.         highlight_button = Button(self.tab_display, text="Highlight", command=self.highlight_text_in_note)
  99.         highlight_button.place(relx=0.90, rely=0.14, height=30, width=80)
  100.  
  101.         # Edit and Save Buttons
  102.         edit_button = Button(self.tab_display, text="Edit", command=self.edit_note)
  103.         edit_button.place(relx=0.90, rely=0.54, height=30, width=80)
  104.  
  105.         save_button = Button(self.tab_display, text="Save", command=self.save_note)
  106.         save_button.place(relx=0.90, rely=0.62, height=30, width=80)
  107.  
  108.         # Video Play Buttons for VLC and PotPlayer
  109.         play_vlc_button = Button(self.tab_display, text="VLCPlayer", command=lambda: self.play_video("vlc"))
  110.         play_vlc_button.place(relx=0.90, rely=0.70, height=30, width=80)
  111.  
  112.         play_cmd_button = Button(self.tab_display, text="RunCMD", command=self.run_cmd)
  113.         play_cmd_button.place(relx=0.90, rely=0.78, height=30, width=80)
  114.  
  115.     def setup_create_tab(self):
  116.         Label(self.tab_create, text="For creating a new notepads database").place(relx=0.09, rely=0.14)
  117.         create_button = Button(self.tab_create, text="Create", command=self.create_db)
  118.         create_button.place(relx=0.22, rely=0.25, height=30, width=100)
  119.  
  120.         browse_button = Button(self.tab_create, text="Browse Database", command=self.browse_db)
  121.         browse_button.place(relx=0.22, rely=0.35, height=30, width=150)
  122.  
  123.     # Database Functions
  124.     def connect_db(self, db_path):
  125.         try:
  126.             self.connection = sqlite3.connect(db_path)
  127.             self.cursor = self.connection.cursor()
  128.             self.error_output.configure(text="Connected to database.")
  129.         except Exception as e:
  130.             self.error_output.configure(text="Failed to connect: " + str(e))
  131.  
  132.     def create_db(self):
  133.         if not self.cursor:
  134.             self.error_output.configure(text="Please select a database.")
  135.             return
  136.         try:
  137.             self.cursor.execute("""
  138.                CREATE TABLE IF NOT EXISTS notes (
  139.                    id INTEGER PRIMARY KEY,
  140.                    title TEXT,
  141.                    note TEXT
  142.                );""")
  143.             self.connection.commit()
  144.             self.error_output.configure(text="Database created successfully!")
  145.         except Exception as e:
  146.             self.error_output.configure(text=str(e))
  147.  
  148.     def browse_db(self):
  149.         db_path = filedialog.askopenfilename(filetypes=[("SQLite DB", "*.db")])
  150.         if db_path:
  151.             self.connect_db(db_path)
  152.  
  153.     def add_note(self):
  154.         if not self.cursor:
  155.             self.error_output.configure(text="Please select a database.")
  156.             return
  157.         title = self.input_title.get()
  158.         note = self.input_notice.get("1.0", END).strip()
  159.         if title and note:
  160.             try:
  161.                 self.cursor.execute("INSERT INTO notes (title, note) VALUES (?, ?)", (title, note))
  162.                 self.connection.commit()
  163.                 self.error_output.configure(text="Note added successfully!")
  164.             except Exception as e:
  165.                 self.error_output.configure(text="Failed to add note: " + str(e))
  166.         else:
  167.             self.error_output.configure(text="Please fill in both fields.")
  168.  
  169.     def search_notes(self):
  170.         if not self.cursor:
  171.             self.error_output.configure(text="Please select a database.")
  172.             return
  173.         search_title = self.input_search_title.get()
  174.         if search_title:
  175.             try:
  176.                 self.cursor.execute("SELECT * FROM notes WHERE title LIKE ?", ('%' + search_title + '%',))
  177.                 self.results = self.cursor.fetchall()
  178.                 self.error_output.configure(text=f"{len(self.results)} result(s) found")
  179.                 self.index = 0
  180.                 if self.results:
  181.                     self.show_note()
  182.             except Exception as e:
  183.                 self.error_output.configure(text="Search failed: " + str(e))
  184.  
  185.     def show_note(self):
  186.         """Display the current note and set the current note's ID."""
  187.         if self.results:
  188.             self.output_notice.config(state=NORMAL)
  189.             self.output_notice.delete(1.0, END)
  190.             self.output_notice.insert(END, self.results[self.index][2])
  191.             self.current_note_id = self.results[self.index][0]  # Track the ID of the current note
  192.             self.output_notice.config(state=DISABLED)  # Make output read-only again
  193.  
  194.     def next_note(self):
  195.         """Move to the next note in the search results."""
  196.         if self.results and self.index < len(self.results) - 1:
  197.             self.index += 1
  198.             self.show_note()
  199.  
  200.     def previous_note(self):
  201.         """Move to the previous note in the search results."""
  202.         if self.results and self.index > 0:
  203.             self.index -= 1
  204.             self.show_note()
  205.  
  206.     def delete_note(self):
  207.         """Delete the current note from the database."""
  208.         if self.cursor and self.current_note_id:
  209.             try:
  210.                 self.cursor.execute("DELETE FROM notes WHERE id=?", (self.current_note_id,))
  211.                 self.connection.commit()
  212.                 self.error_output.configure(text="Note deleted.")
  213.                 self.results.pop(self.index)
  214.                 self.index = min(self.index, len(self.results) - 1)  # Ensure index is valid
  215.                 self.show_note()
  216.             except Exception as e:
  217.                 self.error_output.configure(text="Failed to delete: " + str(e))
  218.  
  219.     def clear_note(self):
  220.         self.input_title.delete(0, END)
  221.         self.input_notice.delete(1.0, END)
  222.  
  223.     def run_selected_text(self):
  224.         """Run the selected text from the output_notice field as a command."""
  225.         selected_text = self.output_notice.get(SEL_FIRST, SEL_LAST)
  226.         if selected_text:
  227.             try:
  228.                 subprocess.run(selected_text, shell=True)
  229.             except Exception as e:
  230.                 messagebox.showerror("Error", str(e))
  231.  
  232.     def highlight_text_in_note(self):
  233.         """Highlight all occurrences of a keyword in the note text."""
  234.         keyword = self.search_in_note.get()
  235.         self.output_notice.tag_remove("highlight", 1.0, END)
  236.         if keyword:
  237.             start_pos = "1.0"
  238.             while True:
  239.                 start_pos = self.output_notice.search(keyword, start_pos, stopindex=END)
  240.                 if not start_pos:
  241.                     break
  242.                 end_pos = f"{start_pos}+{len(keyword)}c"
  243.                 self.output_notice.tag_add("highlight", start_pos, end_pos)
  244.                 self.output_notice.tag_config("highlight", background="yellow", foreground="black")
  245.                 start_pos = end_pos
  246.  
  247.     def edit_note(self):
  248.         """Allow editing of the current note."""
  249.         self.output_notice.config(state=NORMAL)
  250.  
  251.     def save_note(self):
  252.         """Save the edited note back to the database."""
  253.         if self.cursor and self.current_note_id:
  254.             updated_note = self.output_notice.get(1.0, END).strip()
  255.             try:
  256.                 self.cursor.execute("UPDATE notes SET note=? WHERE id=?", (updated_note, self.current_note_id))
  257.                 self.connection.commit()
  258.                 self.error_output.configure(text="Note updated.")
  259.             except Exception as e:
  260.                 self.error_output.configure(text="Failed to save: " + str(e))
  261.  
  262.     def play_video(self, player):
  263.         selected_url = self.output_notice.get(SEL_FIRST, SEL_LAST).strip()
  264.         if selected_url:
  265.             if player == "vlc":
  266.                 subprocess.run([r"C:\Program Files\VideoLAN\VLC\vlc.exe", selected_url])
  267.             elif player == "potplayer":
  268.                 subprocess.run([r"C:\Program Files\DAUM\PotPlayer\PotPlayerMini.exe", selected_url])
  269.  
  270.  
  271.     def run_cmd(self):
  272.         """Run a command in CMD based on the selected text."""
  273.         selected_text = self.output_notice.get(SEL_FIRST, SEL_LAST)
  274.         if selected_text:
  275.             subprocess.run(selected_text, shell=True)
  276.  
  277.     def exit_app(self):
  278.         """Gracefully close the application."""
  279.         if self.connection:
  280.             self.connection.close()
  281.         self.root.quit()
  282.  
  283. if __name__ == "__main__":
  284.     root = Tk()
  285.     app = NotepadsManagementApp(root)
  286.     root.mainloop()
  287.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement