-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
131 lines (107 loc) · 5.12 KB
/
main.py
File metadata and controls
131 lines (107 loc) · 5.12 KB
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
from tkinter import *
from tkinter import messagebox # special dialogue boxes (pop-ups)
import random
import json
# ---------------------------- PASSWORD GENERATOR ------------------------------- #
# Password Generator
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)]
password_list = password_letters + password_symbols + password_numbers
random.shuffle(password_list)
# for char in password_list:
# password += char
password = "".join(password_list)
password_ip.insert(0, password)
# print(f"Your password is: {password}")
# ---------------------------- SAVE PASSWORD ------------------------------- #
def save():
website_data = website_ip.get() # get() method is used to retrieve the input in entry method
email_data = mail_ip.get()
password_data = password_ip.get()
new_data = {
website_data: {
"email": email_data,
"password": password_data
}
}
flag = True
if len(website_data) == 0 or len(password_data) == 0 or len(email_data) == 0: # in case if any field is left empty
messagebox.showinfo(title="Oops", message="Please dont leave any field empty.") # show message dialogue box
# flag = False
else:
'''is_ok = messagebox.askokcancel(title=website_data,
message=f"Your credentials are:\nEmail: {email_data}\nPassword: {password_data}")'''
# ok & cancel pop up box and return true if 'ok' and false if 'cancel' is pressed
try:
with open("data.json", "r") as data_file:
# json.dump(new_data,data_file,indent=4)
# reading old data
data = json.load(data_file)
except FileNotFoundError:
with open("data.json", "w") as data_file:
json.dump(new_data, data_file, indent=4)
# updating old data with new data
else:
data.update(new_data)
# saving updated data
with open("data.json", "w") as data_file:
json.dump(data, data_file, indent=4)
# data_file.write(f"{website_data} | {email_data} | {password_data}\n")
finally:
website_ip.delete(0, END)
password_ip.delete(0, END)
# _______________________________ Find Password ____________________________________________________________________
def find_password():
website_data = website_ip.get()
website_data = website_data.lower() # make entry case-sensitive
try:
with open("data.json") as file:
file_data = json.load(file)
except FileNotFoundError:
messagebox.showinfo(title="Error", message="No data file found")
else:
if website_data in file_data:
email_op = file_data[website_data]["email"]
password_op = file_data[website_data]["password"]
messagebox.showinfo(title=website_data, message=f"Email:{email_op}\nPassword:{password_op}")
else:
messagebox.showinfo(title="Not Found", message=f"No data found for website:{website_data}")
# ---------------------------- UI SETUP ------------------------------- #
window = Tk()
window.title("Password Manager")
window.config(padx=50, pady=50)
canvas = Canvas(width=200, height=200)
logo = PhotoImage(file="logo.png")
canvas.create_image(100, 100, image=logo)
canvas.grid(column=1, row=0)
website_ip = Entry(width=25)
website_ip.grid(column=1, row=1, columnspan=1)
website_ip.focus() # lets the cursor straight to corresponding entry box
website_label = Label(text="Website: ")
website_label.grid(column=0, row=1) # used to relocate the attribute in the from of rows and columns
mail_ip = Entry(width=35) # entry box
mail_ip.grid(column=1, row=2, columnspan=2) # columnspan is used if your attribute is covering more than one column
mail_label = Label(text="Email/Username: ")
mail_label.grid(column=0, row=2)
mail_ip.insert(0, "jalajsrivastav21@gmail.com") # prefilled details (often used)
password_ip = Entry(width=25)
password_ip.grid(column=1, row=3)
password_label = Label(text="Password: ")
password_label.grid(column=0, row=3)
password_button = Button(text="Generate", command=Generate_password)
password_button.grid(column=2, row=3, columnspan=2)
add_button = Button(text="Add", width=33, command=save)
add_button.grid(column=1, row=4, columnspan=2)
search_button = Button(text="Search", command=find_password)
search_button.grid(column=2, row=1)
window.mainloop()