【问题标题】:Python - IF function inside of the buttonPython - 按钮内的 IF 函数
【发布时间】:2016-09-24 19:06:46
【问题描述】:

下面的代码有问题。默认情况下,它是一个简单的代码,用于切换帧。我尝试做的是修改 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()

【问题讨论】:

  • 你必须比“每次我接近这个班级我打破密码”更具体。究竟出了什么问题,您遇到了什么错误,您是如何尝试解决的?
  • 嗯,主要问题是我并不是一个非常好的程序员。除此之外 - 显然删除其中一个按钮是没有问题的。添加简单的标签或字段以输入代码也是如此。我的主要问题是弄清楚如何在按钮内嵌入 IF 函数(因此它读取登录名和密码并运行 SQL 查询以获取输入的登录密码并将其与输入的密码进行比较)。我完全一无所知。现在我正在搜索谷歌,但我找不到解决方案。

标签: python sql tkinter


【解决方案1】:

考虑添加一个方法 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")

【讨论】:

    猜你喜欢
    • 2020-08-17
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 2017-03-27
    相关资源
    最近更新 更多