Oct-19-2020, 08:04 PM
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.
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()