5

Me and my friend are making a program that requires a login at the start. We've managed to get the user to input details, and have the program create a text file called (users name) profile with the details in. each one is formatted like this:

(Name)

(Username)

(Password)

when you want to log in, the program asks for your name and finds the file with the name (whatever they typed) profile. if it exists, the program then opens a GUI window and asks for the username and password, and if you put in the correct details for the file it has opened, it says that the details are wrong. We think its to do with the variables, but we've tried lot of different ways of laying it out e.t.c and can't find the problem. Can anyone help? (The code I have included is only the GUI part including the bit that isn't working, the rest is fine.

# Log in
def LogIn():
    name=input("Please enter your name: ")
    file = open(name.lower() + " profile.txt", "r")
#+=========GUI===========GUI============GUI===========+

    #mport modules
    import tkinter
    import time

    #---Window---#
    #make window
    window = tkinter.Tk()
    #change title
    window.title("Python Games Login")
    #change size
    window.geometry("270x210")
    #change window icon
    window.wm_iconbitmap("Login icon.ico")
    #change window colour
    window.configure(bg="#39d972")

    #---Commands---#
    #go
    def callback():
        line = file.readlines()
        username = user.get()
        password = passw.get()
        if username == line[1] and password == line[2]:
            message.configure(text = "Logged in.")
        else:
            message.configure(text = "Username and password don't match the account \n under the name;\n \'" + name + "\'. \nPlease try again.")
    #---Widgets---#
    #labels
    title1 = tkinter.Label(window, text="--Log in to play the Python Games--\n", bg="#39d972")
    usertitle = tkinter.Label(window, text="---Username---", bg="#39d972")
    passtitle = tkinter.Label(window, text="---Password---", bg="#39d972")
    message = tkinter.Label(window, bg="#39d972")

    #text entry windows
    user = tkinter.Entry(window)
    passw = tkinter.Entry(window, show='*')

    #buttons
    go = tkinter.Button(window, text="Log in!", command = callback, bg="#93ff00")

    #pack widgets
    title1.pack()
    usertitle.pack()
    user.pack()
    passtitle.pack()
    passw.pack()
    go.pack()
    message.pack()

    #start window
    window.mainloop()

#+===================GUI END=====================+

2 Answers 2

3

I would use python's pickle module to save data. It is much more high level than just saving it in a text file. In my example, I pickled a list of dictionaries.

import pickle
def LogIn():
    name=input("Please enter your name: ").lower()
    #data.pickle should be a list of dictionaries representing a user
    usernames= pickle.load('data.pickle')
    for userdata in usernames:
        if userdata['name']== name:
            return userdata
    #didn't find the name
    print('could not find '+ name+ ' in data.pickle')
    return None

A note about pickle from the docs:

Warning:

The pickle module is not intended to be secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.

Also check out shelve and marshal, they perform similar results, or consider saving it in a json file format (python has a json module)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, we'll try using the pickle module in a duplicate and see which works best/we prefer, thanks for the suggestion
No problem, if you have any trouble let me know, and Ill be gld to help out
2

Notice that readlines does not strip the end-of-line character(s) from the lines:

In [57]: f = open('data','r')

In [58]: f.readlines()
Out[58]: ['index,value\n', '0,16714217840939775\n', '1,16714217840939776 \n']

So username == line[1] is probably failing since username does not include the end-of-line character. And the same goes for password == line[2].

A simple fix would be to use

username == line[1].strip()

3 Comments

Thanks a lot, i hadn't though about the end of line characters, that makes a lot of sense now, thank you :)
For better security you may want to read this and store the bcrypt hash of the password rather than the password itself.
thanks, that will be useful if we need it in the future, but at the moment, security isnt the problem, we decided we could use accounts to store scores, so nothing is really being lost if someone decides to hack it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.