1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| # imports
from tkinter import *
from tkinter import messagebox as ms
import sqlite3
# Variable BDD
bdd = "base.db"
# Connection à la BDD
with sqlite3.connect(bdd) as db:
c = db.cursor()
# Création de la BDD si inexistante
c.execute("""CREATE TABLE IF NOT EXISTS "users" (
"id_user" INTEGER,
"user_name" TEXT,
"user_password" TEXT,
"user_email" TEXT,
"user_phone" TEXT,
"user_team" TEXT,
"user_type" TEXT,
PRIMARY KEY("id_user" AUTOINCREMENT)
)""")
db.commit()
db.close()
class main:
"""
Main Class
"""
def __init__(self, master):
# Window
self.master = master
# Variables
self.username = StringVar()
self.password = StringVar()
self.n_username = StringVar()
self.n_password = StringVar()
# Create Widgets
self.widgets()
# login function
def login(self):
# Establish Connection
with sqlite3.connect(bdd) as db:
c = db.cursor()
#Si vide
if self.username.get() == "" and self.password.get() == "":
ms.showerror('Error','Please fill the fields')
else:
# User exist
find_user_exist = ('SELECT * FROM users WHERE user_name = ?')
c.execute(find_user_exist, [(self.username.get())])
user_exist = c.fetchall()
if user_exist:
# Exist ?
find_user = ('SELECT * FROM users WHERE user_name = ? and user_password = ?')
c.execute(find_user, [(self.username.get()), (self.password.get())])
result = c.fetchall()
if result:
self.loginFrame.pack_forget()
self.head['text'] = self.username.get() + '\n Logged In'
self.head['pady'] = 150
#self.loggedInFrame.pack()
else:
ms.showerror('Error', 'Incorrect Password.')
else:
ms.showerror('Error', 'Username not found.')
def new_user(self):
# Establish Connection
with sqlite3.connect(bdd) as db:
c = db.cursor()
# Find Existing username if any take proper action
find_user = ('SELECT user_name FROM users WHERE user_name = ?')
c.execute(find_user, [(self.n_username.get())])
if c.fetchall():
ms.showerror('Error', 'Username already taken.')
else:
ms.showinfo('Success', 'Account Created')
self.log_packing()
# Create New Account
insert = 'INSERT INTO users(user_name,user_password) VALUES(?,?)'
c.execute(insert, [(self.n_username.get()), (self.n_password.get())])
db.commit()
def log_packing(self):
self.username.set('')
self.password.set('')
self.createUserFrame.pack_forget()
self.head['text'] = 'Login'
self.loginFrame.pack()
def create_user_packing(self):
self.n_username.set('')
self.n_password.set('')
self.loginFrame.pack_forget()
self.head['text'] = 'Create Account'
self.createUserFrame.pack()
# Draw Widgets
def widgets(self):
self.head = Label(self.master, text="\nLogin\n", font=(police, 12), bg=bgcolor,
fg=fgcolor, pady=0)
self.head.pack()
self.loginFrame = Frame(self.master, padx=10, pady=10, bg=bgcolor)
Label(self.loginFrame, text='Username: ', font=(police, 10), pady=5, padx=5, bg=bgcolor, fg=fgcolor).grid(sticky=W)
Entry(self.loginFrame, textvariable=self.username, bd=0, font=(police, 10), bg = entrycolor).grid(row=0, column=1)
Label(self.loginFrame, text='Password: ', font=(police, 10), pady=5, padx=5, bg=bgcolor, fg=fgcolor).grid(sticky=W)
Entry(self.loginFrame, textvariable=self.password, bd=0, font=(police, 10), bg = entrycolor).grid(row=1, column=1) # show='*'
Button(self.loginFrame, text=' Login ', bd=0, font=(police, 10), padx=5, pady=25, bg= buttoncolor, fg= fgcolor, command=self.login).grid()
Button(self.loginFrame, text=' Create Account ', bd=0, font=(police, 10), padx=5, pady=25, bg= buttoncolor, fg= fgcolor, command=self.create_user_packing).grid(row=2,column=1)
self.loginFrame.bind('<FocusIn>', self.func_entree)
self.loginFrame.pack()
self.createUserFrame = Frame(self.master, padx=10, pady=10, bg=bgcolor)
Label(self.createUserFrame, text='Username: ', font=(police, 10), pady=5, padx=5, bg=bgcolor, fg=fgcolor).grid(sticky=W)
Entry(self.createUserFrame, textvariable=self.n_username, bd=0, font=(police, 10), bg=entrycolor).grid(row=0, column=1)
Label(self.createUserFrame, text='Password: ', font=(police, 10), pady=5, padx=5, bg=bgcolor, fg=fgcolor).grid(sticky=W)
Entry(self.createUserFrame, textvariable=self.n_password, bd=0, font=(police, 10), show='*', bg=entrycolor).grid(row=1,column=1)
Button(self.createUserFrame, text=' Register ', bd=0, font=(police, 10), padx=5, pady=25, bg=buttoncolor, fg=fgcolor,command=self.new_user).grid()
Button(self.createUserFrame, text=' Go to Login ', bd=0, font=(police, 10), padx=5, pady=25, bg=buttoncolor,fg=fgcolor, command=self.log_packing).grid(row=2, column=1)
#self.loggedInFrame = Frame(self.master, padx=10, pady=10, bg=bgcolor)
#Label(self.createUserFrame, text='Logged In', font=(police, 10), pady=5, padx=5, bg=bgcolor, fg=fgcolor)
#Button(self.loggedInFrame, text='Disconnect', bd=0, font=(police, 10), padx=5, pady=25, bg=buttoncolor, fg=fgcolor)
def func_entree(event):
print("La touche Entrée a été pressée.")
if __name__ == '__main__':
# Variables
bgcolor = "#303030"
buttoncolor = "#303030"
entrycolor = "#CECECE"
fgcolor = "#E4E4E4"
police = "Sinkin"
# Setup Window
window = Tk()
window.title('Login Window')
window_height = 250
window_width = 300
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x_cordinate = int((screen_width / 2) - (window_width / 2))
y_cordinate = int((screen_height / 2) - (window_height / 2))
window.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
window.minsize(250, 300)
window.iconbitmap("icon/icon.ico")
window.config(background=bgcolor)
logo = PhotoImage(file="img/logo_xxs.png")
canvas_logo = Canvas(window, bg=bgcolor, width=75, height=75, highlightthickness=0, relief="ridge")
canvas_logo.create_image(32, 32, image=logo)
canvas_logo.pack(pady=(10,0))
main(window)
window.mainloop() |
Partager