· 7 years ago · Dec 14, 2018, 09:26 AM
1from tkinter import *
2from tkinter import messagebox as ms
3import sqlite3
4
5# make database and users (if not exists already) table at programme start up
6with sqlite3.connect('quit.db') as db:
7 c = db.cursor()
8
9c.execute("""CREATE TABLE IF NOT EXISTS tblUsers
10 (username TEXT NOT NULL,
11 password TEXT NOT NULL,
12 primary key (username));""")
13
14db.commit()
15db.close()
16
17#main Class
18class main:
19 def __init__(self,master):
20 # Window
21 self.master = master
22 # Some Usefull variables
23 self.username = StringVar()
24 self.password = StringVar()
25 self.n_username = StringVar()
26 self.n_password = StringVar()
27 #Create Widgets
28 self.widgets()
29
30 #Login Function
31 def login(self):
32 #Establish Connection
33 with sqlite3.connect('quit.db') as db:
34 c = db.cursor()
35
36 #Find user If there is any take proper action
37 find_user = ('SELECT * FROM tblUsers WHERE username = ? and password = ?')
38 c.execute(find_user,[(self.username.get()),(self.password.get())])
39 result = c.fetchall()
40 if result:
41 self.logf.pack_forget()
42 self.head['text'] = self.username.get() + '\n Loged In'
43 self.head['pady'] = 150
44 else:
45 ms.showerror('Oops!','The user cannot be found.','Have you typed in the correct details?"')
46
47 def new_user(self):
48 #Establish Connection
49 with sqlite3.connect('quit.db') as db:
50 c = db.cursor()
51
52 #Find Existing username if any take proper action
53 find_user = ('SELECT * FROM tblUsers WHERE username = ?')
54 c.execute(find_user,[(self.username.get())])
55 if c.fetchall():
56 ms.showerror('Error!','Username Taken Try a Diffrent One.')
57 else:
58 ms.showinfo('Success!','Account Created!')
59 self.log()
60 #Create New Account
61 insert = 'INSERT INTO tblUsers(username,password) VALUES(?,?)'
62 c.execute(insert,[(self.n_username.get()),(self.n_password.get())])
63 db.commit()
64
65 #Frame Packing Methords
66 def log(self):
67 self.username.set('')
68 self.password.set('')
69 self.crf.pack_forget()
70 self.head['text'] = 'LOGIN'
71 self.logf.pack()
72 def cr(self):
73 self.n_username.set('')
74 self.n_password.set('')
75 self.logf.pack_forget()
76 self.head['text'] = 'Create Account'
77 self.crf.pack()
78
79 #Draw Widgets
80 def widgets(self):
81 self.head = Label(self.master,text = 'LOGIN',font = ('',35),pady = 10)
82 self.head.pack()
83 self.logf = Frame(self.master,padx =10,pady = 10)
84 Label(self.logf,text = 'Username: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
85 Entry(self.logf,textvariable = self.username,bd = 5,font = ('',15)).grid(row=0,column=1)
86 Label(self.logf,text = 'Password: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
87 Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1)
88 Button(self.logf,text = ' Login ',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.login).grid()
89 Button(self.logf,text = ' Create Account ',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.cr).grid(row=2,column=1)
90 self.logf.pack()
91
92 self.crf = Frame(self.master,padx =10,pady = 10)
93 Label(self.crf,text = 'Username: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
94 Entry(self.crf,textvariable = self.n_username,bd = 5,font = ('',15)).grid(row=0,column=1)
95 Label(self.crf,text = 'Password: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
96 Entry(self.crf,textvariable = self.n_password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1)
97 Button(self.crf,text = 'Create Account',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.new_user).grid()
98 Button(self.crf,text = 'Go to Login',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.log).grid(row=2,column=1)
99
100
101
102#create window and application object
103root = Tk()
104#root.title("Login Form")
105main(root)
106root.mainloop()