簡體   English   中英

如何從tkinter條目小部件獲取值,以便可以對其進行驗證?

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

我正在設置一個鏈接到SQLite數據庫的登錄頁面。 我要完成的工作是從UserNameBox和PasswordBox條目中獲取並驗證該條目。 在這里,我想將值傳遞到一個子例程中,該子例程將從連接的SQL數據庫(使用SQLite)進行驗證,以查看這些值是否有效。 問題是我似乎找不到找到在輸入框中輸入值的方法/教程。

據我所知/學到的,您必須使用.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()

我想要它,以便當我按下EnterButton時能夠從UserNameBox和PasswordBox條目中傳遞值,這些值向下傳遞到一個子例程中,在該例程中驗證了Username和Password。 請幫助謝謝。

或多或少。

我用self. 有機會獲得self.UserNameBoxself.PasswordBox在類的其他方法LoginPageGUI

我將方法check_username分配給button。 此方法從Entry獲取值,並運行帶有參數username, password外部函數check_Username

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