Untitled

 avatar
unknown
python
2 years ago
5.4 kB
4
Indexable
from tkinter import *
import os
import json
from tkinter import ttk, messagebox
from ttkbootstrap import Style
def open_notes(my_notes_btn):
    
    
    root=Toplevel(screen)
    root.title("MyNotes")
    root.geometry("500x500")
    '''iconpic = PhotoImage(file='notepad_icon.png')
    root.iconphoto(False, iconpic)'''#Ignore this
    style = Style(theme='journal')
    style = ttk.Style()

    # Configure the tab font to be bold
    style.configure("TNotebook.Tab", font=("TkDefaultFont", 14, "bold"))

    # Create the notebook to hold the notes
    notebook = ttk.Notebook(root, style="TNotebook")

    # Load saved notes
    notes = {}
    try:
        with open("notes.json", "r") as f:
            notes = json.load(f)
    except FileNotFoundError:
        pass
    
    # Create the notebook to hold the notes
    notebook = ttk.Notebook(root)
    notebook.pack(padx=10, pady=10, fill=BOTH, expand=True)
    
    def on_close(my_notes_btn):
        
        my_notes_btn.config(text='MyNotes', font=('Arial', 16), bd=0, fg='#0097e8', width=10, bg='white')
        root.destroy()
    
    root.protocol("WM_DELETE_WINDOW", lambda : on_close(my_notes_btn))
 

    # Create a function to add a new note
    def add_note():
        # Create a new tab for the note
        note_frame = ttk.Frame(notebook, padding=10)
        notebook.add(note_frame, text="New Note")

        # Create entry widgets for the title and content of the note
        title_label = ttk.Label(note_frame, text="Title:")
        title_label.grid(row=0, column=0, padx=10, pady=10, sticky="W")

        title_entry = ttk.Entry(note_frame, width=40)
        title_entry.grid(row=0, column=1, padx=10, pady=10)

        content_label = ttk.Label(note_frame, text="Content:")
        content_label.grid(row=1, column=0, padx=10, pady=10, sticky="W")

        content_entry = Text(note_frame, width=40, height=10)
        content_entry.grid(row=1, column=1, padx=10, pady=10)

        # Create a function to save the note
        def save_note():
            # Get the title and content of the note
            title = title_entry.get()
            content = content_entry.get("1.0", END)

            # Add the note to the notes dictionary
            notes[title] = content.strip()

            # Save the notes dictionary to the file
            with open("notes.json", "w") as f:
                json.dump(notes, f)

                # Add the note to the notebook
            note_content = Text(notebook, width=40, height=10)
            note_content.insert(END, content)
            notebook.forget(notebook.select())
            notebook.add(note_content, text=title)

        # Add a save button to the note frame
        save_button = ttk.Button(note_frame, text="Save",
                                 command=save_note, style="secondary.TButton")
        save_button.grid(row=2, column=1, padx=10, pady=10)

    def load_notes():
        try:
            with open("notes.json", "r") as f:
                notes = json.load(f)

            for title, content in notes.items():
                # Add the note to the notebook
                note_content = Text(notebook, width=40, height=10)
                note_content.insert(END, content)
                notebook.add(note_content, text=title)

        except FileNotFoundError:
            # If the file does not exist, do nothing
            pass

    # Call the load_notes function when the app starts
    load_notes()

    # Create a function to delete a note
    def delete_note():
        # Get the current tab index
        current_tab = notebook.index(notebook.select())

        # Get the title of the note to be deleted
        note_title = notebook.tab(current_tab, "text")

        # Show a confirmation dialog
        confirm = messagebox.askyesno("Delete Note",
                                      f"Are you sure you want to delete {note_title}?")

        if confirm:
            # Remove the note from the notebook
            notebook.forget(current_tab)

            # Remove the note from the notes dictionary
            notes.pop(note_title)

            # Save the notes dictionary to the file
            with open("notes.json", "w") as f:
                json.dump(notes, f)

    # Add buttons to the main window
    new_button = ttk.Button(root, text="New Note",
                            command=add_note, style="info.TButton")
    new_button.pack(side=LEFT, padx=10, pady=10)

    delete_button = ttk.Button(root, text="Delete",
                               command=delete_note, style="primary.TButton")
    delete_button.pack(side=LEFT, padx=10, pady=10)

    root.mainloop()
def mainwindow():
    global screen
    screen = Tk()
    screen.title("STUDY BUDDY-MY STUDY PLANNER")
    screen.geometry("720x480")
    screen.configure(bg='#7AC5CD')
    screen.resizable(False, False)
    options_fm = Frame(screen, bg='white')
    my_notes_btn = Button(options_fm, text='MyNotes', font=('Arial', 16), bd=0, fg='#0097e8', width=10, command=lambda: open_notes(my_notes_btn))
    my_notes_btn.place(x=2, y=0)
    options_fm.pack(pady=1)
    options_fm.pack_propagate(False)
    options_fm.configure(width=720, height=35)
    main_fm = Frame(screen)
    main_fm.pack(fill=BOTH, expand=True)
    screen.mainloop()
mainwindow()
Editor is loading...
Leave a Comment