gpt4 book ai didi

Python - 按钮内的 IF 函数

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:03 24 4
gpt4 key购买 nike

我对下面的代码有疑问。默认情况下,它是一个简单的代码,用于切换框架。我尝试做的是修改 LoginPage 类以执行它所说的操作 - 登录;)如您所见,我有一个 test.db SQL 数据库。它包含具有以下列的表 users:(Id INT、Name TEXT、Password TEXT)我需要做的是输入登录名和密码,并将其与数据库中的用户进行比较。然后将它们定向到 LoginSuccessful 或 LoginFailed 帧。问题是每次我接近这个类时,我都会中断代码。

我完全不知道如何在按钮内插入 IF 语句。

只是澄清一下:它还没有加密(这只是一个学校项目),所以你不必提到它不安全,因为我非常清楚这一点:)大家有什么想法吗?

import tkinter as tk
import sqlite3 as lite
import sys
from Crypto.Cipher import AES

con = None
con = lite.connect('test.db')
cur = con.cursor()


TITLE_FONT = ("Helvetica", 18, "bold")

class SampleApp(tk.Tk):

def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)

# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)

self.frames = {}
for F in (LoginPage, LoginSuccessful, LoginFailed):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame

# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")

self.show_frame("LoginPage")

def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()


class LoginPage(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is the login page", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
inst_lbl = tk.Label(self, text = "Please enter your login credentials")
inst_lbl.pack()
pwa = tk.Label(self, text = "Login")
pwa.pack()
login = tk.Entry(self)
login.pack()
pwb = tk.Label(self, text = "pPassword")
pwb.pack()
password = tk.Entry(self)
password.pack()
button1 = tk.Button(self, text="Log in",
command=lambda: controller.show_frame("LoginSuccessful"))
button1.pack()



class LoginSuccessful(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Login was successful!", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the Login page",
command=lambda: controller.show_frame("LoginPage"))
button.pack()


class LoginFailed(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Login failed!", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the Login page",
command=lambda: controller.show_frame("LoginPage"))
button.pack()



if __name__ == "__main__":
app = SampleApp()
app.mainloop()

最佳答案

考虑添加方法 checkLogin,而不是当前始终打开成功屏幕的匿名 lambda 函数。此方法将运行参数化 SQL 查询来检查凭据,并根据结果调用登录成功或失败的帧:

SELECT 1 FROM users WHERE [Name] = ? AND [Password] = ?

然后,让登录按钮调用此方法。一项重要的内容是使用 self. 限定所有类变量,以便 checkLogin 可以使用返回的 LoginPage 框架用户输入值。下面是对 LoginPage 类的调整,唯一需要的更改是:

class LoginPage(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.label = tk.Label(self, text="This is the login page", font=TITLE_FONT)
self.label.pack(side="top", fill="x", pady=10)
self.inst_lbl = tk.Label(self, text = "Please enter your login credentials")
self.inst_lbl.pack()
self.pwa = tk.Label(self, text = "Login")
self.pwa.pack()
self.login = tk.Entry(self)
self.login.pack()
self.pwb = tk.Label(self, text = "pPassword")
self.pwb.pack()
self.password = tk.Entry(self)
self.password.pack()

button1 = tk.Button(self, text="Log in", command=self.checkLogin)
button1.pack()

def checkLogin(self):
cur.execute("SELECT 1 FROM users WHERE [Name] = ? AND [Password] = ?",
[self.login.get(), self.password.get()])
result = cur.fetchone()

if result is None:
self.controller.show_frame("LoginFailed")
else:
self.controller.show_frame("LoginSuccessful")

关于Python - 按钮内的 IF 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39679966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com