Guest User

Untitled

a guest
Mar 3rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. import MySQLdb
  2. from tkinter import *
  3. import tkinter.messagebox
  4. import hashlib
  5. class MainWindow:
  6.  
  7. def __init__(self,master):
  8.  
  9. self.master=master
  10. self.master.title("Online Quiz")
  11. self.master.config(bg = "powder blue")
  12. f = Frame(self.master, height =750,width = 600,bg="powder blue")
  13. f.propagate(0)
  14. f.pack()
  15.  
  16. self.regi = Button(f, text="Register",command = self.c_reg)
  17. self.login = Button(f, text="Login",command = self.c_login)
  18. self.regi.place(x=135,y=400)
  19. self.login.place(x=250,y=400)
  20. def c_reg(self):
  21. self.newWindow = Toplevel(self.master)
  22. self.app = Register(self.newWindow)
  23. def c_login(self):
  24. self.login = Toplevel(self.master)
  25. self.log = Login(self.login)
  26.  
  27.  
  28. class Register:
  29.  
  30. def __init__(self,master):
  31.  
  32. self.master=master
  33. self.master.title("Online Quiz")
  34. self.master.config(bg = "powder blue")
  35. f = Frame(self.master, height =750,width = 600,bg="powder blue")
  36. f.propagate(0)
  37. f.pack()
  38.  
  39. #-----------------------Labels----------------------
  40. self.name = Label(text = "First Name : ")
  41. self.lname = Label(text = "Last name : ")
  42. self.email = Label(text = "Email id : ")
  43. self.uname = Label(text = "Username :")
  44. self.pw = Label(text = "Enter password : ")
  45. self.cpw = Label(text = "Confirm password : ")
  46.  
  47. #----------------------Textboxes----------------------
  48. self.tname = Entry(f, width=30)
  49. self.tlname = Entry(f, width=30)
  50. self.temail = Entry(f, width=30)
  51. self.tuname = Entry(f, width=30)
  52. self.tpw = Entry(f, width=30,show="*")
  53. self.tcpw = Entry(f, width=30,show="*")
  54.  
  55. #----------------------Button-------------------------
  56. self.submit = Button(f, text="Submit",command = self.c_submit)
  57. self.cancel = Button(f, text="Cancel",command = self.c_cancel)
  58.  
  59.  
  60. #----------------------Position----------------------
  61. self.name.place(x=50 ,y=100)
  62. self.tname.place(x=200 ,y=100)
  63. self.lname.place(x=50 ,y=150)
  64. self.tlname.place(x=200 ,y=150)
  65. self.email.place(x=50 ,y=200)
  66. self.temail.place(x=200 ,y=200)
  67. self.uname.place(x=50 ,y=250)
  68. self.tuname.place(x=200 ,y=250)
  69. self.pw.place(x=50 ,y=300)
  70. self.tpw.place(x=200 ,y=300)
  71. self.cpw.place(x=50 ,y=350)
  72. self.tcpw.place(x=200 ,y=350)
  73. self.submit.place(x=135,y=400)
  74. self.cancel.place(x=250,y=400)
  75.  
  76. #----------Action after Submit button is pressed---------------
  77. def c_submit(self):
  78. conn = MySQLdb.connect(host='localhost', database='world', user='root', password='root')
  79. cursor = conn.cursor()
  80. name = self.tname.get()
  81. lname = self.tlname.get()
  82. email = self.temail.get()
  83. uname = self.tuname.get()
  84. pw = self.tpw.get()
  85. conpw = self.tcpw.get()
  86. p = hashlib.sha1((uname[:5]+pw).encode('utf-8')).hexdigest()
  87. try:
  88. s = "insert into reg2(name,lname,email,uname,p) values('%s','%s','%s','%s','%s')"
  89. arg = (name,lname,email,uname,p)
  90. cursor.execute(s % arg)
  91. conn.commit()
  92. except:
  93. conn.rollback()
  94. cursor.close()
  95. conn.close()
  96.  
  97. def c_cancel(self):
  98. pass
  99.  
  100. class Login:
  101.  
  102. def __init__(self, master):
  103. f = Frame(master, height=350, width=600)
  104. f.propagate(0)
  105. f.pack()
  106.  
  107. self.l1 = Label(text="Enter Username: ")
  108. self.e1 = Entry(f, width=25)
  109. self.l2 = Label(text="Enter Password: ")
  110. self.e2 = Entry(f, width=25, show="*")
  111. self.b1 = Button(f, text="Signup", command=self.clicked)
  112. self.b2 = Button(f, text="Cancel", command=self.cancelLogin)
  113. self.l1.place(x=50, y=100)
  114. self.e1.place(x=150, y=100)
  115. self.l2.place(x=50, y=150)
  116. self.e2.place(x=150, y=150)
  117. self.b1.place(x=170, y=200)
  118. self.b2.place(x=250, y=200)
  119.  
  120. def cancelLogin(self):
  121. exit(0)
  122.  
  123. #------------------------------------------signup button---------------------------------------------
  124. def clicked(self):
  125. conn = MySQLdb.connect(host='localhost', database='world', user='root', password='veer1811')
  126. cursor = conn.cursor()
  127. u = self.e1.get()
  128. pw = self.e2.get()
  129. #password hash
  130. p = hashlib.sha1((u[:5]+pw).encode('utf-8')).hexdigest()
  131. print(p)
  132. try:
  133. s = "insert into reg1(username, pw) values('%s', '%s')"
  134. arg = (u, p)
  135. cursor.execute(s % arg)
  136. conn.commit()
  137. except:
  138. conn.rollback()
  139. cursor.close()
  140. conn.close()
  141.  
  142. root = Tk()
  143. RegObj = MainWindow(root)
  144. root.mainloop()
Add Comment
Please, Sign In to add comment