-1
我正在嘗試使用數據庫從中讀取有效的登錄名。一列是用戶名,另一列是密碼。我如何編寫我的代碼,以便用戶的用戶名輸入以及來自用戶的密碼輸入匹配數據庫中的一行,它將驗證登錄並移到下一個頁面,該示例稱爲「屏幕「SQLite3讀取數據庫的用戶名和密碼
我在下面創建了一個演示登錄頁面,其中添加了創建數據庫功能。我必須編輯/添加什麼才能填補我的目標。
from tkinter import * #imports all the GUI libraries
from tkinter import messagebox #so it can be used outside idle
from tkinter import ttk
import sqlite3
class LoginPopup():
#constructor sets up the buttons
def __init__(Top):
Top.GenericGui = Tk()#creating a window
Top.GenericGui.title('Login')
Top.GenericGui.geometry('300x200+250+30')
Top.Check = 1
Top.GenericGui.iconbitmap('home.ico')
Password = StringVar()
Username = StringVar()
conn = sqlite3.connect('Logins.db')
c = conn.cursor()
#takes you back to the main menu
def BackToMain():
Top.delete()
openscreen = Screen()
def create_table():
c.execute("CREATE TABLE IF NOT EXISTS stuffToPlot(Username TEXT, Password TEXT)")
def read_from_db():
c.execute('SELECT * FROM stuffToPlot')
data = c.fetchone()
for row in data:
print(row)
def UsernameAndPassword(Top):
UsernameAttempt = Username.get()#how to get value from entry box
PasswordAttempt = Password.get()#how to get value from entry box
#Login Components
LoginVerify = ttk.Button(Top.GenericGui, text="Login", command = lambda: UsernameAndPassword(Top)).place(x=220,y=170)
ttk.Entry(Top.GenericGui,textvariable = Username,width = 20).place(x=140,y=60)
ttk.Entry(Top.GenericGui,show = '*',textvariable = Password,width = 20).place(x=140,y=100)#can hide letters entered with *
ttk.Label(Top.GenericGui,text = 'Username:').place(x=40,y=60)
ttk.Label(Top.GenericGui,text = 'Password:').place(x=40,y=100)
Label(Top.GenericGui,text = ' Please Enter Your Username \nand Password ',font = ('TkDefaultFont',12)).pack(side = TOP, fill = X)
create_table()
read_from_db()
c.close
conn.close()
class Screen(LoginPopup):
#constructor sets up the buttons
def __init__(Top):
Top.GenericGui = Tk()#creating a window
Top.GenericGui.title('Screen')
Top.GenericGui.geometry('300x200+250+30')
Top.Check = 1
Top.GenericGui.iconbitmap('home.ico')
Password = StringVar()
Username = StringVar()
runprogram = LoginPopup()
感謝您抽出寶貴的時間來閱讀和任何幫助,將不勝感激
@JohnGordon感謝您的幫助,我將如何將它集成到「If」語句中,基本上說,如果用戶輸入的用戶名和密碼與數據庫中的某行匹配,它將前進到下一個屏幕,而else函數會包括我向他們展示了一個錯誤消息框 – Ross
@Ross我不熟悉Tk,所以我不知道如何讓它繼續下一步。 –