1
+ # Import Modules
2
+ import tkinter as tk
3
+ from tkinter import ttk , messagebox
4
+ import json
5
+ from ttkbootstrap import Style
6
+
7
+ # Create main window
8
+ root = tk .Tk ()
9
+ root .title ("Note App" )
10
+ root .geometry ("515x515" )
11
+
12
+ # Configure style
13
+ style = Style (theme = "darkly" )
14
+ style = ttk .Style ()
15
+
16
+
17
+ # Configure font for tabs
18
+ style .configure ("TNotebook.Tab" , font = ("TkDefaultFont" , 14 , "bold" ))
19
+
20
+ # Set dark background color
21
+ root .configure (bg = "#333333" )
22
+
23
+ # Create Notebook
24
+ notebook = ttk .Notebook (root , style = "TNotebook" )
25
+
26
+ # Load previously saved notes
27
+ notes = {}
28
+ try :
29
+ with open ("notes.json" , "r" ) as f :
30
+ notes = json .load (f )
31
+ except FileNotFoundError :
32
+ pass
33
+
34
+ # Create and pack notebook widget
35
+ notebook = ttk .Notebook (root )
36
+ notebook .pack (padx = 10 , pady = 10 , fill = tk .BOTH , expand = True )
37
+
38
+ # Function to add new note
39
+ def add_note ():
40
+
41
+ # Create frame for new note
42
+ note_frame = ttk .Frame (notebook , padding = 10 )
43
+ notebook .add (note_frame , text = "New Note" )
44
+
45
+ # Widgets for note title and content
46
+ title_label = ttk .Label (note_frame , text = "Title:" , foreground = "white" , background = "#333333" )
47
+ title_label .grid (row = 0 , column = 0 , padx = 10 , pady = 10 , sticky = "W" )
48
+
49
+ title_entry = ttk .Entry (note_frame , width = 40 , foreground = "white" , background = "#444444" )
50
+ title_entry .grid (row = 0 , column = 1 , padx = 10 , pady = 10 )
51
+
52
+ content_label = ttk .Label (note_frame , text = "Content:" , foreground = "white" , background = "#333333" )
53
+ content_label .grid (row = 1 , column = 0 , padx = 10 , pady = 10 , sticky = "W" )
54
+
55
+ content_entry = tk .Text (note_frame , width = 40 , height = 10 , foreground = "white" , background = "#444444" )
56
+ content_entry .grid (row = 1 , column = 1 , padx = 10 , pady = 10 )
57
+
58
+ # Function to save note
59
+ def save_note ():
60
+
61
+ # Get title and content
62
+ title = title_entry .get ()
63
+ content = content_entry .get ("1.0" , tk .END )
64
+
65
+ # Add note to dictionary
66
+ notes [title ] = content .strip ()
67
+
68
+ # Save notes to file (JSON)
69
+ with open ("notes.json" , "w" ) as f :
70
+ json .dump (notes , f )
71
+
72
+ # Add note to notebook
73
+ note_content = tk .Text (notebook , width = 40 , height = 10 , foreground = "white" , background = "#444444" )
74
+ note_content .insert (tk .END , content )
75
+ notebook .forget (notebook .select ())
76
+ notebook .add (note_content , text = title )
77
+
78
+ # Save button
79
+ save_button = ttk .Button (note_frame , text = "Save" ,
80
+ command = save_note , style = "secondary.TButton" )
81
+ save_button .grid (row = 2 , column = 0 , padx = 10 , pady = 10 )
82
+
83
+ # Function to load notes
84
+ def load_notes ():
85
+ try :
86
+ with open ("notes.json" , "r" ) as f :
87
+ notes = json .load (f )
88
+
89
+ # Add each note to the notebook
90
+ for title , content in notes .items ():
91
+ note_content = tk .Text (notebook , width = 40 , height = 10 )
92
+ note_content .insert (tk .END , content )
93
+ notebook .add (note_content , text = title )
94
+
95
+ except FileNotFoundError :
96
+ # Do nothing if file doesn't exists
97
+ pass
98
+
99
+ # Load notes at start
100
+ load_notes ()
101
+
102
+ # Function to delete note
103
+ def delete_note ():
104
+ # Get current tab index
105
+ current_tab = notebook .index (notebook .select ())
106
+
107
+ # Get note title
108
+ note_title = notebook .tab (current_tab , "text" )
109
+
110
+ # Confirm deletion
111
+ confirm = messagebox .askyesno ("Delete Note" ,
112
+ f"Are you sure you want to delete { note_title } ?" )
113
+
114
+ if confirm :
115
+ # Remove from notebook
116
+ notebook .forget (current_tab )
117
+
118
+ # Remove from notes dict
119
+ notes .pop (note_title )
120
+
121
+ # Save notes to file
122
+ with open ("notes.json" , "w" ) as f :
123
+ json .dump (notes , f )
124
+
125
+ # Buttons
126
+ new_button = ttk .Button (root , text = "New" ,
127
+ command = add_note , style = "dark.TButton" )
128
+ new_button .pack (side = tk .LEFT , padx = 10 , pady = 10 )
129
+
130
+ delete_button = ttk .Button (root , text = "Delete" ,
131
+ command = delete_note , style = "dark.TButton" )
132
+ delete_button .pack (side = tk .LEFT , padx = 10 , pady = 10 )
133
+
134
+ root .mainloop ()
0 commit comments