Python GUI Login Program

My friend and I are making a program that requires a login. We managed to force the user to enter detailed information and create a program to create a text file with the name (username) with detailed information. Each of them is formatted as follows:

(Name)

(Username)

(Password)

when you want to log in, the program asks for your name and finds a file with the name (no matter what they typed). if it exists, the program opens a GUI window and asks for a username and password, and if you enter the correct information about the file that opens, it says that the data is incorrect. We think this is due to variables, but we tried many different ways to lay etc and cannot find the problem. Can anyone help? (The code I included is just part of the GUI, including a bit that doesn't work, the rest is ok.

# 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=====================+
+4
source share
2 answers

Note that it readlinesdoes not separate the end of the line from the lines:

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

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

, username == line[1], , , username . password == line[2].

username == line[1].strip()
+1

python . , . .

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

:

:

. , .

, json ( python json-)

+2
source

Source: https://habr.com/ru/post/1530740/


All Articles