Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从tkinter条目小部件中获取值,以便对其进行验证?_Python_User Interface_Tkinter_Login - Fatal编程技术网

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

Python 如何从tkinter条目小部件中获取值,以便对其进行验证?,python,user-interface,tkinter,login,Python,User Interface,Tkinter,Login,我正在设置一个链接到SQLite数据库的登录页面。我试图完成的是从UserNameBox和PasswordBox条目中获取并验证条目。从那里,我想将这些值传递到一个子例程中,该子例程将从连接的SQL数据库(使用SQLite)进行验证,以查看这些值是否有效。问题是我似乎找不到方法/教程来获取输入框中输入的值 据我所知,您必须使用.get函数从条目小部件获取值,但我不知道如何将小部件中的值传递到要验证的子例程中 import tkinter as tk import os import sqlite

我正在设置一个链接到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()
我想要它,这样我就能够从UserNameBox和PasswordBox条目中传递值。当我按下enter按钮时,这些值被传递到一个子例程中,在子例程中验证用户名和密码。请帮忙,多谢。

多多少少

我使用
self.
在类
LoginPageGUI
的其他方法中访问
self.UserNameBox
self.PasswordBox

我将方法
check_username
分配给按钮。此方法从
Entry
s获取值,并使用参数
Username、password
运行外部函数
check\u Username

check\u Username
返回
True
False
,我用它来运行外部程序

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

LoginPageGUI
中,您可以使用
按钮
“Enter”来运行外部脚本,但您应该运行函数,该函数从
条目
中获取值,并使用
检查用户名
来检查这些值,然后可以运行外部脚本。