简体   繁体   English

如何从tkinter条目小部件获取值,以便可以对其进行验证?

[英]How do I get a value from a tkinter entry widget, so that it can be validated?

I am setting up a login page that is linked to an SQLite database. 我正在设置一个链接到SQLite数据库的登录页面。 What I am trying to accomplish is to get and validate the entry from the UserNameBox and PasswordBox entry. 我要完成的工作是从UserNameBox和PasswordBox条目中获取并验证该条目。 From there I want to pass down the values into a subroutine, which would validate from the connected SQL database(Using SQLite) to see if the values are valid. 在这里,我想将值传递到一个子例程中,该子例程将从连接的SQL数据库(使用SQLite)进行验证,以查看这些值是否有效。 The problem is I can't seem to find a way/tutorial to get the values entered in the entry box. 问题是我似乎找不到找到在输入框中输入值的方法/教程。

From what I already know/Learnt, you have to use a .get function to get the values from the entry widget, but I don't know how to pass down the values from the widgets into a subroutine to be validated. 据我所知/学到的,您必须使用.get函数从条目小部件中获取值,但是我不知道如何将小部件中的值传递给要验证的子例程。

import tkinter as tk
import os
import sqlite3

LARGE_FONT = ("Courier", 20)
SMALL_FONT = ("Courier", 10)

class Frame(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        MainFrame = tk.Frame(self)
        MainFrame.pack(side="top", fill="both", expand =True)
        MainFrame.grid_rowconfigure(0, weight=1)
        MainFrame.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (LoginPageGUI, QuitGUI):
            frame = F(MainFrame, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(LoginPageGUI)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

def xD(StringToPrint):
    print(StringToPrint)

class LoginPageGUI(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        LoginPageTitle = tk.Label(self, text="Login Page", font=LARGE_FONT)
        LoginPageTitle.pack(padx=10, pady=10)

        UserNameTitle = tk.Label(self, text="UserName", font=SMALL_FONT)
        UserNameTitle.pack()
        UserNameBox = tk.Entry(self, bd=7, fg="Black")
        UserNameBox.pack()

        PasswordTitle = tk.Label(self, text="Password", font=SMALL_FONT)
        PasswordTitle.pack()
        PasswordBox = tk.Entry(self, bd=7, fg="Black")
        PasswordBox.pack()

        EnterButton = tk.Button(self, text="Enter", fg="black",
                               command=lambda: os.system('python Reaction_Testing_GUI_version_2.py'))
        EnterButton.pack()

        MakeNewAccountButton = tk.Button(self, text="Make a new account", fg="black",
                               command=lambda: controller.show_frame(NewAccountPage))
        MakeNewAccountButton.pack()

        QuitButton = tk.Button(self, text="Quit", fg="red",
                               command=lambda: controller.show_frame(QuitGUI))
        QuitButton.pack()


class NewAccountpage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        NewAccountTitle = tk.Label(self, text="Make a new Account", font=LARGE_FONT)
        NewAccountTitle.pack(padx=10, pady=10)

        UserNameTitle = tk.Label(self, text="UserName", font=SMALL_FONT)
        UserNameTitle.pack()
        UserName = UserNameTitle.get()
        UserNameBox = tk.Entry(self, bd=7, fg="Black")
        UserNameBox.pack()

        PasswordTitle = tk.Label(self, text="Password", font=SMALL_FONT)
        PasswordTitle.pack()
        PasswordBox = tk.Entry(self, bd=7, fg="Black")
        PasswordBox.pack()

        EnterButton = tk.Button(self, text="Enter", fg="black",
                                command=lambda: Create_New_Account())
        EnterButton.pack()

        MakeNewAccountButton = tk.Button(self, text="Make a new account", fg="black",
                                         command=lambda: controller.show_frame(NewAccountPage))
        MakeNewAccountButton.pack()

        QuitButton = tk.Button(self, text="Quit", fg="red",
                               command=lambda: controller.show_frame(QuitGUI))
        QuitButton.pack()

class QuitGUI(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        QuitTitle = tk.Label(self, text="Are You Sure You Want To Quit?", font=LARGE_FONT)
        QuitTitle.pack(pady=10, padx=10)

        YesButton = tk.Button(self, text="Yes", fg="black",
                                 command=lambda: xD("xD it works"))
        YesButton.pack()

        GoBackButton = tk.Button(self, text="Go back to main page", fg="black",
                                 command=lambda: controller.show_frame(LoginGUI))
        GoBackButton.pack()

def check_Username():
    with sqlite3.connect('Database_Version_1.db') as db:
        Cursor = db.cursor()
        for name in(UserNameBox):
            Cursor.execute("SELECT Username FROM User WHERE Username = ?"(UserNameBox))
            exist = Cursor.fetchall()
            if len(exist)==0:
                print("There is no component named %s"%name)
            else:
                print()

LoginGUI = Frame()
LoginGUI.mainloop()

I want it so that I am able to pass down the values from UserNameBox and PasswordBox entry when I press EnterButton, the values are passed down into a subroutine, where the Username and Password are validated. 我想要它,以便当我按下EnterButton时能够从UserNameBox和PasswordBox条目中传递值,这些值向下传递到一个子例程中,在该例程中验证了Username和Password。 Pls help thanks. 请帮助谢谢。

More or less. 或多或少。

I use self. 我用self. to have access to self.UserNameBox and self.PasswordBox in other methods in class LoginPageGUI . 有机会获得self.UserNameBoxself.PasswordBox在类的其他方法LoginPageGUI

I assign method check_username to button. 我将方法check_username分配给button。 This method gets values from Entry s and runs external function check_Username with parameters username, password . 此方法从Entry获取值,并运行带有参数username, password外部函数check_Username

check_Username returns True or False and I use it to run external program. check_Username返回TrueFalse ,我用它来运行外部程序。

class LoginPageGUI(tk.Frame):

    def __init__(self, parent, controller):

        self.UserNameBox = tk.Entry(self)

        self.PasswordBox = tk.Entry(self)

        EnterButton = tk.Button(self, text="Enter", command=self.check_login)


    def check_login(self):
        user_name = self.UserNameBox.get()
        password = self.PasswordBox.get()

        if check_Username(user_name, password):            
            os.system('python Reaction_Testing_GUI_version_2.py')


def check_Username(username, password):                                
    with sqlite3.connect('Database_Version_1.db') as db:
        Cursor = db.cursor()
        Cursor.execute("SELECT Username FROM User WHERE Username = ?", (username,))
        exist = Cursor.fetchall()
        if len(exist) == 0:
            print("There is no component named %s" % user_name)
            return False
        else:
            # ... check password and return True or False ...
            return True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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