Bhaskar pal
Bhaskar pal

Reputation: 154

How to take information that takes entered into a entry box and save(dump) it in a text file(json)

I created a program which should dump the username and password in json file

But i am having problem in solving it

plz help

def createAccount():
    A = tk.StringVar()
    B = tk.StringVar()
    root1 = Tk()
    root1.resizable(0, 0)
    root1.title('Signup')
    instruction = Label(root1, text='Please Enter new Credentials')
    instruction.grid(row=0, column=0, sticky=E)

    nameL = Label(root1, text='New Username: ')
    pwordL = Label(root1, text='New Password: ')
    nameL.grid(row=1, column=0, sticky=W)
    pwordL.grid(row=2, column=0, sticky=W)

    nameE = Entry(root1, textvariable=A)
    pwordE = Entry(root1, textvariable=B )
    nameE.grid(row=1, column=1)
    pwordE.grid(row=2, column=1)

    signupButton = Button(root1, text='Signup')
    signupButton.grid(columnspan=2, sticky=W)
    root1.mainloop()
    username = A
    password = B
    with open('user_accounts.json', 'r+') as user_accounts:
        users = json.load(user_accounts)
        if username in users.keys():
            print('error')

        else:
                users[username] = [password, "PLAYER"]
                user_accounts.seek(0)
                json.dump(users, user_accounts)
                user_accounts.truncate()
                print("success")

I tried to convert username and password into string by using tk.StringVar()

But a error is displayed enter image description here

Plz provide any appropriate solution

Upvotes: 1

Views: 61

Answers (1)

pitamer
pitamer

Reputation: 1064

tkinter labels don't use normal Python variables types. Instead, they use tcl types, such as StringVar. to get the values of such variables, you can call their .get() method, which returns a native Python value. Now you can convert, change and use it as you like :)

Upvotes: 1

Related Questions