1
1
from tkinter import *
2
2
import pyperclip
3
+ import json
3
4
from tkinter import messagebox
4
5
from password_gen import generatePassword
5
6
LBLUE = '#eff8ff'
6
7
BLUE = '#cae4db'
7
8
8
9
# ---------------------------- PASSWORD GENERATOR ------------------------------- #
10
+ def genPassword ():
11
+ password_entry .delete (0 ,END )
12
+ new_password = generatePassword ()
13
+ # Copy password to clipboard
14
+ pyperclip .copy (new_password )
15
+ password_entry .insert (index = 0 , string = new_password )
9
16
10
17
# ---------------------------- SAVE PASSWORD ------------------------------- #
18
+ def addPassword ():
19
+ website = website_entry .get ()
20
+ email = email_entry .get ()
21
+ password = password_entry .get ()
22
+ new_data = {
23
+ website : {
24
+ 'email' : email ,
25
+ 'password' : password
26
+ }
27
+ }
28
+ # Check for empty fields
29
+ if website == '' or email == '' or password == '' :
30
+ messagebox .showwarning (title = 'Opps!' , message = "Please don't leave any field empty" )
31
+ else :
32
+ # Show confirmation popup
33
+ is_ok = messagebox .askokcancel (
34
+ title = f'{ website } ' ,
35
+ message = f'Are you sure? \n Email: { email } \n Password: { password } '
36
+ )
37
+ # Add new data to data.json
38
+ if is_ok :
39
+ try :
40
+ with open ('data.json' ,'r' ) as f :
41
+ # Read old data
42
+ data = json .load (f )
43
+ except (FileNotFoundError , json .decoder .JSONDecodeError ):
44
+ # If no data found
45
+ data = new_data
46
+ else :
47
+ # Update old data with new data
48
+ data .update (new_data )
49
+ finally :
50
+ with open ('data.json' ,'w' ) as f :
51
+ # Saving new data to json
52
+ json .dump (data , f , indent = 4 )
53
+ website_entry .delete (0 ,END )
54
+ password_entry .delete (0 ,END )
55
+ website_entry .focus ()
56
+
57
+ # ---------------------------- FIND PASSWORD ------------------------------- #
58
+ def findPassword ():
59
+ website = website_entry .get ()
60
+ try :
61
+ with open ('data.json' ,'r' ) as f :
62
+ data = json .load (f )
63
+ email = data [website ]['email' ]
64
+ password = data [website ]['password' ]
65
+ messagebox .askokcancel (
66
+ title = f'{ website } ' ,
67
+ message = f'Email: { email } \n Password: { password } '
68
+ )
69
+ except (KeyError , FileNotFoundError ):
70
+ messagebox .showwarning (title = 'Error' , message = f"No details for { website } exists." )
11
71
12
72
# ---------------------------- UI SETUP ------------------------------- #
13
73
window = Tk ()
31
91
32
92
# Entry
33
93
website_entry = Entry ()
34
- website_entry .grid (row = 2 , column = 2 , padx = 10 , pady = 5 , columnspan = 2 , sticky = 'EW' )
94
+ website_entry .grid (row = 2 , column = 2 , padx = 10 , pady = 5 , sticky = 'EW' )
35
95
website_entry .focus ()
36
96
email_entry = Entry ()
37
97
email_entry .grid (row = 3 , column = 2 , padx = 10 , pady = 5 , columnspan = 2 , sticky = 'EW' )
38
98
email_entry .insert (index = END , string = 'youremail@email.com' )
39
99
password_entry = Entry ()
40
100
password_entry .grid (row = 4 , column = 2 , padx = 10 , pady = 5 , sticky = 'EW' )
41
101
42
- def genPassword ():
43
- password_entry .delete (0 ,END )
44
- new_password = generatePassword ()
45
- # Copy password to clipboard
46
- pyperclip .copy (new_password )
47
- password_entry .insert (index = 0 , string = new_password )
48
-
49
- def addPassword ():
50
- # Check for empty fields
51
- if website_entry .get ()== '' or email_entry .get ()== '' or password_entry .get ()== '' :
52
- messagebox .showwarning (title = 'Opps!' , message = "Please don't leave any field empty" )
53
- else :
54
- # Show confirmation popup
55
- is_ok = messagebox .askokcancel (
56
- title = f'{ website_entry .get ()} ' ,
57
- message = f'Are you sure? \n Email: { email_entry .get ()} \n Password: { password_entry .get ()} '
58
- )
59
- # Add new data to data.txt
60
- if is_ok :
61
- with open ('data.txt' ,'a' ) as f :
62
- # Using print() to write files
63
- print (f"{ website_entry .get ()} | { email_entry .get ()} | { password_entry .get ()} " , file = f )
64
- website_entry .delete (0 ,END )
65
- password_entry .delete (0 ,END )
66
- website_entry .focus ()
67
-
68
102
# Button
69
103
gen_password_btn = Button (text = 'Generate Password' , command = genPassword , bg = BLUE )
70
104
gen_password_btn .grid (row = 4 , column = 3 )
71
105
add_btn = Button (text = 'Add' , command = addPassword , bg = BLUE )
72
106
add_btn .grid (row = 5 , column = 2 , columnspan = 2 , sticky = 'EW' )
107
+ search_btn = Button (text = 'Search' , command = findPassword , bg = BLUE )
108
+ search_btn .grid (row = 2 , column = 3 , columnspan = 2 , sticky = 'EW' )
73
109
74
110
window .mainloop ()
0 commit comments