简体   繁体   中英

How do I make my tkinter login system work?

All I want is a working login system with a basic Pastebin "database" for my program but I do not know how to do it.

After you enter the right login details that are written in Pastebin and press the "Enter" button I want to be redirected to a new window where my program will open and if the Pastebin login details are wrong, not to be redirected. How do I do it?

My code:

from tkinter import *
import requests

win = Tk()
win.geometry("500x500")
win.title("Login Page")


def validateLogin():
    accounts = requests.get("https://pastebin.com/pzhDWPDq")

    print("username entered :", user1.get())
    print("password entered :", passwd1.get())

    user = user1.get()
    pword = passwd1.get()

    if f"{user}::{pword}" in accounts:
        return True
    else:
        return False


userlvl = Label(win, text="Username :")
passwdlvl = Label(win, text="Password  :")

user1 = Entry(win, textvariable=StringVar())
passwd1 = Entry(win, textvariable=IntVar().set(""))

enter = Button(win, text="Enter", command=lambda: validateLogin(), bd=0)

enter.configure(bg="pink")
user1.place(x=200, y=220)
passwd1.place(x=200, y=270)
userlvl.place(x=130, y=220)
passwdlvl.place(x=130, y=270)
enter.place(x=238, y=325)

win.mainloop()

The URL link will get the HTML version so you need to use the raw content link instead.

Below is a modified validateLogin() :

def validateLogin():
    # use raw URL link
    response = requests.get("https://pastebin.com/raw/pzhDWPDq")
    # requests.get() returns a response object
    # so use attribute 'content' to get the real content (type is bytes)
    # then use `decode()` to convert bytes to string
    # and finally split the content into list of lines
    accounts = response.content.decode().splitlines()

    user = user1.get()
    pword = passwd1.get()

    print("username entered :", user)
    print("password entered :", pword)

    print("OK" if f"{user}::{pword}" in accounts else "Failed")
    # or update a label text

Note that storing plain text password is not recommended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM