-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
144 lines (113 loc) · 4.89 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
from tkinter import *
from tkinter import messagebox
import random
import pyperclip
import json
# ---------------------------- FIND PASSWORD ------------------------------- #
# FUNCTION FOR SEARCH BUTTON AND IMPLEMENTING EXCEPTIONS BECAUSE WHEN WE FIRST RUN PROGRAM JSON FILES NOT EXIST!!!
def find_password():
website = web_input.get()
try:
with open("data.json") as data_file:
data = json.load(data_file)
except FileNotFoundError:
messagebox.showinfo(title="Error", message="No data found")
else:
if website in data:
email = data[website]["email"]
password = data[website]["password"]
messagebox.showinfo(title=website, message=f"Email: {email}\nPassword: {password}")
else:
messagebox.showinfo(title="Error", message=f"No details for {website}.")
# ---------------------------- PASSWORD GENERATOR ------------------------------- #
# FUNCTION FOR THE GENERATE PASSWORD BUTTON
def generate_password():
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v',
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
nr_letters = random.randint(8, 10)
nr_symbols = random.randint(2, 4)
nr_numbers = random.randint(2, 4)
password_letters = [random.choice(letters) for _ in range(nr_letters)]
password_symbols = [random.choice(symbols) for _ in range(nr_symbols)]
password_numbers = [random.choice(numbers) for _ in range(nr_numbers)]
generated_password_list = password_letters + password_symbols + password_numbers
random.shuffle(generated_password_list)
generated_password = ''.join(generated_password_list)
# INSERT FUNCTION WRITES IN THE PASSWORD ENTRY AS SOON AS IS GENERATED
password_input.insert(0, generated_password)
# WE INSTALLED PYPERCLIP AND WITH THIS COMMAND THE GENERATED PASSWORD COPIED TO CLIPBOARD AND READY TO USE
pyperclip.copy(generated_password)
# ---------------------------- SAVE PASSWORD ------------------------------- #
# CREATING A FUNCTION FOR THE ADD BUTTON WHERE WE GET INPUTS FROM THE ENTRIES
# WE WRITE INTO THE FILE
# THEN AFTER ADD BUTTON PRESSED DELETES EVERYTHING AND START THE CURSOR FROM THE FIRST ENTRY
def save_data():
website = web_input.get()
email = email_input.get()
password = password_input.get()
new_data = {
website:
{"email": email,
"password": password,
}
}
# IF USER LEAVES BLANK THE ENTRIES THEN WE POP UP A MESSAGE WINDOW
if len(password) == 0 or len(website) == 0:
messagebox.showinfo(title="Oops", message="Please no fields empty")
else:
try:
with open("data.json", mode="r") as file:
# READING OLD DATA
data = json.load(file)
except FileNotFoundError:
with open("data.json", mode="w") as file:
json.dump(new_data, file, indent=4)
else:
# UPDATING OLD DATA WITH NEW DATA
data.update(new_data)
with open("data.json", mode="w") as file:
# SAVING UPDATED DATA
json.dump(data, file, indent=4)
finally:
web_input.delete(0, 'end')
password_input.delete(0, 'end')
web_input.focus()
# ---------------------------- UI SETUP ------------------------------- #
# CREATING THE WINDOW
window = Tk()
window.title("Password Manager")
window.config(padx=50, pady=50)
# CREATING THE CANVAS AND IMPORTING THE IMAGE
canvas = Canvas(width=200, height=200)
password_img = PhotoImage(file="logo.png")
canvas.create_image(100, 100, image=password_img)
canvas.grid(column=1, row=0)
# CREATING THE LABELS
web_label = Label(text="Website: ")
web_label.grid(column=0, row=1)
email_label = Label(text="Email/Username: ")
email_label.grid(column=0, row=2)
password_label = Label(text="Password: ")
password_label.grid(column=0, row=3)
# CREATING INPUT PROMPTS
web_input = Entry(width=21)
web_input.grid(column=1, row=1, sticky="EW")
web_input.focus()
email_input = Entry(width=35)
email_input.grid(column=1, row=2, columnspan=2, sticky="EW")
email_input.insert(0, "[email protected]")
password_input = Entry(width=21)
password_input.grid(column=1, row=3, sticky="EW")
# CREATING BUTTONS
generate_button = Button(text="Generate Password", command=generate_password)
generate_button.grid(column=2, row=3, sticky="EW")
add_button = Button(text="Add", width=36, command=save_data)
add_button.grid(column=1, row=4, columnspan=2, sticky="EW")
search_button = Button(text="Search", command=find_password)
search_button.grid(column=2, row=1, sticky="EW")
window.mainloop()