-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
155 lines (135 loc) · 5.79 KB
/
main.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
# Libraries
from tkinter import *
from tkinter import messagebox
from password import Password
import json
# Colors
WHITE = "#F9F9F9"
RED = "#c0392b"
# Font Family
FONT = ("Raleway", 9, "bold")
# Clear all fields
def clear_all():
website_input.delete(0, END)
email_username_input.delete(0, END)
email_username_input.insert(0, "[email protected]")
password_input.delete(0, END)
# Search password
def search_password():
search_data = website_input.get()
try:
with open("data.json") as data_file:
data = json.load(data_file)
except (FileNotFoundError, json.decoder.JSONDecodeError):
messagebox.showerror(title="No data found",
message="There is no data in the database.")
website_input.delete(0, END)
else:
if search_data in data:
email = data[search_data]["email"]
password = data[search_data]["password"]
email_username_input.delete(0, END)
password_input.delete(0, END)
email_username_input.insert(0, email)
password_input.insert(0, password)
messagebox.showinfo(title=f"{website_input.get()}", message=f"Email: {email}\nPassword: {password}")
search_button.clipboard_clear()
search_button.clipboard_append(password)
else:
messagebox.showerror(title="Website not found",
message="The website you searched can not be found, please try again.")
finally:
website_input.delete(0, END)
password_input.delete(0, END)
# Generate random password
def generate_password():
random_password = Password().create_password()
if password_input.get() == "":
password_input.insert(0, random_password)
pass_gen_button.clipboard_append(random_password)
else:
password_input.delete(0, END)
pass_gen_button.clipboard_clear()
password_input.insert(0, random_password)
pass_gen_button.clipboard_append(random_password)
# Add passwords to data.json
def add_password():
website = website_input.get()
email_username = email_username_input.get()
password = password_input.get()
new_data = {
website: {
"email": email_username,
"password": password,
}
}
if website != "" and email_username != "" and password != "":
correct_info = messagebox.askokcancel(title=website, message=f"These are the details you have entered:\n"
f"Email: {email_username}\nPassword: {password}\n"
f"Is it OK to save?")
if correct_info:
try:
with open("data.json", mode="r") as data_file:
# Reading old data
data = json.load(data_file)
# Updating old data with new data
data.update(new_data)
except (FileNotFoundError, json.decoder.JSONDecodeError):
with open("data.json", mode="w") as data_file:
# Saving the updated data
json.dump(new_data, data_file, indent=4)
else:
with open("data.json", mode="w") as data_file:
# Saving the updated data
json.dump(data, data_file, indent=4)
finally:
website_input.delete(0, END)
email_username_input.delete(0, END)
email_username_input.insert(0, "[email protected]")
password_input.delete(0, END)
else:
messagebox.showerror(title="Insufficient information", message="Please don't leave any fields empty")
# Root object definition
root = Tk()
root.title("Password Manager")
root.config(padx=40, pady=40, bg=WHITE)
root.resizable(width=False, height=False)
# Canvas object definition
canvas = Canvas(width=200, height=200, bg=WHITE, highlightthickness=0)
logo = PhotoImage(file="logo.png")
canvas.create_image(120, 100, image=logo)
canvas.grid(row=0, column=1)
# Widgets
# Website label
website_label = Label(text="Website:", font=FONT, bg=WHITE, fg=RED)
website_input = Entry(width=25, highlightbackground=RED, highlightthickness=1, font=FONT)
website_input.focus()
# Email/Username label
email_username_label = Label(text="Email/Username:", font=FONT, bg=WHITE, fg=RED)
email_username_input = Entry(width=44, highlightbackground=RED, highlightthickness=1, font=FONT)
email_username_input.insert(0, "[email protected]")
# Password label
password_label = Label(text="Password:", font=FONT, bg=WHITE, fg=RED)
password_input = Entry(width=25, highlightbackground=RED, highlightthickness=1, font=FONT)
# Buttons
clear_all_button = Button(text="Clear All", font=("Raleway", 9, "bold"), fg="white", bg=RED,
width=44, command=clear_all)
search_button = Button(text="Search", font=("Raleway", 9, "bold"), fg="white", bg=RED,
width=16, command=search_password)
pass_gen_button = Button(text="Generate Password", font=("Raleway", 9, "bold"), fg="white", bg=RED,
command=generate_password)
add_button = Button(text="Add Password", font=("Raleway", 9, "bold"), bg=RED, fg="white", width=44,
command=add_password)
# Grids
website_label.grid(row=1, column=0)
website_input.grid(row=1, column=1)
email_username_label.grid(row=2, column=0)
email_username_input.grid(row=2, column=1, columnspan=2, sticky="e", pady=2)
password_label.grid(row=3, column=0)
password_input.grid(row=3, column=1)
clear_all_button.grid(row=5, column=0, columnspan=3, sticky="e", pady=2)
search_button.grid(row=1, column=2, pady=2)
pass_gen_button.grid(row=3, column=2, sticky="e", pady=2)
add_button.grid(row=4, column=0, columnspan=3, sticky="e", pady=2)
# Root mainloop
root.mainloop()