This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.py
executable file
·801 lines (638 loc) · 29.1 KB
/
notes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A Tkinter application that allows users to create and store notes in a database.
"""
from __future__ import annotations
import tkinter as tk
from tkinter.colorchooser import askcolor
from tkinter import ttk
import sqlite3
import hashlib
import uuid
import datetime
import json
from typing import Literal
from PIL import Image, ImageTk
class Setting:
"""A class for managing the settings of the application."""
def __init__(self, label: tk.Label) -> None:
self.label = label
def change_color(self) -> None:
"""Change the color of the main screen window."""
color = askcolor()[1]
self.label.configure(background=color)
self.save_color(color)
def save_color(self, color: str) -> None:
"""Save the updated color to a JSON file."""
try:
with open("settings.json", "r", encoding="utf-8") as file:
settings = json.load(file)
except FileNotFoundError:
settings = {"background_color": "#4BA400"}
settings["background_color"] = color
with open("settings.json", "w", encoding="utf-8") as file:
json.dump(settings, file, indent=4)
class Window:
"""A class for managing windows that are used throughout the program."""
# Store the instance of the class once instantiated.
__instance: Window | None = None
@classmethod
def get_instance(cls: Window) -> Window:
""" Class method that returns the singleton instance of Window class.
:param cls: a reference to the class.
:return: None
"""
if not cls.__instance:
Window()
return cls.__instance
def __init__(self) -> None:
"""Creates a new instance of Window class and assigns it to Window.__instance
and creates attributes for register_screen, login_screen, and notes_screen.
:return: None
"""
if Window.__instance is None:
Window.__instance = self
self.register_screen: tk.Toplevel | None = None
self.login_screen: tk.Toplevel | None = None
self.notes_screen: tk.Toplevel | None = None
else:
raise ValueError("ERROR: This class is a singleton!")
class PromptUserInputs:
"""Provide response to the user"""
def __init__(self) -> None:
# A instance of the window management class.
self.window: Window = Window.get_instance()
@staticmethod
def create_label(text: str, color: str, window, pack: bool = False) -> None:
"""Helper method to create a label that get's destroyed after a default of 1.5 second.
:param str text: The text for the label.
:param str color: The color of the label in hex.
:param Entry window: The window the label will be created in.
:param bool pack: Optional, determines if the label should be packed or gridded (defaults to False).
:return: None
"""
label: tk.Label = tk.Label(
window, text=text, fg=color, font=("Arial", 12), width=25)
label.after(1500, label.destroy)
label: tk.Label = label.pack() if pack else label.grid(row=8, column=1)
def prompt_user(self, option: str) -> None:
"""Prompt the user with prompts based on actions
:param str option: The option to prompt the user with, should be one of the following:
- 'fail-login'
- 'user-taken'
- 'registered'
- 'notes-saved'
- 'bad-password'
:return: None
"""
if option == 'fail-login':
self.create_label("Please check login details.",
"#E81500", self.window.login_screen)
elif option == 'user-taken':
self.create_label("Username taken.", "#E81500",
self.window.register_screen)
elif option == 'registered':
self.create_label("Successfully registered.",
"#36BB00", self.window.register_screen)
elif option == 'notes-saved':
self.create_label("Text File Saved.", "#36BB00",
self.window.notes_screen, pack=True)
elif option == 'bad-password':
# Create a new top-level window.
pass_req = tk.Toplevel()
pass_req.title("Password Requirement")
pass_req.geometry("300x150")
pass_req.resizable(False, False)
# Create a list of requirements for a strong password.
requirements = [
"Password is at least 8 characters long.",
"Username is not in the password",
"Password contains at least one number.",
"Password contains at least one letter.",
"Password contains at least one symbol.",
"Password is not a common word."
]
# Create a listbox widget to display the requirements.
len_max = 0
listbox = tk.Listbox(pass_req)
for i in range(1, len(requirements)):
current_requirement = requirements[i]
if len(current_requirement) > len_max:
len_max = len(current_requirement)
listbox.insert(i, f"{i}. {current_requirement}")
# Configure the list-box's width and background.
listbox.configure(width=len_max, background="#333333")
listbox.pack()
# Destroy the window after 7.5 seconds.
pass_req.after(7500, pass_req.destroy)
# Create an extra label on the window to make user aware password does not meet requirements.
self.create_label("Password does not meet \n minimum requirement.",
"#E81500", self.window.register_screen)
class Interface(tk.Frame):
""" A class for managing the graphical user interface."""
def __init__(self, parent, *args, **kwargs) -> None:
"""Initialize the interface components of the application.
:return: None
"""
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
# Variables related to the login form.
self.entry_username: tk.Entry | None = None
self.entry_password: tk.Entry | None = None
self.password_viewer: tk.Button | None = None
# A instance of the window management class.
self.window: Window = Window.get_instance()
# A instance of the settings class.
self.settings: Setting | None = None
# A reference to the password viewer image.
self.photo_image: ImageTk.PhotoImage = self.update_password_image()
# Setup the main window.
self.main_screen()
def main_screen(self) -> None:
"""Creates the interface for the main screen
:return: None
"""
self.parent.title("Secret Notes")
self.parent.geometry("350x250")
# Remove ability to resize the window.
self.parent.resizable(False, False)
# Default color of the UI.
try:
with open("settings.json", "r", encoding="utf-8") as file:
settings = json.load(file)
ui_color = settings.get("background_color", "#4BA400")
except FileNotFoundError:
ui_color = "#4BA400"
# Title.
title = tk.Label(text="Secret Notes", bg=ui_color, fg="#FFFFFF", width="300",
height="2", font=("Arial", 14))
title.pack(side='top')
# Create a settings button.
self.settings = Setting(title)
image_name = "settings"
img = Image.open(f'assets/images/{image_name}.png').resize((20, 20))
image = ImageTk.PhotoImage(img)
setting_button = tk.Button(root, image=image, bg=ui_color,
command=self.settings.change_color)
setting_button.image = image
setting_button.pack(side='top', anchor='ne')
# Login button.
tk.Button(text="Login", height="1", width="25", font=("Arial", 12),
command=self.login_page).pack()
tk.Label(text="").pack()
# Register button.
tk.Button(text="Register", height="1", width="25", font=("Arial", 12),
command=self.register_page).pack()
def register_page(self) -> None:
"""Creates the interface for the registration screen
:return: None
"""
self.window.register_screen = tk.Toplevel()
self.close_window(self.window.login_screen)
self.create_page(self.window.register_screen, login_screen=False)
self.window.register_screen.resizable(False, False)
def login_page(self) -> None:
"""Creates the interface for the login screen
:return: None
"""
self.window.login_screen = tk.Toplevel()
self.close_window(self.window.register_screen)
self.create_page(self.window.login_screen)
self.window.login_screen.resizable(False, False)
@staticmethod
def close_window(window) -> None:
""" Close a specific window.
:param Toplevel window: tkinter toplevel window object
:return: None
"""
if window is None:
return
window.destroy()
def create_page(self, window, login_screen: bool = True) -> None:
"""Creates a user interface for entry based on the screen type
:param Toplevel window: tkinter toplevel window object
:param bool login_screen: determine whether it is a login screen or not (Default value = True)
:return: None
"""
# Check if the current window is a login or registration screen.
if login_screen:
entry_point = "Login"
else:
entry_point = "Registration"
# Creates the window for the login screen.
window.title(entry_point)
window.geometry("350x250")
# Configure the width of the columns.
window.columnconfigure(0, weight=2)
window.columnconfigure(3, weight=1)
# The header for the current window.
header = tk.Label(
window, text=f"Enter your {entry_point.lower()} details:")
header.grid(row=0, column=1)
tk.Label(window, text="").grid(row=1, column=1)
# Instantiate StringVar objects to handle the username and password of the entries.
username: tk.StringVar = tk.StringVar()
password: tk.StringVar = tk.StringVar()
# Create a username entry for the user to interact.
tk.Label(window, text="Username").grid(row=2, column=1)
self.entry_username = tk.Entry(window, textvariable=username)
self.entry_username.grid(row=3, column=1, sticky="news")
# Create a password entry for the user to interact.
tk.Label(window, text="Password").grid(row=4, column=1)
bullet = "\u2022" # Specifies bullet character.
self.entry_password = tk.Entry(
window, show=bullet, textvariable=password)
self.entry_password.grid(row=5, column=1, sticky="news")
# Create a password viewer button to show and hide the password on the password entry.
self.password_viewer = tk.Button(window, bg="#333333", image=self.photo_image,
command=lambda: self.password_show(self.entry_password))
self.password_viewer.grid(row=5, column=2, sticky="we")
# Submit button to send the credentials the account management class.
tk.Label(window, text="").grid(row=6, column=1)
submit_button: tk.Button = tk.Button(window, text="Submit", height="1", width="10",
command=lambda: self.send_credentials(username.get(), password.get(), login_screen))
# Allow the user to press enter on the password entry to submit.
self.entry_password.bind("<Return>", lambda _:
self.send_credentials(username.get(), password.get(), login_screen))
submit_button.grid(row=7, column=1)
def send_credentials(self, username: str, password: str, login_flag: bool) -> None:
"""Sends user information to the account management system
:param str password: the user submitted password.
:param str username: the user submitted username.
:param bool login_flag: whether the window is the login or registration page.
:return: None
"""
# Send the credentials.
AccountManagement(self.parent).receive_credentials(
username, password, login_flag)
self.clear_user_forum(self.entry_username, self.entry_password)
def clear_user_forum(self, entry_user: tk.Entry | None, entry_pass: tk.Entry | None) -> None:
"""Delete the username and password on the entry box once submitted.
:param Entry entry_user: the entry box for the username.
:param Entry entry_pass: the entry box for the password.
:return: None
"""
if entry_user:
entry_user.delete(0, tk.END)
if entry_pass:
entry_pass.delete(0, tk.END)
def password_show(self, entry_password: tk.Entry | None) -> None:
"""Show to users password
:param Entry entry_password: an entry box specifically for a password that has
been configured with the bullet character (\u2022).
:return: None
"""
if entry_password is None:
return
bullet: str = "\u2022"
if entry_password.cget('show') == bullet:
entry_password.config(show='')
view = "hide"
else:
entry_password.config(show=bullet)
view = "view"
# Update the image of the password viewer icon.
self.photo_image = self.update_password_image(view)
self.password_viewer.configure(image=self.photo_image)
@staticmethod
def update_password_image(option: str = "view") -> ImageTk.PhotoImage:
"""This function loads an image from the assets folder and resizes
it to a specified width and height before returning it as a tkinter PhotoImage.
:param str option: is a string that specifies whether the user wishes to view or hide the password
(Default value = "view")
- 'view'
- 'hide'
:return: PhotoImage: the new image.
"""
image_name: Literal['invisible',
'view'] = "invisible" if option == "hide" else "view"
img = Image.open(f'assets/images/{image_name}.png').resize((20, 20))
return ImageTk.PhotoImage(img)
class AccountManagement(tk.Frame):
""" A class for managing user account data stored in a database. """
def __init__(self, parent) -> None:
"""Initialize the account management system of the application.
:param parent: the parent window.
:return: None
"""
super().__init__()
# Variables related to the users login detail.
self.username: str = ""
self.password: str = ""
self.parent = parent
# The creation of the account database.
self.connect_to_database: sqlite3.Connection = sqlite3.connect(
"user-data.db")
self.cursor: sqlite3.Cursor = self.connect_to_database.cursor()
self.create_database() # Construct the database.
# Provide feedback to the user when certain actions are done.
self.interface = PromptUserInputs()
# Close the database on close of the application.
parent.protocol("WM_DELETE_WINDOW", self.close_connection)
def create_database(self) -> None:
"""Creates a SQL database to store the user data.
:return: None
"""
# Create the table if it is an empty database.
self.cursor.execute(
'CREATE TABLE IF NOT EXISTS accounts (username TEXT, password TEXT, uuid TEXT)')
self.cursor.execute(
'CREATE TABLE IF NOT EXISTS notes (uuid TEXT, content TEXT, date DATETIME)')
def receive_credentials(self, username: str, password: str, login_flag: bool) -> None:
"""Retrieve the user data from the text boxes.
:param str password: the user submitted password.
:param str username: the user submitted username.
:param bool login_flag: whether the user is submitting the password through the login
or registration page.
:return: None
"""
self.username: str = username.lower()
self.password: str = password
if login_flag:
self.login_verify(self.username, self.password)
else:
self.register_user(self.username, self.password)
def user_exist(self, username: str) -> bool:
"""Check is the username is already taken.
:param str username: the user submitted username.
:return: bool: a boolean indicating if the user already exists.
"""
search_query = "SELECT * FROM accounts WHERE username = ?"
self.cursor.execute(search_query, (username.lower(),))
return True if self.cursor.fetchone() is not None else False
def check_password(self, password: str) -> bool:
"""Check if the users password has a minimum of 8 characters,
combination of letters, numbers and symbols, no personal information,
amd common phrases.
:param str password: the password to be evaluated.
:return: bool: a boolean indicating if the password meets the
minimum requirements.
"""
if password is None:
raise ValueError('Password cannot be None.')
# Read in the common_words file:
with open('common_words.txt', 'rb') as common_words_file:
common_words = common_words_file.read().splitlines()
# Check if the password is at least 8 characters long.
if len(password) < 8:
return False
# Check if the username is in the password.
elif self.username in password:
return False
# Check if the password contains at least one number.
elif not any(char.isdigit() for char in password):
return False
# Check if the password contains at least one letter.
elif not any(char.isalpha() for char in password):
return False
# Check if the password contains at least one symbol.
elif not any(char in '!@#$%^&*()_+-' for char in password):
return False
# Check if the password is a common word.
elif password in common_words:
return False
# If all checks pass, return True
else:
return True
def register_user(self, username: str, password: str) -> None:
"""Check if the user meets the requirements.
:param str username: the user submitted username.
:param str password: the user submitted password.
:return: None
"""
# Check if the username does not exist
if not self.user_exist(username):
# Check if the password meets the criteria.
if self.check_password(password):
# Add the user to the database
self.add_to_database()
else:
# Respond with a prompt on meeting the criteria.
self.interface.prompt_user("bad-password")
else:
# Respond with a prompt on already being a user.
self.interface.prompt_user('user-taken')
def add_to_database(self) -> None:
"""Register the new user into the database.
:return: None
"""
hashed_password = self.hash_password(self.password)
insert_query = "INSERT INTO accounts (username, password, uuid) VALUES (?, ?, ?)"
self.cursor.execute(insert_query, (self.username,
hashed_password, str(uuid.uuid4())))
self.connect_to_database.commit()
self.interface.prompt_user('registered')
def login_verify(self, username: str, password: str) -> None:
"""Verify the user data to proceed with login.
:param str username: the user submitted username.
:param str password: the user submitted password.
:return: None
"""
# Compare the submitted data to the database.
hashed_password = self.hash_password(password)
select_query = "SELECT * FROM accounts WHERE username = ? AND password = ?"
self.cursor.execute(select_query, (username, hashed_password))
result = self.cursor.fetchone()
# Proceed to the dashboard if the login was successful.
self.dashboard() if result else self.interface.prompt_user('fail-login')
@staticmethod
def hash_password(password: str) -> str:
"""Hash the users password.
:param str password: the user submitted password.
:return: str: the hashed form of the password.
"""
if password is None:
return
# Use the SHA-256 algorithm to generate a cryptographic hash
hash_object = hashlib.sha256(password.encode())
# Return the hexadecimal representation of the hash
return hash_object.hexdigest()
def close_connection(self):
"""Close the application and the database."""
self.connect_to_database.close()
self.parent.destroy()
def access_uuid(self, username: str) -> str:
"""Access the users UUID.
:param str username: the user submitted username.
:return: str: the user's UUID.
"""
if username is None:
raise ValueError("Username cannot be None.")
select_query = "SELECT uuid FROM accounts WHERE username = ?"
self.cursor.execute(select_query, (username,))
uuid_code: tuple = self.cursor.fetchone()
return uuid_code[0]
def dashboard(self) -> None:
"""Users dashboard.
:return: None
"""
note_management = NoteManagement(self.parent)
dashboard_screen = tk.Toplevel()
dashboard_screen.title("Dashboard")
dashboard_screen.geometry("350x250")
# Send the UUID of the logged-in user to the note management.
note_management.retrieve_uuid_code(self.access_uuid(self.username))
# Display different options to the user on how to manage their notes.
tk.Label(dashboard_screen,
text=f"Greetings, {self.username.capitalize()}!").pack()
tk.Button(dashboard_screen, text="Create secret note",
command=note_management.create_secret_notes).pack()
tk.Button(dashboard_screen, text="View secret note",
command=note_management.view_notes).pack()
tk.Button(dashboard_screen, text="Delete secret note",
command=note_management.view_delete_notes).pack()
class NoteManagement(tk.Frame):
""" A class for managing notes stored in a database. """
def __init__(self, parent) -> None:
"""Initialize the note management components of the application.
:return: None
"""
super().__init__()
# The creation of the account database.
self.parent = parent
self.connect_to_database = sqlite3.connect("user-data.db")
self.cursor = self.connect_to_database.cursor()
self.window = Window.get_instance()
self.uuid_code = None
# Provide feedback to the user when certain actions are done.
self.interface = PromptUserInputs()
def retrieve_uuid_code(self, temp_uuid: str) -> None:
"""Get the user UUID.
:param uuid: the user's UUID.
:return: None
"""
self.uuid_code = temp_uuid
def view_delete_notes(self) -> None:
"""View the set of notes scheduled to be deleted in a tree view.
:return: None
"""
# Create the window.
delete_note_screen = tk.Toplevel()
delete_note_screen.title("Delete")
delete_note_screen.geometry("350x250")
# Change the style of the table.
ttk.Style().configure("Treeview", background="#333333",
foreground="white", fieldbackground="333333")
# Create a treeview with 2 columns
tree = ttk.Treeview(delete_note_screen, columns=(
"note", "date"), show="headings")
# Create the Scrollbar widget
scrollbar = ttk.Scrollbar(
delete_note_screen, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Set the yscrollcommand of the Treeview widget to the Scrollbar set.
tree.configure(yscrollcommand=scrollbar.set)
# Set the column width
tree.column("date", width=50)
# Set column headings
tree.heading("note", text="Note")
tree.heading("date", text="Date")
# Create a query to select all notes from the database
select_query = "SELECT content, date FROM notes"
# Execute the query and select the results
self.cursor.execute(select_query)
rows = self.cursor.fetchall()
# Insert the results into the treeview
for row in rows:
tree.insert('', tk.END, values=row)
# Set the treeview
tree.pack(expand=tk.YES, fill=tk.BOTH)
# Button to delete the selected content.
delete_button = tk.Button(delete_note_screen, text="Delete",
command=lambda: self.delete_note(tree))
delete_button.pack(side=tk.BOTTOM)
def delete_note(self, current_tree: ttk.Treeview) -> None:
"""Delete a note from a database based on a treeview selection.
:param current_tree: A reference to the treeview widget.
:return: None
"""
# Check if a row is selected.
if not current_tree.selection():
return
# Get the selected item
selected_item = current_tree.selection()[0]
# Get the item's value
item_value = current_tree.item(selected_item)['values']
# Get the note content
note = item_value[0]
# Execute to delete query based on the note content
delete_query = "DELETE FROM notes WHERE content = ?"
self.cursor.execute(delete_query, (note,))
self.connect_to_database.commit()
# Refresh the treeview
current_tree.delete(selected_item)
def view_notes(self) -> None:
"""View all the notes.
:return: None
"""
read_file_screen = tk.Toplevel()
read_file_screen.title("Notes")
read_file_screen.geometry("350x250")
select_query = "SELECT content, date FROM notes WHERE uuid = ?"
self.cursor.execute(select_query, (self.uuid_code,))
results = self.cursor.fetchall()
if results:
count = 1
for row in results:
message = f"{row[0]}, {row[1]} \n"
tk.Label(read_file_screen, text=message,
font=("arial", 15)).pack()
count += 1
else:
tk.Label(read_file_screen, text="No data.",
font=("arial", 15)).pack()
def save_text(self, notes: str) -> None:
"""Save the newly created notes.
:param str notes:
:return: None
"""
insert_query = "INSERT INTO notes (uuid, content, date) VALUES (?, ?, ?)"
date_time = datetime.datetime.now()
self.cursor.execute(insert_query, (self.uuid_code,
notes, date_time.strftime("%x")))
self.connect_to_database.commit()
# Confirm to the user that the data was saved successfully.
self.interface.prompt_user('notes-saved')
def create_secret_notes(self) -> None:
"""Creates a new note.
:return: None
"""
# Create the window for the creation of notes.
self.window.notes_screen = tk.Toplevel()
self.window.notes_screen.title("Make Notes")
self.window.notes_screen.geometry("350x250")
# Create the text box for the user to write.
tk.Label(self.window.notes_screen, text="Enter secret notes: ").pack()
text_box = tk.Text(self.window.notes_screen, width=30, height=10)
text_box.insert(tk.END, "Enter your notes here!")
text_box.config(state=tk.NORMAL)
text_box.pack(expand=False)
# Clear the text box once clicked.
text_box.bind("<Button-1>", lambda _: self.clear_text(text_box))
text_box.focus_set()
# Save the text.
tk.Button(self.window.notes_screen, text="Save", command=lambda: self.save_text(
text_box.get("1.0", "end-1c"))).pack()
@staticmethod
def clear_text(entry: tk.Text) -> None:
""" Clears the text off an entry.
:param Text entry: the text in a text box to be deleted.
:return: None
"""
if entry is None:
return
entry.delete('1.0', tk.END)
class MainApplication(tk.Frame):
""" Initialize all the components to the secret note app. """
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.interface = Interface(parent)
self.account_management = AccountManagement(parent)
self.note_management = NoteManagement(parent)
if __name__ == '__main__':
root = tk.Tk()
img = ImageTk.PhotoImage(file="assets/images/notes.png")
root.iconphoto(False, img)
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()