|
| 1 | +from tkinter import Tk, Label, Frame, Entry, Button, Listbox, Scrollbar, TclError |
| 2 | +from tkinter.font import Font |
| 3 | +from tkinter import END, TOP, BOTTOM, ANCHOR, LEFT, BOTH, RIGHT, FLAT |
| 4 | +import ctypes |
| 5 | + |
| 6 | +ctypes.windll.shcore.SetProcessDpiAwareness(True) |
| 7 | + |
| 8 | +# Error handler in GUI |
| 9 | + |
| 10 | + |
| 11 | +def err(msg): |
| 12 | + error.config(text=f"Error: {msg}", fg="red", font=('Sans Serif', 18)) |
| 13 | + error.pack(pady=20) |
| 14 | + |
| 15 | +# Tkinter button functions |
| 16 | + |
| 17 | + |
| 18 | +def add_note(): |
| 19 | + if len(notes_entry.get()) == 0: |
| 20 | + err("Please enter a note first.") |
| 21 | + else: |
| 22 | + error.pack_forget() |
| 23 | + start_msg.pack_forget() |
| 24 | + notes_list.insert( |
| 25 | + END, f"{notes_list.size() + 1}. " + notes_entry.get()) |
| 26 | + notes_entry.delete(0, END) |
| 27 | + notes_list.see("end") |
| 28 | + |
| 29 | + |
| 30 | +def delete_note(): |
| 31 | + try: |
| 32 | + selected = notes_list.get(notes_list.curselection()) |
| 33 | + if len(selected) == 0: |
| 34 | + error.config(text="Error: Please select a note first to delete.") |
| 35 | + error.pack() |
| 36 | + else: |
| 37 | + notes_list.delete(ANCHOR) |
| 38 | + if notes_list.size() == 0: |
| 39 | + start_msg.pack(before=notes_list) |
| 40 | + error.pack_forget() |
| 41 | + except TclError: |
| 42 | + err("Select a note to delete") |
| 43 | + |
| 44 | + |
| 45 | +def check_note(): |
| 46 | + try: |
| 47 | + notes_list.itemconfig( |
| 48 | + notes_list.curselection(), |
| 49 | + fg="mediumseagreen", |
| 50 | + ) |
| 51 | + notes_list.selection_clear(0, END) |
| 52 | + error.pack_forget() |
| 53 | + except TclError: |
| 54 | + err("Please select a note first.") |
| 55 | + |
| 56 | + |
| 57 | +def uncheck_note(): |
| 58 | + try: |
| 59 | + notes_list.itemconfig( |
| 60 | + notes_list.curselection(), |
| 61 | + fg="black", |
| 62 | + ) |
| 63 | + notes_list.selection_clear(0, END) |
| 64 | + error.pack_forget() |
| 65 | + except TclError: |
| 66 | + err("Please select checked note first.") |
| 67 | + |
| 68 | + |
| 69 | +# Main window |
| 70 | +root = Tk() |
| 71 | +root.title('Python Sticky notes') |
| 72 | +root.geometry('600x600') |
| 73 | +root.config(bg='#d3d3d3') |
| 74 | +root.iconbitmap("./favicon.ico") |
| 75 | +root.resizable(False, False) |
| 76 | + |
| 77 | +# Fonts |
| 78 | +my_font = Font( |
| 79 | + family="Sans Serif", |
| 80 | + size=25, |
| 81 | +) |
| 82 | + |
| 83 | +# Frame for list items |
| 84 | +notes_frame = Frame(root) |
| 85 | +notes_frame.pack(pady=10) |
| 86 | + |
| 87 | +# Welcome Message |
| 88 | +start_msg = Label(notes_frame, text="Enter a note to get started...", font=( |
| 89 | + "Sans serif", 18), fg="darkgrey") |
| 90 | +start_msg.pack(side=TOP) |
| 91 | + |
| 92 | +# Main ListBox for notes |
| 93 | +notes_list = Listbox(notes_frame, |
| 94 | + font=my_font, |
| 95 | + width=25, |
| 96 | + height=5, |
| 97 | + bd=0, |
| 98 | + bg="SystemButtonFace", |
| 99 | + selectbackground="grey", |
| 100 | + activestyle="none", |
| 101 | + highlightthickness=0 |
| 102 | + ) |
| 103 | +notes_list.pack(side=LEFT, fill=BOTH) |
| 104 | + |
| 105 | +# Scrollbar for notes_frame |
| 106 | +scroll = Scrollbar(notes_frame) |
| 107 | +scroll.pack(side=RIGHT, fill=BOTH) |
| 108 | + |
| 109 | +# Scrollbar configuration |
| 110 | +notes_list.config(yscrollcommand=scroll.set) |
| 111 | +scroll.config(command=notes_list.yview) |
| 112 | + |
| 113 | +# Notes Entry Box |
| 114 | +notes_entry = Entry(root, font=("Sans Serif", 22), relief=FLAT, borderwidth=10) |
| 115 | +notes_entry.pack(pady=20) |
| 116 | + |
| 117 | +# Button Frame |
| 118 | +function_button_frame = Frame(root, bg="#d3d3d3") |
| 119 | +function_button_frame.pack() |
| 120 | + |
| 121 | +# Buttons |
| 122 | +add_button = Button(function_button_frame, text="Add note", command=add_note) |
| 123 | +delete_button = Button(function_button_frame, |
| 124 | + text="Delete note", command=delete_note) |
| 125 | +checked_button = Button(function_button_frame, |
| 126 | + text="Check item", command=check_note) |
| 127 | +unchecked_button = Button(function_button_frame, |
| 128 | + text="Uncheck item", command=uncheck_note) |
| 129 | + |
| 130 | +add_button.grid(row=0, column=0) |
| 131 | +delete_button.grid(row=0, column=1, padx=10) |
| 132 | +checked_button.grid(row=0, column=2) |
| 133 | +unchecked_button.grid(row=0, column=3, padx=10) |
| 134 | + |
| 135 | +# Error Message |
| 136 | +error = Label(root) |
| 137 | + |
| 138 | +# Author Info |
| 139 | +author = Label(root, text="© Swaraj Baral\nGithub: SwarajBaral", |
| 140 | + bg="#d3d3d3", fg="grey") |
| 141 | +author.pack(side=BOTTOM) |
| 142 | + |
| 143 | +if __name__ == '__main__': |
| 144 | + root.mainloop() |
0 commit comments