功能顺序有多重要?

时间:2019-02-07 17:52:55

标签: python tkinter sqlite

我有一个用户登录/注册程序,用于验证来自SQLite3数据库的数据。我已经在Tkinter GUI上正确运行了所有功能,即:我能够注册新用户并使用详细信息登录。注册过程创建了一个SQLite数据库,针对该数据库执行了登录验证。

当我刚刚运行Users表作为参考时,一切都正常。

然后我进行了一些更改,以整理代码并按正常运行的顺序从上到下(启动->注册->登录->登录成功->主应用)运行代码。< / p>

我做了以下事情:

1。添加了app_start函数,以便能够破坏初始的main_screen。将app_start函数作为mainloop()而不是main_screen函数运行。

  1. 将代码重构为按照GUI运行的顺序。

  2. 在“注册页面”上添加了其他用户条目以收集更多信息,并创建了一个新表。

代码现在创建空白的数据库条目,结果登录功能不起作用。

import tkinter as tk
from tkinter import *
import os
import time
import datetime
import sqlite3

screensize = "500x500"
popsize = "150x150"


conn = sqlite3.connect('UserInfo.db')
c = conn.cursor()


def main_screen():
    global screen
    screen = Tk()
    screen.geometry('300x200')
    screen.title("Main Screen")
    Label(text = "LOGIN PAGE", bg = "grey", width = "300", height = "2", font = ("Calibri", 12)).pack()
    Label(text = "").pack()
    Button(text = "Login", height = "2", width = "30", command = Login).pack()
    Label(text = "").pack()
    Button(text = "Register", height = "2", width = "30" , command = register).pack()


def register():
    global screen1
    screen1 = Tk()
    screen1.title("Register")
    screen1.geometry(screensize)

    global username
    global password
    global Firstname
    global Surname
    global Registrationnumber


    username = StringVar()
    password = StringVar()
    Firstname = StringVar()
    Surname = StringVar()
    Registrationnumber = StringVar()


    Label(screen1, text = "Please enter details below", font = ("Calibiri", 20)).place(x = 10, y = 50)
    Label(screen1, text = "")

    Label(screen1,text = "First Name / Company name*").place(x = 10, y = 120)
    global Firstname_entry
    Firstname_entry = Entry(screen1, textvariable = Firstname)
    Firstname_entry.place(x = 250, y = 120)

    Label(screen1,text = "Surname*").place(x = 10, y = 155)
    global Surname_entry
    Surname_entry = Entry(screen1, textvariable = Surname)
    Surname_entry.place(x = 250, y = 155)

    Label(screen1,text = "Registration number*").place( x = 10, y = 185)
    global Registrationnumber_entry
    Registrationnumber_entry = Entry(screen1, textvariable = Registrationnumber)
    Registrationnumber_entry.place(x = 250, y = 185)

    Label(screen1,text = "Username *").place(x = 10, y = 220)
    global username_entry
    username_entry = Entry(screen1, textvariable = username)
    username_entry.place(x = 250, y = 220)

    Label(screen1,text = "Password*").place(x = 10, y = 255)
    global password_entry
    password_entry = Entry(screen1, textvariable = password)
    password_entry.place(x = 250, y = 255)

    Button(screen1, text = "Register user now", command = register_user).place(x = 200, y = 300)
    Button(screen1, text = "Go to login", command = Login).place(x = 200, y = 350)


def register_user():


    Firstname_info = Firstname.get()
    Surname_info = Surname.get()
    Registrationnumber_info = Registrationnumber.get()
    username_info = username.get()
    password_info = password.get()

    c.execute("CREATE TABLE IF NOT EXISTS User_Information (Firstname TEXT , Surname TEXT , Registration TEXT, Username TEXT, Password TEXT )")
    c.execute("INSERT INTO User_Information (Firstname, Surname, Registration, Username, Password) VALUES(?, ?, ?, ?, ?)", (Firstname_info, Surname_info, Registrationnumber_info, username_info, password_info))

    c.execute("CREATE TABLE IF NOT EXISTS Users (Username TEXT NOT NULL, Password TEXT NOT NULL)")
    c.execute("INSERT INTO Users (Username, Password) VALUES(?, ?)", (username_info, password_info, ))

    conn.commit()

    User_registered()

    Firstname_entry.delete(0, END)
    Surname_entry.delete(0, END)
    Registrationnumber_entry.delete(0, END)
    username_entry.delete(0, END)
    password_entry.delete(0, END)


