· 5 years ago · Jun 13, 2020, 03:44 PM
1from PIL import Image
2from PIL import ImageTk
3import tkinter as tk
4from tkinter import ttk
5from tkinter import messagebox
6import csv
7
8
9class Program(tk.Tk):
10
11 def __init__(self):
12 tk.Tk.__init__(self)
13 container = tk.Frame(self)
14 container.pack(side="top", fill="both", expand=True)
15 self.frames = {}
16 for F in (MainView, LoginForm, RegisterForm):
17 frame = F(container, self)
18 self.frames[F] = frame
19 frame.grid(row=0, column=0, sticky='NSEW')
20 self.show_frame(MainView)
21
22 def show_frame(self, page_name):
23 frame = self.frames[page_name]
24 frame.tkraise()
25
26
27class MainView(tk.LabelFrame):
28
29 def __init__(self, parent, controller):
30 self.controller = controller
31 tk.LabelFrame.__init__(self, parent, bg='blue', width=300, height=600)
32 self.make_widget()
33
34 def make_widget(self):
35 self.bgs=tk.Frame(self, width=300, height=650, bg='#416791')
36 self.bgs.pack(fill= 'both', expand=True)
37 background = '#416791'
38 width = 150
39 height = 150
40 img = Image.open("image/user2.png")
41 img = img.resize((width, height), Image.ANTIALIAS)
42 user_photo = ImageTk.PhotoImage(img)
43 self.imagine=tk.Label(self.bgs, width=150, height=150, image=user_photo, bg=background)
44 self.imagine.image = user_photo
45 self.imagine.place(x=70, y=40)
46 #============T I T L E========================================
47 self.title=tk.Label(self.bgs, text='LOGIN PAGE', font='Fixedsys 25', bg=background)
48 self.title.place(x=25, y=250)
49 #==================B U T T O N ====================================
50 self.login_button = tk.Button(self.bgs, text="LOGIN ", font='Arial 20 bold', activeforeground="red",
51 width=10, activebackground="beige", command=lambda: self.controller.show_frame(LoginForm))
52 self.login_button.bind('<Enter>', lambda e: self.login_button.config(bg='#0f003d', fg='white'))
53 self.login_button.bind('<Leave>', lambda e: self.login_button.config(bg='white', fg='black'))
54 self.login_button.place(x=70,y=400)
55 self.register_button = tk.Button(self.bgs, text='CREATE +', font='Arial 20 bold', activeforeground="red",
56 activebackground="beige", width=10, command=lambda: self.controller.show_frame(RegisterForm))
57 self.register_button.bind('<Enter>', lambda e: self.register_button.config(bg='#0f003d', fg='white'))
58 self.register_button.bind('<Leave>', lambda e: self.register_button.config(bg='white', fg='black'))
59 self.register_button.place(x=70, y=550)
60
61class LoginForm(tk.Frame):
62 def __init__(self, parent, controller):
63 self.controller = controller
64 tk.Frame.__init__(self, parent, background = '#416791')
65 self.make_widget()
66
67 def make_widget(self):
68 #==== B A C K G R O U N D===============================
69 self.bgL = tk.Canvas(self)
70 self.bgL.pack(fill='both', expand=True)
71 width = 300
72 height = 650
73 img = Image.open("image/-Login-Form.png")
74 img = img.resize((width, height), Image.ANTIALIAS)
75 login_bg = ImageTk.PhotoImage(img)
76 self.bgL.create_image(width/2, height/2, image=login_bg)
77 self.bgL.image=login_bg
78 self.bgL.create_rectangle(25, 230, 280, 550, width=3, outline= 'white')
79
80 width = 150
81 height = 150
82 img = Image.open("image/icons8-user-96.png")
83 img = img.resize((width, height), Image.ANTIALIAS)
84 img1 = ImageTk.PhotoImage(img)
85 self.bgL.create_image(155, 220, image = img1)
86 self.bgL.img = img1
87 #====== C R E A T E E N T R Y U S E R A N D P A S S W O R D ===============
88 l1 = tk.Label(self.bgL, text = 'U S E R I D**', font = 'Fixedsys 15')
89 l1.place(x =40, y =300)
90 l2 = tk.Label(self.bgL, text='P A S S W O R D * ', font = 'Fixedsys 15')
91 l2.place(x =40, y =400 )
92 username = tk.StringVar()
93 password = tk.StringVar()
94 e1=tk.Entry(self.bgL, width=20, textvariable=username, font = 'Arial 15') # entry user
95 e1.place(x=40, y=330)
96 e2=tk.Entry(self.bgL, width=20, textvariable=password, font ='Arial 15', show = '*') # entry password
97 e2.place(x= 40, y =430)
98 #=========C R E A T E B U T T O N F O R S H O W P A S S W O R D ===
99 def show_hide_psd():
100 if check_var.get():
101 e2.config(show="")
102 else:
103 e2.config(show="*")
104
105 check_var = tk.IntVar()
106 check_show_psw = tk.Checkbutton(self.bgL, variable=check_var, relief='flat', onvalue=1, offvalue=0,
107 borderwidth=0, highlightthickness=0, bg='blue', command=show_hide_psd )
108 check_show_psw.place(x=30, y=460)
109 self.bgL.create_text(120, 470, text='Show my password', font="Times 12 bold")
110#========= C R E A T E C O M A N D F O R F O R G O T=================================================
111 self.fp = self.bgL.create_text(210, 500, text= 'Forgot password ?', font = "Times 12 bold", tags = 'show')
112 def enterx(event):
113 self.bgL.itemconfig(self.fp, fill="red") # change color)
114 def leavex(event):
115 self.bgL.itemconfig(self.fp, fill="black") # change color
116 def commandForgot(event):
117 rootx= tk.Toplevel()
118 rootx.title('FORGOT PASSWORD')
119 rootx.geometry('300x200')
120 forgot_background=tk.Frame(rootx, width=450, height=500)
121 forgot_background.pack()
122 l1=tk.Label(forgot_background,text='Entry your username')
123 entryF=tk.Entry(forgot_background,width=20,font='Arial 15')
124 l2=tk.Label(forgot_background,text='Entry your email adress')
125 emailF=tk.Entry(forgot_background,width=20,font='Arial 15')
126 l1.pack()
127 entryF.pack()
128 l2.pack()
129 emailF.pack()
130 def sendPassword():
131 #entryF---is username for forgot
132 #emailF----is email for forgot
133 data = []
134 with open('user.csv','r') as fps:
135 red=csv.reader(fps)
136 for row in red:
137 data.append(row)
138 col0 = [x[0] for x in data] # username
139 col1 = [x[1] for x in data] # password
140 col2 = [x[2] for x in data] # email
141 #print(col0)
142 #print(col2)
143 #print([emailF.get()])
144 if emailF.get() in col2:
145 for y in range(0, len(col2)):
146 if col0[y] == entryF.get() and col2[y] == emailF.get():
147 print(col0[y])
148 print(col1[y])
149 print(col2[y])
150 messagebox.showinfo('Password recovery', 'Your password is: '+col1[y])
151 fps.close()
152 else:
153 messagebox.showinfo('ERROR', 'WRONG USER OR EMAIL')
154
155
156
157
158 b1=tk.Button(forgot_background,text='submit',command=sendPassword)
159 b1.pack()
160 rootx.mainloop()
161
162 self.bgL.tag_bind("show", "<Enter>", enterx)
163 self.bgL.tag_bind("show", "<Leave>", leavex)
164 self.bgL.tag_bind("show", "<Button-1>", commandForgot)
165
166 #==========I F Y O U D O N T H A V E A C C O U N T=======
167 self.signupx = self.bgL.create_text(200, 530, text=" Don't have account ?", font="Times 12 bold", tags='signUp')
168 def enterUPX(event):
169 self.bgL.itemconfig(self.signupx, fill="red") # change color)
170 def leaveUPX(event):
171 self.bgL.itemconfig(self.signupx, fill="black") # change color
172 self.bgL.tag_bind("signUp", "<Enter>", enterUPX)
173 self.bgL.tag_bind("signUp", "<Leave>", leaveUPX)
174 self.bgL.tag_bind("signUp", "<Button-1>", lambda e: self.controller.show_frame(RegisterForm))
175
176 #==== C R E A T E ===== S U B M I T =====L O G I N==================================
177 '''username = tk.StringVar()
178 password = tk.StringVar()'''
179 def checkUser():
180 if len(username.get())<6 or len(password.get())<6:
181 e1.delete(0,tk.END)
182 e2.delete(0,tk.END)
183 e1.configure(fg='red',font='Arial 10 bold',width=30)
184 e1.insert(0,'ERROR')
185 e1.bind('<Button-1>',lambda e:(e1.delete(0,tk.END),e1.configure(width=20,fg='black', textvariable=username, font = 'Arial 15')))
186 else:
187 login = False
188 while login == False:
189 # log in which uses a database to find multiple username and passw
190 data = []
191 with open('user.csv', 'r+') as f:
192 reader = csv.reader(f)
193 for row in reader:
194 data.append(row)
195 # loop through all the data in column 0 and 1 assigns it to a variable called col0/col1
196 col0 = [x[0] for x in data]
197 col1 = [x[1] for x in data]
198 print(col0)
199 if username.get() in col0:
200 for y in range(0, len(col0)):
201 if col0[y] == username.get() and col1[y] == password.get():
202 login = True
203 messagebox.showinfo('LOGIN','you are on')
204 e1.delete(0, tk.END)
205 e2.delete(0, tk.END)
206 else:
207 messagebox.showinfo('ERROR','WRONG USER OR PASSWORD')
208 e1.delete(0, tk.END)
209 e2.delete(0, tk.END)
210 break
211 #==== C R E A T E B U T T O N =============================================================
212 submitBtn = tk.Button(self.bgL, text='SIGN IN', width = 10, height = 2, font = 'Arial 10 bold',
213 command = checkUser )
214 submitBtn.pack(side = 'bottom',anchor='s', pady = 50)
215
216
217class RegisterForm(tk.Frame):
218 def __init__(self, parent, controller):
219 self.controller = controller
220 tk.Frame.__init__(self, parent,background = '#416791')
221 self.make_widget()
222
223 def make_widget(self):
224 # === B A C K G R O U N D ======P A G E======================
225 self.bgR = tk.Canvas(self)
226 self.bgR.pack(fill='both', expand=True)
227 width = 300
228 height = 650
229 img = Image.open("image/mainBackground.png")
230 img = img.resize((width, height), Image.ANTIALIAS)
231 register_bg = ImageTk.PhotoImage(img)
232 self.bgR.create_image(width / 2, height / 2, image=register_bg)
233 self.bgR.image = register_bg
234 # ======== U S E R =======I C O N ================================
235 width = 150
236 height = 150
237 img = Image.open("image/user-register.png")
238 img = img.resize((width, height), Image.ANTIALIAS)
239 img2 = ImageTk.PhotoImage(img)
240 self.bgR.create_image(155, 100, image=img2)
241 self.bgR.img = img2
242 #========C R E A T E =========E N T R Y ===================
243 emailR=tk.StringVar()
244 usernameR=tk.StringVar()
245 passwordR=tk.StringVar()
246 box_value=tk.StringVar()
247
248 self.bgR.create_text(100, 200, text=' Enter your email adress:', font="Times 12 bold", fill='white')
249 self.entry_email=tk.Entry(self.bgR,textvariable=emailR, font='Arial 15')
250 self.entry_email.place(x=20,y=220)
251 self.bgR.create_text(100, 265, text=' @', font="Arial 12 bold", fill='white')
252 self.chose_email=ttk.Combobox(self.bgR,textvariable=box_value, values=[
253 "gmail.com",
254 "yahoo.com",
255 "msn.com",
256 "mail.com"], state="readonly")
257 self.chose_email.place(x=115, y=255)
258 self.chose_email.current(1)
259 self.bgR.create_text(90, 300, text=' Enter your username:', font="Times 12 bold", fill='white')
260 self.entry_username = tk.Entry(self.bgR,textvariable=usernameR, font='Arial 15')
261 self.entry_username.place(x=20, y=320)
262 self.bgR.create_text(90, 380, text=' Enter your password:', font="Times 12 bold", fill='white')
263 self.entry_password = tk.Entry(self.bgR,textvariable=passwordR ,font='Arial 15', show='*')
264 #self.entry_password.focus()
265 self.entry_password.place(x=20, y=400)
266
267 #========= S H O W ==== M Y ===== P A S S W O R D ==========================
268 def show_psw():
269 if check_var.get():
270 self.entry_password.config(show="")
271 else:
272 self.entry_password.config(show="*")
273 check_var = tk.IntVar()
274 check_show_psw = tk.Checkbutton(self.bgR, variable=check_var, relief='flat', onvalue=1, offvalue=0,
275 borderwidth=0, highlightthickness=0, bg='#1a0447', command=show_psw)
276 check_show_psw.place(x=20, y=440)
277 self.bgR.create_text(110, 450, text='Show my password', font="Times 12 bold", fill='white')
278 #===== C R E A T E === R E G I S T E R ==== C S V ==== F I L E =================
279 '''emailR = tk.StringVar()
280 usernameR = tk.StringVar()
281 passwordR = tk.StringVar()
282 box_value=tk.StringVar()'''
283 def registerUser():
284 if emailR.get() == '' or usernameR.get() == '' or passwordR.get()=='':
285 messagebox.showinfo('ERROR','EMPTY ONE OF THE FIELD')
286 elif len(usernameR.get()) < 6 or len(passwordR.get()) < 6:
287 messagebox.showinfo('ERROR','YOUR USERNAME OR PASSWORD'+ '\n IS TO SHORT\n MINIM 7 CHARACTER')
288 else:
289 with open('User.csv', 'a', newline='') as fp, \
290 open('User.csv', 'r', newline='') as readx:
291 a = csv.writer(fp)
292 b = csv.reader(readx)
293
294 print(usernameR.get() + ' this is username')
295 print(passwordR.get() + ' this is password')
296 print(emailR.get() + '@' + box_value.get() + ' this is your email')
297 print()
298 lista = []
299 for x in b:
300 # print(x[0], x[1])
301 lista.append(x[0])
302 if usernameR.get() in lista:
303 messagebox.showinfo('ERROR','Your username '+ (usernameR.get()).upper()+'\n already exist! ' )
304 print(f'\nThe user {usernameR.get()} already exist!')
305 else:
306 a.writerow([usernameR.get(), passwordR.get(),emailR.get() + '@' + box_value.get()])
307 #self.entry_username.delete(0, tk.END)
308 #self.entry_password.delete(0, tk.END)
309 #self.entry_email.delete(0, tk.END)
310
311
312
313
314 # =====C R E A T E====== ===== B U T T O N =====================
315 submitBTN = tk.Button(self.bgR, text='SUBMIT', width=10, height=2, font='Arial 10 bold',
316 command=registerUser)
317 submitBTN.place(x=105, y=500)
318 self.bgR.create_text(148,560,text='OR',font='Fixedsys 20 bold',fill='red')
319 loginBtn=tk.Button(self.bgR, text='LOGIN', width=5, height=1, font='Arial 10 bold',
320 command=lambda: self.controller.show_frame(LoginForm))
321 loginBtn.place(x=125, y=580)
322
323if __name__ == '__main__':
324 root = Program()
325 root.geometry('300x650')
326 root.resizable(0,0)
327 root.title('Program')
328 root.mainloop()