Python -按钮内部的IF函数



我有以下代码的问题。默认情况下,它是一个简单的代码,用于切换帧。我尝试做的是修改LoginPage类来执行它所说的—login;),正如您所看到的,我有一个test.db SQL数据库。它包含表用户,列有:(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")

最新更新