KieranF
KieranF

Reputation: 79

Python Tkinter program will not send data to my text file correctly

I am currently trying to learn tkinter so building an application to save 3 variables, in this case username, password and website into a text file. Obviously actually using this method to save your passwords is an awful idea but its just for the sake of learning.

I have a problem where my application is running perfectly on the first go only. It takes in 3 variables and stores them in a text file following a button click. The problem is when I try to do this again I get an attribute error. I want to be able to append the file so I can fill in as many entries as I want without having to close it down and open it again.

AttributeError: 'str' object has no attribute 'get'

Here is my code.

    from tkinter import *
    import tkinter.messagebox

    def save_info():
        global username
        global password
        global website

        username = username.get()
        password = password.get()
        website = website.get()

        print(username,password,website)

        file = open("Software.txt","a")
        file.write("Username: " + username)
        file.write("\n")
        file.write("Password: " + password)
        file.write("\n")
        file.write("Website: " + website)
        file.write("\n")
        file.write("----------")
        file.write("\n")
        file.close()

app = Tk()
app.geometry("500x500")
app.title("Password Manager")
heading = Label(text="Password Manager",fg="white",bg="blue",width="500",height="3",font="10")
heading.pack()

username_text = Label(text="Username: ")
password_text = Label(text="Password: ")
website_text = Label(text="Website: ")
username_text.place(x=15,y=70)
password_text.place(x=15,y=140)
website_text.place(x=15,y=210)

username = StringVar()
password = StringVar()
website = StringVar()

username_entry = Entry(textvariable=username,width="30")
password_entry = Entry(textvariable=password,width="30")
website_entry = Entry(textvariable=website,width="30")
username_entry.place(x=15,y=100)
password_entry.place(x=15,y=170)
website_entry.place(x=15,y=240)

button = Button(app,text="Submit Data",command=lambda: 
[save_info(),onClick()],width="30",height="2",bg="lightgrey")
button.place(x=15,y=290)

def onClick():
    tkinter.messagebox.showinfo("Password Manager","Data Saved Successfully")

mainloop()

Upvotes: 2

Views: 146

Answers (2)

typedecker
typedecker

Reputation: 1370

When your function(save_info) is called for the first time, the lines -:

username = username.get()
password = password.get()
website = website.get()

set the 3 tkinter StringVars, to their string values, this is the reason that the next time you call the same function it throws an AttributeError, The easy fix to this would be to change these lines within the function, to store the value in 3 differently named variables, say username_, password_, website_.

The code then becomes -:

username_ = username.get()
password_ = password.get()
website_ = website.get()

Upvotes: 2

Finn
Finn

Reputation: 2343

In your save_info() you overwrite the 3 varibales that were StringVar() so far. You have to find new variable names for them to store the strings.

def save_info():
    global username
    global password
    global website

    username_str = username.get()
    password_str = password.get()
    website_str = website.get()
    print(username,password,website)

    file = open("Software.txt","a")
    file.write("Username: " + username_str)
    file.write("\n")
    file.write("Password: " + password_str)
    file.write("\n")
    file.write("Website: " + website_str)
    file.write("\n")
    file.write("----------")
    file.write("\n")
    file.close()

Upvotes: 3

Related Questions