· 8 years ago · Jul 14, 2018, 06:14 PM
1#imports
2from tkinter import *
3from tkinter import messagebox as ms
4import sqlite3
5
6# make database and users (if not exists already) table at programme start up
7with sqlite3.connect('quit.db') as db:
8 c = db.cursor()
9
10c.execute('CREATE TABLE IF NOT EXISTS user (id text, password text, name text ,email text, mobile text, gender text);')
11db.commit()
12db.close()
13
14#main Class
15class main:
16 def __init__(self,master):
17 # Window
18 self.master = master
19 # Some Usefull variables
20 self.id = StringVar()
21 self.password = StringVar()
22 self.name = StringVar()
23 self.email = StringVar()
24 self.mobile = StringVar()
25 self.gender = StringVar()
26
27 self.n_id = StringVar()
28 self.n_password = StringVar()
29 self.n_name = StringVar()
30 self.n_email = StringVar()
31 self.n_mobile = StringVar()
32 self.n_gender = StringVar()
33 #Create Widgets
34 self.widgets()
35
36 #Login Function
37 def login(self):
38 #Establish Connection
39 with sqlite3.connect('quit.db') as db:
40 c = db.cursor()
41
42 #Find user If there is any take proper action
43 find_user = ('SELECT * FROM user WHERE id = ? and password = ? and name = ? and email = ? and mobile = ? and gender =?')
44 c.execute(find_user,[(self.id.get()),(self.password.get()),(self.name.get()),(self.email.get()),(self.mobile.get()),(self.gender.get())])
45 result = c.fetchall()
46 if result:
47 self.logf.pack_forget()
48 self.head['text'] = self.name.get() + '\n Loged In'
49 self.head['pady'] = 150
50 else:
51 ms.showerror("Oops!","Password doesn't match")
52
53 def new_user(self):
54 #Establish Connection
55 with sqlite3.connect('quit.db') as db:
56 c = db.cursor()
57
58 #Find Existing name if any take proper action
59 find_user = ('SELECT * FROM user WHERE id = ? and password = ?')
60 c.execute(find_user,[(self.password.get()),(self.password.get())])
61 if c.fetchall():
62 ms.showerror('Error!','id Taken Try a Diffrent One.')
63 else:
64 ms.showinfo('Success!','Account Created!')
65 self.log()
66 #Create New Account
67 insert = 'INSERT INTO user(id, password, name, email, mobile, gender) VALUES(?,?,?,?,?,?)'
68 c.execute(insert,[(self.n_id.get()),(self.n_password.get()),(self.n_name.get()),(self.n_email.get()),(self.n_mobile.get()),(self.n_gender.get())])
69 db.commit()
70
71 #Frame Packing Methods
72 def log(self):
73 self.id.set('')
74 self.password.set('')
75 #self.name.set('')
76 #self.email.set('')
77 #self.mobile.set('')
78 #self.gender.set('')
79 self.crf.pack_forget()
80 self.head['text'] = 'LOGIN'
81 self.logf.pack()
82 def cr(self):
83 self.n_id.set('')
84 self.n_password.set('')
85 #self.n_name.set('')
86 #self.n_email.set('')
87 #self.n_mobile.set('')
88 #self.n_gender.set('')
89 self.logf.pack_forget()
90 self.head['text'] = 'Create Account'
91 self.crf.pack()
92
93 #Draw Widgets
94 def widgets(self):
95 self.head = Label(self.master,text = 'LOGIN',font = ('',35),pady = 10)
96 self.head.pack()
97 self.logf = Frame(self.master,padx =10,pady = 10)
98 Label(self.logf,text = 'id: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
99 Entry(self.logf,textvariable = self.id,bd = 5,font = ('',15)).grid(row=0,column=1)
100 Label(self.logf,text = 'password: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
101 Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15)).grid(row=1,column=1)
102 '''Label(self.logf,text = 'name: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
103 Entry(self.logf,textvariable = self.name,bd = 5,font = ('',15),).grid(row=2,column=1)
104 Label(self.logf,text = 'email: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
105 Entry(self.logf,textvariable = self.email,bd = 5,font = ('',15),).grid(row=3,column=1)
106 Label(self.logf,text = 'mobile: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
107 Entry(self.logf,textvariable = self.mobile,bd = 5,font = ('',15),).grid(row=4,column=1)
108 Label(self.logf,text = 'gender: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
109 Entry(self.logf,textvariable = self.gender,bd = 5,font = ('',15),).grid(row=5,column=1)'''
110 Button(self.logf,text = ' Login ',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.login).grid()
111 Button(self.logf,text = ' Create Account ',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.cr).grid(row=2,column=1)
112 self.logf.pack()
113
114 self.crf = Frame(self.master,padx =10,pady = 10)
115 Label(self.crf,text = 'id: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
116 Entry(self.crf,textvariable = self.n_id,bd = 5,font = ('',15)).grid(row=0,column=1)
117 Label(self.crf,text = 'password: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
118 Entry(self.crf,textvariable = self.n_password,bd = 5,font = ('',15)).grid(row=1,column=1)
119 Label(self.crf,text = 'name: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
120 Entry(self.crf,textvariable = self.n_name,bd = 5,font = ('',15)).grid(row=2,column=1)
121 Label(self.crf,text = 'email: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
122 Entry(self.crf,textvariable = self.n_email,bd = 5,font = ('',15)).grid(row=3,column=1)
123 Label(self.crf,text = 'mobile: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
124 Entry(self.crf,textvariable = self.n_mobile,bd = 5,font = ('',15)).grid(row=4,column=1)
125 Label(self.crf,text = 'gender: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
126 Entry(self.crf,textvariable = self.n_gender,bd = 5,font = ('',15)).grid(row=5,column=1)
127 Button(self.crf,text = 'Create Account',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.new_user).grid()
128 Button(self.crf,text = 'Go to Login',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.log).grid(row=6,column=1)
129
130
131
132if __name__ == '__main__':
133 #Create Object
134 #and setup window
135 root = Tk()
136 root.title('Login Form')
137 #root.geometry('400x350+300+300')
138 main(root)
139 root.mainloop()