我正在设置一个链接到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条目中传递值,这些值会传递到一个子例程中,在其中验证用户名和密码。请帮助谢谢。
答案 0 :(得分:1)
或多或少。
我使用self.
来访问类self.UserNameBox
中其他方法中的self.PasswordBox
和LoginPageGUI
。
我将方法check_username
分配给按钮。该方法从Entry
获取值,并使用参数check_Username
运行外部函数username, password
。
check_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