In: Computer Science
Phyton Login program
To start the program each day the manager must login. The main
(manager) window will appear with a login, create password, and
cancel button. A password must exist for the login button to be
enabled. The password is created in a separate window and must be 9
characters or more, and it must have at least on digit, uppercase
and lowercase letter. The program will continue to show error
messages and prompt for a password until a valid password is
created. The valid password will be stored in a file for validation
on login.
When a valid password has been created or on subsequent program
runs, the main window “Create Account” button for the manager will
be disabled. When the login button is clicked, the main window will
change (morph) to add a login entry box.
Create the “Account Creation / Login” interface and the
functions using a class for the GUI in a separate module (file) and
begin the design for user name and password file handling. The
window is launched when the “Create Account” button is clicked. Use
the <enter> key to obtain the user entry.
Design and develop the functionality for changing the window or
creating a new one to obtain a user name and password including
input validation. Create an error dialog that echoes the password,
and create the password validation function which must test for
length (9 characters or more), an uppercase and lowercase letter,
and a digit. The window must contain instructions and operating
“Cancel” and “Create Account” buttons.
When the “Ok” button in the “Password Accepted” dialog is
clicked the Account Creation dialog should be destroyed and the
main window will now have the “Create Account” button disabled.
Store the user name and password for future checking. The user
should login next.
Hint: The call to the login function may not be able to be handled
directly through the “command=” option of the button. A call to a
function within the main GUI (outside the main loop) may need to be
called which then calls the login function in the separate
module.
Add the code to the login function to accept the password when the <Enter> key is pressed by binding the entry widget to a function call that tests for a match with the stored password by calling a “verify” function. The entry widget should accept input left aligned in the text box. If the entered password and stored password don’t match, alert the user and the text box should be cleared like the account creation example- entry.delete(0, END). Login operations can be done on the main window as shown here or in a separate window. Consider how the user and manager would like to interface with your program.
from tkinter import *
import tkinter.messagebox as tkMessageBox
import sqlite3
root = Tk()
root.title("account creaation")
width = 840
height = 680
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)
//variable
USERNAME = StringVar()
PASSWORD = StringVar()
FIRSTNAME = StringVar()
LASTNAME = StringVar()
//db conn
def Database():
global conn, cursor
conn = sqlite3.connect("db_member.db")
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT, firstname TEXT, lastname TEXT)")
//account creation
def LoginForm():
global LoginFrame, lbl_result1
LoginFrame = Frame(root)
LoginFrame.pack(side=TOP, pady=80)
lbl_username = Label(LoginFrame, text="Username:", font=('arial', 25), bd=18)
lbl_username.grid(row=1)
lbl_password = Label(LoginFrame, text="Password:", font=('arial', 25), bd=18)
lbl_password.grid(row=2)
lbl_result1 = Label(LoginFrame, text="", font=('arial', 18))
lbl_result1.grid(row=3, columnspan=2)
username = Entry(LoginFrame, font=('arial', 20), textvariable=USERNAME, width=15)
username.grid(row=1, column=1)
password = Entry(LoginFrame, font=('arial', 20), textvariable=PASSWORD, width=15, show="*")
password.grid(row=2, column=1)
btn_login = Button(LoginFrame, text="Login", font=('arial', 18), width=35, command=Login)
btn_login.grid(row=4, columnspan=2, pady=20)
lbl_register = Label(LoginFrame, text="Register", fg="Blue", font=('arial', 12))
lbl_register.grid(row=0, sticky=W)
lbl_register.bind('<Button-1>', ToggleToRegister)
def RegisterForm():
global RegisterFrame, lbl_result2
RegisterFrame = Frame(root)
RegisterFrame.pack(side=TOP, pady=40)
lbl_username = Label(RegisterFrame, text="Username:", font=('arial', 18), bd=18)
lbl_username.grid(row=1)
lbl_password = Label(RegisterFrame, text="Password:", font=('arial', 18), bd=18)
lbl_password.grid(row=2)
lbl_firstname = Label(RegisterFrame, text="Firstname:", font=('arial', 18), bd=18)
lbl_firstname.grid(row=3)
lbl_lastname = Label(RegisterFrame, text="Lastname:", font=('arial', 18), bd=18)
lbl_lastname.grid(row=4)
lbl_result2 = Label(RegisterFrame, text="", font=('arial', 18))
lbl_result2.grid(row=5, columnspan=2)
username = Entry(RegisterFrame, font=('arial', 20), textvariable=USERNAME, width=15)
username.grid(row=1, column=1)
password = Entry(RegisterFrame, font=('arial', 20), textvariable=PASSWORD, width=15, show="*")
password.grid(row=2, column=1)
firstname = Entry(RegisterFrame, font=('arial', 20), textvariable=FIRSTNAME, width=15)
firstname.grid(row=3, column=1)
lastname = Entry(RegisterFrame, font=('arial', 20), textvariable=LASTNAME, width=15)
lastname.grid(row=4, column=1)
btn_login = Button(RegisterFrame, text="Register", font=('arial', 18), width=35, command=Register)
btn_login.grid(row=6, columnspan=2, pady=20)
lbl_login = Label(RegisterFrame, text="Login", fg="Blue", font=('arial', 12))
lbl_login.grid(row=0, sticky=W)
lbl_login.bind('<Button-1>', ToggleToLogin)
#========================================MENUBAR WIDGETS==================================
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=Exit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
// functions
def Exit():
result = tkMessageBox.askquestion('System', 'Are you sure you want to exit?', icon="warning")
if result == 'yes':
root.destroy()
exit()
def ToggleToLogin(event=None):
RegisterFrame.destroy()
LoginForm()
def ToggleToRegister(event=None):
LoginFrame.destroy()
RegisterForm()
def Register():
Database()
if USERNAME.get == "" or PASSWORD.get() == "" or FIRSTNAME.get() == "" or LASTNAME.get == "":
lbl_result2.config(text="Please complete the required field!", fg="orange")
else:
cursor.execute("SELECT * FROM `member` WHERE `username` = ?", (USERNAME.get(),))
if cursor.fetchone() is not None:
lbl_result2.config(text="Username is already taken", fg="red")
else:
cursor.execute("INSERT INTO `member` (username, password, firstname, lastname) VALUES(?, ?, ?, ?)", (str(USERNAME.get()), str(PASSWORD.get()), str(FIRSTNAME.get()), str(LASTNAME.get())))
conn.commit()
USERNAME.set("")
PASSWORD.set("")
FIRSTNAME.set("")
LASTNAME.set("")
lbl_result2.config(text="Successfully Created!", fg="black")
cursor.close()
conn.close()
def Login():
Database()
if USERNAME.get == "" or PASSWORD.get() == "":
lbl_result1.config(text="Please complete the required field!", fg="orange")
else:
cursor.execute("SELECT * FROM `member` WHERE `username` = ? and `password` = ?", (USERNAME.get(), PASSWORD.get()))
if cursor.fetchone() is not None:
lbl_result1.config(text="You Successfully Login", fg="blue")
else:
lbl_result1.config(text="Invalid Username or password", fg="red")
LoginForm()