1
+ import json
1
2
from tkinter import *
2
3
from tkinter import messagebox
3
4
from random import choice , shuffle , randint
@@ -23,20 +24,35 @@ def generate_password():
23
24
24
25
25
26
# ---------------------------- SAVE PASSWORD ------------------------------- #
26
- def add_information ():
27
+ def save ():
27
28
website = website_input .get ()
28
29
username = username_input .get ()
29
30
password = password_input .get ()
30
31
31
32
# Validate Entries
32
33
if validate_entries (website , username , password ):
33
- account_data = f"{ website } | { username } | { password } "
34
- print (account_data )
34
+ new_data = {website : {
35
+ "email" : username ,
36
+ "password" : password
37
+ }}
38
+ print (new_data )
35
39
pyperclip .copy (password )
36
-
37
- # Write information to data file
38
- with open ("data.txt" , "a" ) as file :
39
- file .write (account_data )
40
+ try :
41
+ # Write information to data file
42
+ with open ("data.json" , "r" ) as file :
43
+ # Read/Import json to python dict
44
+ data = json .load (file )
45
+ except FileNotFoundError :
46
+ # Create new data.json file
47
+ with open ("data.json" , "w" ) as file :
48
+ json .dump (new_data , file , indent = 4 )
49
+ else :
50
+ # If file exists, Update dict to add new data
51
+ data .update (new_data )
52
+ with open ("data.json" , "w" ) as file :
53
+ # Add python dict to json
54
+ json .dump (data , file , indent = 4 )
55
+ finally :
40
56
website_input .delete (0 , END )
41
57
password_input .delete (0 , END )
42
58
display_messagebox ("Your info was added successfully!" )
@@ -55,6 +71,23 @@ def validate_entries(website, username, password):
55
71
return True
56
72
57
73
74
+ def find_password ():
75
+ website = website_input .get ()
76
+ try :
77
+ with open ("data.json" , "r" ) as file :
78
+ data = json .load (file )
79
+ except FileNotFoundError :
80
+ display_messagebox ("No data file found." )
81
+ else :
82
+ if website in data :
83
+ email = data [website ]['email' ]
84
+ password = data [website ]['password' ]
85
+ display_messagebox (f"Email: { email } \n Password: { password } " )
86
+ pyperclip .copy (password )
87
+ else :
88
+ display_messagebox (f"No details for the { website } exists." )
89
+
90
+
58
91
# ---------------------------- UI SETUP ------------------------------- #
59
92
window = Tk ()
60
93
window .title ("Password Manager" )
@@ -69,10 +102,13 @@ def validate_entries(website, username, password):
69
102
website_label = Label (text = "Website:" )
70
103
website_label .grid (row = 1 , column = 0 )
71
104
72
- website_input = Entry (width = 38 )
73
- website_input .grid (row = 1 , column = 1 , columnspan = 2 )
105
+ website_input = Entry (width = 21 )
106
+ website_input .grid (row = 1 , column = 1 )
74
107
website_input .focus ()
75
108
109
+ website_search = Button (text = "Search" , width = 13 , command = find_password )
110
+ website_search .grid (row = 1 , column = 2 )
111
+
76
112
# Username Input
77
113
username_label = Label (text = "Email/Username:" )
78
114
username_label .grid (row = 2 , column = 0 )
@@ -92,7 +128,7 @@ def validate_entries(website, username, password):
92
128
password_btn .grid (row = 3 , column = 2 )
93
129
94
130
# Add to list button
95
- add_info_btn = Button (text = "Add" , width = 36 , command = add_information )
131
+ add_info_btn = Button (text = "Add" , width = 36 , command = save )
96
132
add_info_btn .grid (row = 4 , column = 1 , columnspan = 2 )
97
133
98
134
window .mainloop ()
0 commit comments