def User_registered():
    screen5 = Tk()
    screen5.title("Registration Complete")
    screen5.geometry(popsize)
    incorrect = Label(screen5, text = "User has been registered")
    incorrect.pack()
    Button(screen5, text = "okay", command = screen5.destroy).pack()

def Login():
    global screen2
    screen2 = Tk()
    screen2.title("Login")
    screen2.geometry(screensize)

    Label(screen2, text = "Please enter details to login").pack()
    Label(screen2, text = "").pack()

    global username_verify
    global password_verify

    username_verify = StringVar()
    password_verify = StringVar()

    Label(screen2, text = "Username").pack()
    global username_entry1
    username_entry1 = Entry(screen2, textvariable = username_verify)
    username_entry1.pack()
    Label(screen2, text = "Password").pack()
    global password_entry1
    password_entry1 = Entry(screen2, textvariable = password_verify)
    password_entry1.pack()
    Label(text = "")
    Button(screen2, text = "Login now",width = 10, height = 2, command = fetch_login).pack()
    Button(screen2, text = "Register new User", command = register).pack()
    screen.destroy()

def fetch_login():

    Uname = username_entry1.get()# Entry from UI
    Pword = password_entry1.get()# Entry from UI

    a = (Uname,)#Converting entry to tuple to match table data
    b = (Pword, )#Converting entry to tuple to match table data
    d = (a + b)
    print(d)
    print(type(d))# To ensure that entry is now a tuple


    c.execute('SELECT * FROM Users')
    data = c.fetchall()

    for row in data:
        print(row)# To see what SQLite3 table data is being drawn

    if d in data:
        c.execute('SELECT Password FROM Users WHERE Username = ?',(a))
        pdata = c.fetchall()
        print(pdata)        
        if b in pdata:
            Login_success()
        else:
            password_incorrect()
    else:
        User_not_found()


def password_incorrect():
    screen4 = Tk()
    screen4.title("ERROR!")
    screen4.geometry(popsize)
    incorrect = Label(screen4, text = "Password incorrect!")
    incorrect.pack()
    Button(screen4, text = "okay", command = screen4.destroy).pack()



def User_not_found():
    screen5 = Tk()
    screen5.title("ERROR!")
    screen5.geometry("300x100")
    not_found = Label(screen5, text = "User not found or password incorrect!")
    not_found.pack()
    Button(screen5, text = "okay", command = screen5.destroy).pack()



def Login_success():
    Main_Page() 

    screen2.destroy()

def Main_Page():
    global screen3
    screen3 = Tk()
    screen3.geometry(screensize)
    screen3.title("Main Page")

    menubar = tk.Menu(screen3)
    Filemenu = tk.Menu(menubar, tearoff = 0)

    Filemenu.add_command(label = "Add Asset", command = Add_Asset)
    Filemenu.add_separator()

    Filemenu.add_command(label = "Dispose Asset", command = Dipose_Asset)
    Filemenu.add_separator()

    Filemenu.add_command(label = "Edit Asset", command = Edit_Asset)
    Filemenu.add_separator()


    Filemenu.add_command(label = "Exit", command = quit)
    Filemenu.add_separator()



    menubar.add_cascade(label = "File", menu = Filemenu)


    screen3.config(menu=menubar)

def Add_Asset():
    print("Asset Added")

def Dipose_Asset():
    print("Dispose Asset")  

def Edit_Asset():
    print("Asset Added")



def app_start():
    main_screen()
    mainloop()

app_start()

我是编程新手。关于密码验证过程的任何建议将不胜感激。为了将它们与表中的数据进行比较,我不得不将条目转换为元组。

这是工作时的原始代码。

import tkinter as tk
from tkinter import *
import os
import time
import datetime
import sqlite3

screensize = "500x500"
popsize = "150x150"


conn = sqlite3.connect('UserInfo.db')
c = conn.cursor()


def register():
    global screen1
    screen1 = Toplevel(screen)
    screen1.title("Register")
    screen1.geometry(screensize)

    global username
    global password
    username = StringVar()
    password = StringVar()

    Label(screen1, text = "Please enter details below")
    Label(screen1, text = "")
    Label(screen1,text = "Username *").pack()
    global username_entry
    username_entry = Entry(screen1, textvariable = username)
    username_entry.pack()
    Label(screen1,text = "password *").pack()
    global password_entry
    password_entry = Entry(screen1, textvariable = password)
    password_entry.pack()
    Button(screen1, text = "Register user now", command = 
    register_user).pack()
    Button(screen1, text = "Go to login", command = Login).pack()


