[Python] How can i read a username and password from a .txt file using tkinter

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I am reletively new to python and was trying to make a basic login page using tkinter and reading and writing data in a text file, so to have multiple 'accounts'. I was able to write the data in the format "a,b \n", and i am trying to then read the data as a username and password to verify a login, again working with multiple accounts.

    This is my program:

    import tkinter as tk

    from ctypes import windll

    root = tk.Tk()
    root.title("Log-in Page")
    root.configure(background = "#e7d1ff")
    windll.shcore.SetProcessDpiAwareness(1)

    def login_data():
    for file in open("saveData.txt", "r").readlines():
    login = file.split()
    print(login)

    root.geometry('600x400')
    root.resizable(False, False)

    root.columnconfigure(0, weight = 1)
    root.columnconfigure(1, weight = 3)

    userLabel = tk.Label(root, text="Username:", font="arial 22 bold", bg = "#e7d1ff")
    userLabel.grid(row=0, column=0, sticky=tk.E, padx=5, pady=10)

    userEntry = tk.Entry(root, font="helvetica 22")
    userEntry.grid(row = 0, column = 1, padx=5, pady=10)

    passLabel = tk.Label(root, text="Password:", font="arial 22 bold", bg = "#e7d1ff")
    passLabel.grid(row=1, column=0, sticky=tk.E, padx=5, pady=10)

    passEntry = tk.Entry(root, font="helvetica 22", show="*")
    passEntry.grid(row = 1, column = 1, padx=5, pady=10)

    def test_function():
    file = open("saveData.txt", "a")
    user_info = str(userEntry.get() + "," + str(passEntry.get() + "\n"))
    file.write(user_info)
    file.close()

    btnLogin = tk.Button(root, text="Login", font="arial 16 bold", command = test_function)
    btnLogin.place(relx=0.5, rely=0.4, anchor=tk.CENTER)

    btnTest = tk.Button(root, text="Test", font="arial 16 bold", command = login_data)
    your textbtnTest.place (relx=0.5, rely=0.6, anchor=tk.CENTER)

    root.mainloop()


    I am stuck on the login_data function. How can i make this work?

    Continue reading...

Compartilhe esta Página