Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import sqlite3
- import os
- import subprocess
- from tkinter import *
- from tkinter import ttk, scrolledtext, filedialog, messagebox
- class NotepadsManagementApp:
- def __init__(self, root):
- self.root = root
- self.connection = None
- self.cursor = None
- self.results = []
- self.index = 0
- self.current_note_id = None # To track the current note ID
- # GUI Setup
- self.setup_ui()
- def setup_ui(self):
- self.root.title("Najeeb Notes Management")
- self.root.geometry("900x620")
- self.root.resizable(False, False)
- self.style = ttk.Style()
- if sys.platform == "win32":
- self.style.theme_use("winnative")
- self.notebook = ttk.Notebook(self.root)
- self.notebook.place(relx=0.02, rely=0.02, relheight=0.90, relwidth=0.97)
- # Tab 1 - Add
- self.tab_add = Frame(self.notebook)
- self.notebook.add(self.tab_add, text="Add")
- self.setup_add_tab()
- # Tab 2 - Display
- self.tab_display = Frame(self.notebook)
- self.notebook.add(self.tab_display, text="Display")
- self.setup_display_tab()
- # Tab 3 - Create DB
- self.tab_create = Frame(self.notebook)
- self.notebook.add(self.tab_create, text="Create")
- self.setup_create_tab()
- # Exit Button
- self.exit_button = Button(self.root, text="Exit", command=self.exit_app)
- self.exit_button.place(relx=0.4, rely=0.94, height=26, width=117)
- self.error_output = Label(self.root, text="", fg="red")
- self.error_output.place(relx=0.03, rely=0.94, height=18, width=206)
- def setup_add_tab(self):
- Label(self.tab_add, text="Title Note").place(relx=0.02, rely=0.09)
- self.input_title = Entry(self.tab_add)
- self.input_title.place(relx=0.09, rely=0.08, height=30, relwidth=0.7)
- Label(self.tab_add, text="Notice:").place(relx=0.02, rely=0.22)
- self.input_notice = scrolledtext.ScrolledText(self.tab_add)
- self.input_notice.place(relx=0.02, rely=0.28, relheight=0.64, relwidth=0.87)
- add_button = Button(self.tab_add, text="Add", command=self.add_note)
- add_button.place(relx=0.90, rely=0.28, height=30, width=80)
- clear_button = Button(self.tab_add, text="Clear", command=self.clear_note)
- clear_button.place(relx=0.90, rely=0.39, height=30, width=80)
- def setup_display_tab(self):
- Label(self.tab_display, text="Title Note").place(relx=0.02, rely=0.08)
- self.input_search_title = Entry(self.tab_display)
- self.input_search_title.place(relx=0.09, rely=0.07, height=30, relwidth=0.76)
- self.output_notice = scrolledtext.ScrolledText(self.tab_display)
- self.output_notice.place(relx=0.02, rely=0.20, relheight=0.76, relwidth=0.87)
- search_button = Button(self.tab_display, text="Search", command=self.search_notes)
- search_button.place(relx=0.90, rely=0.07, height=30, width=80)
- next_button = Button(self.tab_display, text="Next", command=self.next_note)
- next_button.place(relx=0.90, rely=0.30, height=30, width=80)
- back_button = Button(self.tab_display, text="Back", command=self.previous_note)
- back_button.place(relx=0.90, rely=0.38, height=30, width=80)
- delete_button = Button(self.tab_display, text="Delete", command=self.delete_note)
- delete_button.place(relx=0.90, rely=0.46, height=30, width=80)
- # Run Button to Execute Text as Command
- run_button = Button(self.tab_display, text="Select Run", command=self.run_selected_text)
- run_button.place(relx=0.90, rely=0.22, height=30, width=80)
- # New Search Field for Highlighting
- Label(self.tab_display, text="Search in Note:").place(relx=0.02, rely=0.14)
- self.search_in_note = Entry(self.tab_display)
- self.search_in_note.place(relx=0.14, rely=0.14, height=25, relwidth=0.7)
- highlight_button = Button(self.tab_display, text="Highlight", command=self.highlight_text_in_note)
- highlight_button.place(relx=0.90, rely=0.14, height=30, width=80)
- # Edit and Save Buttons
- edit_button = Button(self.tab_display, text="Edit", command=self.edit_note)
- edit_button.place(relx=0.90, rely=0.54, height=30, width=80)
- save_button = Button(self.tab_display, text="Save", command=self.save_note)
- save_button.place(relx=0.90, rely=0.62, height=30, width=80)
- # Video Play Buttons for VLC and PotPlayer
- play_vlc_button = Button(self.tab_display, text="VLCPlayer", command=lambda: self.play_video("vlc"))
- play_vlc_button.place(relx=0.90, rely=0.70, height=30, width=80)
- play_cmd_button = Button(self.tab_display, text="RunCMD", command=self.run_cmd)
- play_cmd_button.place(relx=0.90, rely=0.78, height=30, width=80)
- def setup_create_tab(self):
- Label(self.tab_create, text="For creating a new notepads database").place(relx=0.09, rely=0.14)
- create_button = Button(self.tab_create, text="Create", command=self.create_db)
- create_button.place(relx=0.22, rely=0.25, height=30, width=100)
- browse_button = Button(self.tab_create, text="Browse Database", command=self.browse_db)
- browse_button.place(relx=0.22, rely=0.35, height=30, width=150)
- # Database Functions
- def connect_db(self, db_path):
- try:
- self.connection = sqlite3.connect(db_path)
- self.cursor = self.connection.cursor()
- self.error_output.configure(text="Connected to database.")
- except Exception as e:
- self.error_output.configure(text="Failed to connect: " + str(e))
- def create_db(self):
- if not self.cursor:
- self.error_output.configure(text="Please select a database.")
- return
- try:
- self.cursor.execute("""
- CREATE TABLE IF NOT EXISTS notes (
- id INTEGER PRIMARY KEY,
- title TEXT,
- note TEXT
- );""")
- self.connection.commit()
- self.error_output.configure(text="Database created successfully!")
- except Exception as e:
- self.error_output.configure(text=str(e))
- def browse_db(self):
- db_path = filedialog.askopenfilename(filetypes=[("SQLite DB", "*.db")])
- if db_path:
- self.connect_db(db_path)
- def add_note(self):
- if not self.cursor:
- self.error_output.configure(text="Please select a database.")
- return
- title = self.input_title.get()
- note = self.input_notice.get("1.0", END).strip()
- if title and note:
- try:
- self.cursor.execute("INSERT INTO notes (title, note) VALUES (?, ?)", (title, note))
- self.connection.commit()
- self.error_output.configure(text="Note added successfully!")
- except Exception as e:
- self.error_output.configure(text="Failed to add note: " + str(e))
- else:
- self.error_output.configure(text="Please fill in both fields.")
- def search_notes(self):
- if not self.cursor:
- self.error_output.configure(text="Please select a database.")
- return
- search_title = self.input_search_title.get()
- if search_title:
- try:
- self.cursor.execute("SELECT * FROM notes WHERE title LIKE ?", ('%' + search_title + '%',))
- self.results = self.cursor.fetchall()
- self.error_output.configure(text=f"{len(self.results)} result(s) found")
- self.index = 0
- if self.results:
- self.show_note()
- except Exception as e:
- self.error_output.configure(text="Search failed: " + str(e))
- def show_note(self):
- """Display the current note and set the current note's ID."""
- if self.results:
- self.output_notice.config(state=NORMAL)
- self.output_notice.delete(1.0, END)
- self.output_notice.insert(END, self.results[self.index][2])
- self.current_note_id = self.results[self.index][0] # Track the ID of the current note
- self.output_notice.config(state=DISABLED) # Make output read-only again
- def next_note(self):
- """Move to the next note in the search results."""
- if self.results and self.index < len(self.results) - 1:
- self.index += 1
- self.show_note()
- def previous_note(self):
- """Move to the previous note in the search results."""
- if self.results and self.index > 0:
- self.index -= 1
- self.show_note()
- def delete_note(self):
- """Delete the current note from the database."""
- if self.cursor and self.current_note_id:
- try:
- self.cursor.execute("DELETE FROM notes WHERE id=?", (self.current_note_id,))
- self.connection.commit()
- self.error_output.configure(text="Note deleted.")
- self.results.pop(self.index)
- self.index = min(self.index, len(self.results) - 1) # Ensure index is valid
- self.show_note()
- except Exception as e:
- self.error_output.configure(text="Failed to delete: " + str(e))
- def clear_note(self):
- self.input_title.delete(0, END)
- self.input_notice.delete(1.0, END)
- def run_selected_text(self):
- """Run the selected text from the output_notice field as a command."""
- selected_text = self.output_notice.get(SEL_FIRST, SEL_LAST)
- if selected_text:
- try:
- subprocess.run(selected_text, shell=True)
- except Exception as e:
- messagebox.showerror("Error", str(e))
- def highlight_text_in_note(self):
- """Highlight all occurrences of a keyword in the note text."""
- keyword = self.search_in_note.get()
- self.output_notice.tag_remove("highlight", 1.0, END)
- if keyword:
- start_pos = "1.0"
- while True:
- start_pos = self.output_notice.search(keyword, start_pos, stopindex=END)
- if not start_pos:
- break
- end_pos = f"{start_pos}+{len(keyword)}c"
- self.output_notice.tag_add("highlight", start_pos, end_pos)
- self.output_notice.tag_config("highlight", background="yellow", foreground="black")
- start_pos = end_pos
- def edit_note(self):
- """Allow editing of the current note."""
- self.output_notice.config(state=NORMAL)
- def save_note(self):
- """Save the edited note back to the database."""
- if self.cursor and self.current_note_id:
- updated_note = self.output_notice.get(1.0, END).strip()
- try:
- self.cursor.execute("UPDATE notes SET note=? WHERE id=?", (updated_note, self.current_note_id))
- self.connection.commit()
- self.error_output.configure(text="Note updated.")
- except Exception as e:
- self.error_output.configure(text="Failed to save: " + str(e))
- def play_video(self, player):
- selected_url = self.output_notice.get(SEL_FIRST, SEL_LAST).strip()
- if selected_url:
- if player == "vlc":
- subprocess.run([r"C:\Program Files\VideoLAN\VLC\vlc.exe", selected_url])
- elif player == "potplayer":
- subprocess.run([r"C:\Program Files\DAUM\PotPlayer\PotPlayerMini.exe", selected_url])
- def run_cmd(self):
- """Run a command in CMD based on the selected text."""
- selected_text = self.output_notice.get(SEL_FIRST, SEL_LAST)
- if selected_text:
- subprocess.run(selected_text, shell=True)
- def exit_app(self):
- """Gracefully close the application."""
- if self.connection:
- self.connection.close()
- self.root.quit()
- if __name__ == "__main__":
- root = Tk()
- app = NotepadsManagementApp(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement