Python Forum
Basic python login system
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic python login system
#1
So I tried creating a basic login system in python since im just trying it to get a grasp of the language. Im using a txt file to save multiple usernames and passwords in it, while also making use of the tkinter library for a GUI. The user and password are always stored respectively above and below each other. When I got to the stage where I started coding the comparison between an input and the stored user/password i ran into the issue that for some reason when comparing the 2 strings in an if condition they never fit together. Even when I printed the 2 strings just before the condition and the console confirmed they were the same they never change the condition to true.

Maybe its just me thats missing some obvious stuff but I hope someone can give me a plausible answer for this.

from tkinter import *
import time


def getEntryInput():

    Input_User = USER.get()
    Input_Pass = PASS.get()

    UserList = open("User.txt", "r")

    RegisteredUsernames = UserList.readlines()  
    a = 0

    for x in RegisteredUsernames: 

        if Input_User == x:
            
            if Input_Pass == RegisteredUsernames[(a+1)]:
                SuccessLabel.configure(text="Success")
                break
            else:
                SuccessLabel.configure(text="Wrong")
        else:
            SuccessLabel.configure(text="Wrong")
        a = a + 1    
        

    UserList.close()


def RegNewUser():
    RegWindow = Toplevel()
    RegWindow.geometry("200x200")
    RegWindow.title("Register")

    RegTitleLabel = Label(RegWindow, text="Register new user")
    RegTitleLabel.pack()

    RegUSER = Entry(RegWindow)
    RegPASS = Entry(RegWindow)
    RegUSER.insert(0, "Username")
    RegPASS.insert(0, "Password")
    RegUSER.pack()
    RegPASS.pack()

    

root = Tk()
root.geometry("400x300")
titlelabel = Label(root, text="Please Enter User and Password")
SuccessLabel = Label(root)

LogButton = Button(root, text="Login", command=getEntryInput)
RegButton = Button(root, text="Register new user", command=RegNewUser)


USER = Entry(root)
PASS = Entry(root)
USER.insert(0, "Username")
PASS.insert(0, "Password")







titlelabel.pack()
SuccessLabel.pack()
USER.pack()
PASS.pack()
LogButton.place(x=150, y=100, height=50, width=100)
RegButton.place(x=150, y=150, height=50, width=100)
root.title("Login")
root.mainloop()
Reply
#2
You have no way to register users. Since you don't register new users I have no idea how user names and passwords are saved. Since I don't know how user names and passwords are saved I have no idea how to parse the password file.

I took your example and added code to register a new user. The resulting password file contains strings which are the username followed by a space followed by the password. Since the password file is saved in this format I use the same format for comparing the login request.
from tkinter import *
from functools import partial
 
 
def login(user, pwd):
    entry = f'{user.get()} {pwd.get()}\n'
    user.set("Username")
    pwd.set("Password")
    try:
        with open("User.txt", "r") as file:
            users = file.readlines()
    except FileNotFoundError:
        users = []

    for x in users:
        if entry == x:
            SuccessLabel.configure(text="Success")
            break
    else:
            SuccessLabel.configure(text="Wrong")
 

def register(user, pwd):
    entry = f'{user.get()} {pwd.get()}\n'
    user.set("Username")
    pwd.set("Password")
    try:
        with open("User.txt", "r") as file:
            users = file.readlines()
    except FileNotFoundError:
        users = []

    if entry not in users:
        users.append(entry)
        with open("User.txt", "w+") as file:
            file.writelines(users)

root = Tk()
root.geometry("400x300")
titlelabel = Label(root, text="Please Enter User and Password")
SuccessLabel = Label(root)

user_var = StringVar()
USER = Entry(root, textvariable = user_var)
user_var.set('Username')

pass_var  = StringVar()
PASS = Entry(root, textvariable = pass_var)
pass_var.set('Password')
 
LogButton = Button(root, text="Login", command=partial(login, user_var, pass_var))
RegButton = Button(root, text="Register new user", command=partial(register, user_var, pass_var))
 
titlelabel.pack()
SuccessLabel.pack()
USER.pack()
PASS.pack()
LogButton.place(x=150, y=100, height=50, width=100)
RegButton.place(x=150, y=150, height=50, width=100)
root.title("Login")
root.mainloop()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020