-2

here is my code it is meant to store the data into a json file

from tkinter import *
from tkinter import messagebox
import pyperclip
import json
# ---------------------------- PASSWORD GENERATOR ------------------------------- #
import random
def password_generator():
    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_list = []

    for char in range(nr_letters):
      password_list.append(random.choice(letters))

    for char in range(nr_symbols):
      password_list += random.choice(symbols)

    for char in range(nr_numbers):
      password_list += random.choice(numbers)



    random.shuffle(password_list)

    password = ""
    for char in password_list:
      password += char
    passinput.delete(0,END)
    passinput.insert(0, password)
    pyperclip.copy(password)
# --------------sbh-------------- SAVE PASSWORD ------------------------------- #
def save():
    get_web=webinput.get()
    get_pass=passinput.get()
    get_mail=emailinput.get()
    new_data={
        get_web: {
            "email": get_mail,
            "password":get_mail
        }
    }


    if len(get_web) and len(get_mail) and len(get_pass) != 0:
        is_ok= messagebox.askokcancel(title="Website", message=f"These are the details you entered: \nEmail:{get_mail}"
                                                        f"\n Website: {get_web} \n Password: {get_pass} \n  is it ok to save?")

        if is_ok:
            try:
                with open("data.json","r") as data_file:
                    data = json.load(data_file)

            except FileNotFoundError:
                with open("data.json", "w") as data_file:
                    json.dump(new_data, data_file, indent=4)
            else:
                data.update(new_data)
                with open("data.json", "w") as data_file:
                    json.dump(data, data_file, indent=4)
            finally:
                webinput.delete(0, END)
                passinput.delete(0, END)


    elif len(get_web) or len(get_mail) or len(get_pass) == 0:
        messagebox.showinfo(title="Error", message="Dont leave any of the field empty")
# ---------------------------- UI SETUP ------------------------------- #
window= Tk()
window.title("Password Generator")
window.minsize(width=500, height=500)
window.config(padx=20,pady=20,bg="#B1DDC6")
my_img= PhotoImage(file="logo.png")
canvas=Canvas(height=200,width=200)
canvas.create_image(100,100,image=my_img)
canvas.grid(column=1, row=0)

webtext=Label(text="Website:")
webtext.grid(column=0,row=1)
webinput=Entry(width=35)
webinput.focus()
webinput.grid(column=1, row=1, columnspan=2)

webemail=Label(text="Email/Username:")
webemail.grid(column=0, row=2)
emailinput=Entry(width=35)
emailinput.insert(END,string="ayodeledanny4@gmail.com")
emailinput.grid(column=1, row=2, columnspan=2)

passtext=Label(text="Password:")
passtext.grid(column=0,row=3)
passinput=Entry(width=21)
passinput.grid(column=1,row=3)

generate_button=Button(text="Generate Password", command=password_generator)
generate_button.grid(column=2,row=3)

add_button=Button(text="Add", width=36, command=save)
add_button.grid(column=1,row=4,columnspan=2)




window=mainloop()

and here is my error

/usr/local/bin/python3.11 /Users/user/Downloads/python projects/password-manager-start/main.py 
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/tkinter/__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "/Users/user/Downloads/python projects/password-manager-start/main.py", line 57, in save
    data = json.load(data_file)
           ^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 293, in load
    return loads(fp.read(),
           ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Process finished with exit code 0
Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
Dannyrayz
  • 1
  • 1
  • 2
    An empty value is being passed to the decoder. Your data.json is empty. – Michael Ruth Aug 10 '23 at 22:03
  • This problem doesn't seem to have anything to do with tkinter. Have you verified that the string you're trying to decode is valid json? – Bryan Oakley Aug 10 '23 at 23:17
  • Did you try to check what is in the file, when you load it as JSON? Do you know what it should look like? Does it look right? (To make sure: you do understand that an *empty* JSON file is **not valid**, right?) For future questions, please read [mre]. Don't show Tkinter code unless you are asking a question about the GUI. Instead, figure out *specific* code that *directly* demonstrates the problem; and make sure that others can see *everything* needed to reproduce the problem (including e.g. any data files you are trying to read). – Karl Knechtel Aug 11 '23 at 01:58

0 Answers0