def register_user():

    username_info = username.get()
    password_info = password.get()

    c.execute("CREATE TABLE IF NOT EXISTS Users (Username TEXT NOT NULL, 
    Password TEXT NOT NULL)")
    c.execute("INSERT INTO Users (Username, Password) VALUES(?, ?)", 
    (username_info, password_info, ))

    conn.commit()


    User_registered()

    username_entry.delete(0, END)
    password_entry.delete(0, END)

def User_registered():
    screen5 = Tk()
    screen5.title("Registration Complete")
    screen5.geometry(popsize)
    incorrect = Label(screen5, text = "User has been registered")
    incorrect.pack()
    Button(screen5, text = "okay", command = screen5.destroy).pack()

def Login():
    global screen2
    screen2 = Toplevel(screen)
    screen2.title("Login")
    screen2.geometry(screensize)

    Label(screen2, text = "Please enter details to login").pack()
    Label(screen2, text = "").pack()

    global username_verify
    global password_verify

    username_verify = StringVar()
    password_verify = StringVar()

    Label(screen2, text = "Username").pack()
    global username_entry1
    username_entry1 = Entry(screen2, textvariable = username_verify)
    username_entry1.pack()
    Label(screen2, text = "Password").pack()
    global password_entry1
    password_entry1 = Entry(screen2, textvariable = password_verify)
    password_entry1.pack()
    Label(text = "")
    Button(screen2, text = "Login now",width = 10, height = 2, command = 
    fetch_login).pack()
    Button(screen2, text = "Register new User", command = register).pack()


def fetch_login():

    Uname = username_entry1.get()# Entry from UI
    Pword = password_entry1.get()# Entry from UI

    a = (Uname,)#Converting entry to tuple to match table data
    b = (Pword, )#Converting entry to tuple to match table data
    d = (a + b)
    print(d)
    print(type(d))# To ensure that entry is now a tuple


    c.execute('SELECT * FROM Users')
    data = c.fetchall()

    for row in data:
        print(row)# To see what SQLite3 table data is being drawn

    if d in data:
        c.execute('SELECT Password FROM Users WHERE Username = ?',(a))
        pdata = c.fetchall()
        print(pdata)        
        if b in pdata:
            Login_success()
        else:
            password_incorrect()
    else:
        User_not_found()


def password_incorrect():
    screen4 = Toplevel(screen2)
    screen4.title("ERROR!")
    screen4.geometry(popsize)
    incorrect = Label(screen4, text = "Password incorrect!")
    incorrect.pack()
    Button(screen4, text = "okay", command = screen4.destroy).pack()



def User_not_found():
    screen5 = Toplevel(screen2)
    screen5.title("ERROR!")
    screen5.geometry("300x100")
    not_found = Label(screen5, text = "User not found or password 
    incorrect!")
    not_found.pack()
    Button(screen5, text = "okay", command = screen5.destroy).pack()



def Login_success():
    Main_Page() 



def Main_Page():
    global screen3
    screen3 = Toplevel(screen2)
    screen3.geometry(screensize)
    screen3.title("Main Page")

    menubar = tk.Menu(screen3)
    Filemenu = tk.Menu(menubar, tearoff = 0)

    Filemenu.add_command(label = "Add Asset", command = Add_Asset)
    Filemenu.add_separator()

    Filemenu.add_command(label = "Dispose Asset", command = Dipose_Asset)
    Filemenu.add_separator()

    Filemenu.add_command(label = "Edit Asset", command = Edit_Asset)
    Filemenu.add_separator()


    Filemenu.add_command(label = "Exit", command = quit)
    Filemenu.add_separator()



    menubar.add_cascade(label = "File", menu = Filemenu)


    screen3.config(menu=menubar)

def Add_Asset():
    print("Asset Added")

def Dipose_Asset():
    print("Dispose Asset")  

def Edit_Asset():
    print("Asset Added")


def main_screen():
    global screen
    screen = Tk()
    screen.geometry(screensize)
    screen.title("Main Screen")
    Label(text = "Notes", bg = "grey", width = "300", height = "2", font = 
    ("Calibri", 12)).pack()
    Label(text = "").pack()
    Button(text = "Login", height = "2", width = "30", command = 
    Login).pack()
    Label(text = "").pack()
    Button(text = "Register", height = "2", width = "30" , command = 
    register).pack()

    screen.mainloop()

main_screen()

主要区别是“顶级”,我已将每个定义更改为具有自己的屏幕= Tk(),因此一旦每个页面输入数据并“注册”或“登录”,我就可以销毁它。

0 个答案:

没有答案