Advertisement
Guest User

Untitled

a guest
Aug 8th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.05 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import messagebox
  3. import ast
  4. import mysql.connector
  5. from mysql.connector import Error
  6. from PIL import Image, ImageTk
  7.  
  8. #@@@@@@@@@@@@@@
  9. #username - 1
  10. #password - 1
  11. #@@@@@@@@@@@@@@
  12.  
  13. root = Tk()
  14. root.title('Login')
  15. root.geometry('925x500+300+200')
  16. root.configure(bg="#fff")
  17. root.resizable(False,False)
  18.  
  19. name = Label(root, text='LEXEME.IO', fg="black", bg='white', font=('Microsoft Yahei UI Light', 16, 'bold'))
  20. name.place(x=410, y=40)
  21.  
  22. trial_no = 0
  23.  
  24. def connect_to_db(): #database details
  25. try:
  26. connection = mysql.connector.connect(
  27. host='localhost',
  28. user='root',
  29. password='password',
  30. database='userregistration'
  31. )
  32. return connection
  33. except Error as e:
  34. messagebox.showerror("Database Error", f"Error connecting to MySQL: {e}")
  35. return None
  36.  
  37. def trial(): #max number of trials
  38. global trial_no
  39. trial_no += 1
  40. print("Trial # is", trial_no)
  41. if trial_no==3:
  42. messagebox.showwarning("Warning","You have tried too many times") #if user fails sign in 3 times
  43. root.destroy() #closes the program
  44. def signin(): #sign in
  45.  
  46. user_input=user.get()
  47. password_input=code.get()
  48.  
  49. if user_input == "" or user_input == "UserID" or password_input == "" or password_input == "Password":
  50. messagebox.showerror("Entry error", "Retry username or password!") #if no entry then return error
  51. return
  52. try:
  53. mydb= connect_to_db()
  54. if mydb is None:
  55. return
  56. mycursor=mydb.cursor()
  57.  
  58. command = "SELECT * FROM users WHERE username=%s AND password=%s" #opens the users table
  59. mycursor.execute(command, (user_input, password_input))
  60. myresult = mycursor.fetchone()
  61. print(myresult)
  62.  
  63.  
  64. if myresult is None: #if it cannot match the username and password in the table
  65. messagebox.showinfo("Invalid", "Invalid Username or Password!")
  66. trial()
  67. else:
  68. messagebox.showinfo("Login", "Successfully Logged in")
  69.  
  70.  
  71. #opens landing page
  72. def homepg():
  73. homepagewindow = Toplevel()
  74. homepagewindow.title("Homepage")
  75. homepagewindow.geometry('925x500+300+200')
  76. homepagewindow.configure(bg='#fff')
  77. homepagewindow.resizable(False, False)
  78.  
  79. homepagewindow.update_idletasks()
  80.  
  81. w= Label(homepagewindow, text="Hello World",fg="black", bg='white', font=('Microsoft Yahei UI Light', 23, 'bold'))
  82. w.place(x=410, y=40)
  83.  
  84. min_w = 50
  85. max_w = 200
  86. cur_width = min_w #increasing width of the frame
  87. expanded = False
  88.  
  89. frame_height = homepagewindow.winfo_height()
  90. frame = Frame(homepagewindow, bg='black', width=50, height=homepagewindow.winfo.height())
  91. frame.grid(row=0,column=0)
  92.  
  93. def expand():
  94. nonlocal cur_width, expanded
  95. cur_width += 10 #increase width by 10
  96. rep = homepagewindow.after(5, expand) #repeats expansion every 5ms
  97. frame.config(width=cur_width)
  98. if cur_width >= max_w:
  99. expanded = True #fully expanded
  100. homepagewindow.after_cancel(rep) #stops the expansion
  101. fill()
  102.  
  103. def contract():
  104. nonlocal cur_width, expanded
  105. cur_width -= 10
  106. rep = homepagewindow.after(5, contract)
  107. frame.config(width=cur_width)
  108. if cur_width <= min_w:
  109. expanded = False
  110. homepagewindow.after_cancel(rep)
  111. fill()
  112.  
  113. def fill():
  114. if expanded:
  115. #Show a text and remove the icon
  116. home_b.config(text='Home', image='', font=(0,21))
  117. profile_b.config(text='Profile', image='', font=(0,21))
  118. savedfiles_b.config(text='Saved Files', image='', font=(0,21))
  119. questions_b.config(text='Saved Question', image='', font=(0,21))
  120. else:
  121. # Bring the icon back
  122. home_b.config(image=home, font=(0,21))
  123. profile_b.config(image=profile, font=(0,21))
  124. savedfiles_b.config(image=savedfiles, font=(0,21))
  125. questions_b.config(image=questions, font=(0,21))
  126.  
  127. try:
  128. home = ImageTk.PhotoImage(Image.open('Images/home.png').resize((40,40,),Image.ANTIALIAS))
  129. profile = ImageTk.PhotoImage(Image.open('Images/profile.png').resize((40,40,),Image.ANTIALIAS))
  130. savedfiles = ImageTk.PhotoImage(Image.open('Images/savedfiles.png').resize((40,40,),Image.ANTIALIAS))
  131. questions = ImageTk.PhotoImage(Image.open('Images/questions.png').resize((40,40,),Image.ANTIALIAS))
  132. except Exception as e:
  133. print(f"Error loading images: {e}")
  134. return
  135.  
  136. #Make the buttons with the icons to be shown
  137. home_b = Button(frame, image=home, bg='white', relief='flat')
  138. profile_b = Button(frame, image=profile, bg='white', relief='flat')
  139. savedfiles_b = Button(frame, image=savedfiles, bg='white', relief='flat')
  140. questions_b = Button(frame, image=questions, bg='white', relief='flat')
  141.  
  142. #place them on the frame
  143. home_b.grid(row=0,column=0,pady=10)
  144. profile_b.grid(row=1,column=0,pady=50)
  145. savedfiles_b.grid(row=2,column=0, pady=70)
  146. questions_b.grid(row=3, column=0, pady=90)
  147.  
  148. #Bind them to the frame,for when expanding or contracting
  149. frame.bind('<Enter>', lambda e: expand())
  150. frame.bind('<Leave>', lambda e: contract())
  151.  
  152. #So that it does not depend on the widgets inside the frame
  153. frame.grid_propagate(False)
  154.  
  155. homepg()
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. except Error as e:
  163. messagebox.showerror("Error", f"Error: {e}")
  164. finally:
  165. if mydb.is_connected():
  166. mycursor.close()
  167. mydb.close()
  168.  
  169. ##################@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ start of sign up window
  170. def signup_command(): #sign up window
  171. signupwindow=Toplevel(root)
  172. signupwindow.title("Sign Up")
  173. signupwindow.geometry('925x500+300+200')
  174. signupwindow.configure(bg='#fff')
  175. signupwindow.resizable(False,False)
  176.  
  177.  
  178. frame=Frame(signupwindow,width=350, height=390, bg='#fff')
  179. frame.place(x=285,y=50)
  180.  
  181. #title
  182. heading=Label(frame,text='Sign up', fg="black", bg='white', font=('Microsoft Yahei UI Light', 23, 'bold'))
  183. heading.place(x=100, y=5)
  184.  
  185.  
  186. def signup():
  187. user_input=user.get()
  188. password_input=code.get()
  189. confirm_password=confirm_code.get()
  190. if password_input == confirm_password:
  191. try:
  192. connection = connect_to_db()
  193. if connection is not None:
  194. cursor = connection.cursor()
  195.  
  196. # Check if username already exists
  197. cursor.execute("SELECT username FROM users WHERE username=%s", (user_input,))
  198. result = cursor.fetchone()
  199. if result:
  200. messagebox.showerror('Error', 'Username already exists')
  201. else:
  202. # Insert new user
  203. cursor.execute("INSERT INTO users (username, password) VALUES (%s, %s)", (user_input, password_input))
  204. connection.commit()
  205. messagebox.showinfo('Signup', 'Successfully signed up')
  206.  
  207. cursor.close()
  208. connection.close()
  209. except Error as e:
  210. messagebox.showerror('Error', f"Error: {e}")
  211. else:
  212. messagebox.showerror('Invalid', 'Both passwords should match')
  213.  
  214.  
  215.  
  216. def sign(): #when you click on 'Already on Lexeme? Sign in' link
  217. signupwindow.destroy()
  218.  
  219.  
  220. ####-------------------------------------------------------------
  221. def on_enter(e): # username in sign up
  222. user.delete(0, 'end')
  223. def on_leave(e):
  224. if user.get()=='':
  225. user.insert(0, 'Username')
  226.  
  227. user = Entry(frame,width=25, fg='black', border=0, bg='white', font=('Microsoft Yahei UI Light',11))
  228. user.place(x=30, y=80)
  229. user.insert(0, 'Username')
  230. user.bind("<FocusIn>", on_enter)
  231. user.bind("<FocusOut>", on_leave)
  232.  
  233. Frame(frame,width=295, height=2, bg='black').place(x=25, y=107)
  234. ###-------------------------------------------------------------
  235. def on_enter(e): # passoword in sign up
  236. code.delete(0, 'end')
  237. def on_leave(e):
  238. if code.get()=='':
  239. code.insert(0, 'Password')
  240.  
  241. code = Entry(frame,width=25, fg='black', border=0, bg='white', font=('Microsoft Yahei UI Light',11))
  242. code.place(x=30, y=150)
  243. code.insert(0, 'Password')
  244. code.bind("<FocusIn>", on_enter)
  245. code.bind("<FocusOut>", on_leave)
  246.  
  247. Frame(frame,width=295, height=2, bg='black').place(x=25, y=177)
  248.  
  249. ###-------------------------------------------------------------
  250. def on_enter(e): #confirm password in sign up
  251. confirm_code.delete(0, 'end')
  252. def on_leave(e):
  253. if confirm_code.get()=='':
  254. confirm_code.insert(0, 'Confirm Password')
  255.  
  256. confirm_code = Entry(frame,width=25, fg='black', border=0, bg='white', font=('Microsoft Yahei UI Light',11))
  257. confirm_code.place(x=30, y=220)
  258. confirm_code.insert(0, 'Confirm Password')
  259. confirm_code.bind("<FocusIn>", on_enter)
  260. confirm_code.bind("<FocusOut>", on_leave)
  261.  
  262. Frame(frame,width=295, height=2, bg='black').place(x=25, y=247)
  263. ###-------------------------------------------------------------
  264. Button(frame, width=39, pady=7, text='Sign Up', bg='black', fg='white', border=0, command=signup).place(x=32, y=280)
  265. label=Label(frame,text='Already on Lexeme?', fg='black', bg='white', font=('Microsoft Yahei UI Light', 9))
  266. label.place(x=90, y=330)
  267.  
  268. signin=Button(frame,width=6, text='Sign In', border=0, bg='white', cursor='hand2', fg='black', command=sign)
  269. signin.place(x=205,y=331)
  270.  
  271. signupwindow.mainloop()
  272. ##################@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ end of sign up window
  273.  
  274.  
  275. frame = Frame(root, width=350, height=350, bg="White")
  276. frame.place(x=300,y=70)
  277.  
  278. heading=Label(frame, text='Sign in', fg='#010101', bg='white', font=('Microsoft YaHei UI Light', 14, 'bold'))
  279. heading.place(x=25, y=30)
  280.  
  281. #########----------------------------------------------------------------------
  282.  
  283. def on_enter(e):
  284. user.delete(0, 'end')
  285. def on_leave(e):
  286. name=user.get()
  287. if name=='':
  288. user.insert(0,'UserID')
  289.  
  290. user = Entry(frame, width=25,fg='black',border=0,bg="white",font=('Microsoft YaHei UI Light', 11))
  291. user.place(x=30,y=80)
  292. user.insert(0, 'Username')
  293. user.bind('<FocusIn>', on_enter)
  294. user.bind('<FocusOut>', on_leave)
  295.  
  296.  
  297. Frame(frame,width=295, height=2, bg='black').place(x=25,y=107)
  298.  
  299. #########----------------------------------------------------------------------
  300. def on_enter(e):
  301. code.delete(0, 'end')
  302. def on_leave(e):
  303. name=code.get()
  304. if name=='':
  305. code.insert(0,'Password')
  306.  
  307. code = Entry(frame, width=25,fg='black',border=0,bg="white",font=('Microsoft YaHei UI Light', 11))
  308. code.place(x=30,y=150)
  309. code.insert(0, 'Password')
  310. code.bind('<FocusIn>', on_enter)
  311. code.bind('<FocusOut>', on_leave)
  312.  
  313. Frame(frame,width=295, height=2, bg='black').place(x=25,y=177)
  314.  
  315. button_mode=True
  316.  
  317. def hide():
  318. global button_mode
  319.  
  320. if button_mode:
  321. eyeButton.config(image=closeye, activebackground="white")
  322. code.config(show="*")
  323. button_mode=False
  324. else:
  325. eyeButton.config(image=openeye, activebackground="white")
  326. code.config(show="")
  327. button_mode=True
  328.  
  329. openeye=PhotoImage(file="Program_vr1/Images/openeye.png")
  330. closeye=PhotoImage(file="Program_vr1/Images/closeye.png")
  331. eyeButton=Button(frame,image=openeye, bg="#375174", bd="0", command=hide)
  332. eyeButton.place(x=580, y=410)
  333.  
  334.  
  335.  
  336. ##############################################################
  337.  
  338. Button(frame, width=39, pady=7, text='Sign in', bg='black', fg='white', border=1, command=signin).place(x=35,y=204)
  339.  
  340. label=Label(frame,text="Don't have an account?",fg='black', bg='white', font=('Microsoft YaHei UI Light',9))
  341. label.place(x=75,y=270)
  342.  
  343. sign_up= Button(frame, width=6, text='Sign up', border=0, bg='white', cursor='hand2', fg='black', command=signup_command)
  344. sign_up.place(x=210,y=271)
  345.  
  346.  
  347.  
  348. root.mainloop()
  349.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement