如何使我的 tkinter 登录系统正常工作?

问题描述

我想要的只是一个工作登录系统,并为我的程序提供一个基本的 Pastebin“数据库”,但我不知道该怎么做。

在您输入在 Pastebin 中编写的正确登录详细信息并按“Enter”按钮后,我想被重定向一个新窗口,我的程序将在其中打开,如果 Pastebin 登录详细信息有误,则不要被重定向。我该怎么做?

我的代码

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()

解决方法

URL 链接将获得 HTML 版本,因此您需要使用原始内容链接。

以下是修改后的 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

请注意,不建议存储纯文本密码。