From 92da43d1321d1cde7f4603d1d29e9cde7ce5e938 Mon Sep 17 00:00:00 2001 From: Rakicc Date: Tue, 5 Nov 2024 10:08:19 +0530 Subject: [PATCH] Update main.py --- .../Password_manager/main.py | 114 ++++++++++-------- 1 file changed, 64 insertions(+), 50 deletions(-) diff --git a/projects/Password Projects/Password_manager/main.py b/projects/Password Projects/Password_manager/main.py index 78732f06..3862e924 100644 --- a/projects/Password Projects/Password_manager/main.py +++ b/projects/Password Projects/Password_manager/main.py @@ -8,20 +8,28 @@ def searc(): try: with open("data.json", "r") as f: d = json.load(f) - p = d[webinput.get()]["password"] - e = d[webinput.get()]["email"] - messagebox.showinfo( - title=f"{webinput.get()}", message=f"Email is {e} \n password is {p}" - ) - - except KeyError: - messagebox.showinfo(title="Not Found", message="Data not found") + website = webinput.get() + if website in d: + email = d[website]["email"] + password = d[website]["password"] + + # Update the entry fields directly + emailinput.delete(0, END) + emailinput.insert(0, email) + passinput.delete(0, END) + passinput.insert(0, password) + else: + messagebox.showinfo(title="Not Found", message="Data not found for the website.") except FileNotFoundError: - messagebox.showinfo(title="Warning", message="File missing") + messagebox.showinfo(title="Warning", message="Data file not found. Please save a password first.") + except json.JSONDecodeError: + messagebox.showinfo(title="Error", message="Error reading data file. Please check the file format.") + # ---------------------------- PASSWORD GENERATOR ------------------------------- # def generator(): + passinput.delete(0, END) # Clear previous password passinput.insert(0, generatedpass()) @@ -31,53 +39,58 @@ def savedata(): email = emailinput.get() passwo = passinput.get() new_data = {website: {"email": email, "password": passwo}} - if emailinput.get() == "" or webinput.get() == "" or passinput.get() == "": - messagebox.showinfo( - title="Warning", message="Please fill all the enteries to proceed" - ) - else: - is_ok = messagebox.askokcancel( - title=webinput.get(), - message=f"Check all details \n Password : {passinput.get()} \n email : {emailinput.get()} ", - ) - if is_ok: - try: - with open("data.json", "r") as f: - data = json.load(f) - data[website] = new_data[website] - with open("data.json", "w") as f: - json.dump(data, f, indent=4) - except: - with open("data.json", "w") as f: - json.dump(new_data, f, indent=4) - - emailinput.delete(0, END) - webinput.delete(0, END) - passinput.delete(0, END) + + if not website or not email or not passwo: + messagebox.showinfo(title="Warning", message="Please fill in all fields to proceed.") + return + + is_ok = messagebox.askokcancel( + title=website, + message=f"Check all details\nPassword: {passwo}\nEmail: {email}" + ) + + if is_ok: + try: + with open("data.json", "r") as f: + data = json.load(f) + except (FileNotFoundError, json.JSONDecodeError): + data = {} + + data.update(new_data) + + with open("data.json", "w") as f: + json.dump(data, f, indent=4) + + webinput.delete(0, END) + emailinput.delete(0, END) + passinput.delete(0, END) # ---------------------------- UI SETUP ------------------------------- # window = Tk() - -window.title("Password Managger") +window.title("Password Manager") window.config(padx=50, pady=50) -canva = Canvas(width=200, height=200) -log = PhotoImage(file="logo.png") -canva.create_image(100, 100, image=log) -canva.grid(row=0, column=1) -website = Label(text="Website") -website.grid(row=1, column=0) +canvas = Canvas(width=200, height=200) +logo = PhotoImage(file="logo.png") +canvas.create_image(100, 100, image=logo) +canvas.grid(row=0, column=1) + +# Labels +website_label = Label(text="Website:") +website_label.grid(row=1, column=0) -email = Label(text="Email") -email.grid(row=2, column=0) +email_label = Label(text="Email:") +email_label.grid(row=2, column=0) -Password = Label(text="Password") -Password.grid(row=3, column=0) +password_label = Label(text="Password:") +password_label.grid(row=3, column=0) +# Entries webinput = Entry(width=30) webinput.grid(row=1, column=1) +webinput.focus() # Focus on website input on start emailinput = Entry(width=30) emailinput.grid(row=2, column=1) @@ -85,13 +98,14 @@ def savedata(): passinput = Entry(width=30) passinput.grid(row=3, column=1) -genbtn = Button(text="genetrate", command=generator) -genbtn.grid(row=3, column=2) +# Buttons +generate_button = Button(text="Generate", command=generator) +generate_button.grid(row=3, column=2) -add = Button(text="ADD", width=36, command=savedata) -add.grid(row=4, column=1, columnspan=2) +add_button = Button(text="Add", width=36, command=savedata) +add_button.grid(row=4, column=1, columnspan=2) -search = Button(text="Search", command=searc) -search.grid(row=1, column=2) +search_button = Button(text="Search", command=searc) +search_button.grid(row=1, column=2) window.mainloop()