Guest User

Untitled

a guest
Mar 23rd, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. import random
  2. import MySQLdb
  3. from tkinter import messagebox
  4. from tkinter import *
  5.  
  6.  
  7. conn = MySQLdb.connect(host='localhost', database='world', user='root', password='veer1811')
  8. cursor = conn.cursor()
  9.  
  10. global questions
  11. questions = []
  12. global options
  13. options = []
  14. global answers
  15. answers = []
  16. answerstemp =[]
  17. s1=set()
  18.  
  19. while len(s1)<10:
  20. strQ=""
  21. strA=""
  22. id = random.randint(1, 30)
  23. s1.add(id)
  24. print(s1)
  25. while len(s1)>0:
  26. s = "select qstn from questions where QID=%d"
  27. id = s1.pop()
  28. arg = (id)
  29. cursor.execute(s % arg)
  30. strQ = strQ.join(list(cursor.fetchone()))
  31. questions.append(strQ)
  32.  
  33. s = "select opA,opB,opC,opD from questions where QID=%d"
  34. arg = (id)
  35. cursor.execute(s % arg)
  36. options.append(list(cursor.fetchone()))
  37.  
  38. s = "select ans from questions where QID=%d"
  39. arg = (id)
  40. cursor.execute(s % arg)
  41. l = list(cursor.fetchone())
  42. answerstemp.append(l)
  43. print(answerstemp)
  44. print(questions)
  45. print(options)
  46. mydict={}
  47. for i in range(10):
  48. mydict[questions[i]]=options[i]
  49. for key,val in mydict.items():
  50. print(key,"---->",val)
  51. print("\n")
  52. for i in range(len(answerstemp)):
  53. answers.append(answerstemp[i][0])
  54.  
  55. print(answers)
  56.  
  57. cursor.close()
  58. conn.close()
  59. l1={}
  60. for i in range(10):
  61. l1[i]=0
  62. print(l1)
  63. class Quiz:
  64. def __init__(self, master):
  65. global mReg
  66. mReg = master
  67. self.master = master
  68. self.master.geometry("1350x750+0+0")
  69. self.master.title("Online Quiz - Registration")
  70. self.master.config(bg="azure")
  71. global f1
  72. f = Frame(self.master, height=1080, width=1920, bg="azure", relief="ridge", bd=20)
  73. f.propagate(0)
  74. f.pack()
  75. self.qno = 0
  76. self.score1 = 0 # self.correct
  77. self.ques = self.create_q(f, self.qno)
  78. self.opts = self.create_options(f)
  79. self.display_q(self.qno)
  80. self.Back = Button(f, text="<-Back",command = self.back).place(x=265,y=225)
  81. self.Next = Button(f, text="Next->",command=self.next ).place(x=315,y=225)
  82. self.submit=Button(f, text="Submit",fg="white",command=self.Submit,bg="blue").place(x=290,y=275)
  83.  
  84.  
  85. def create_q(self, master, qno):
  86. qLabel = Label(master, text=questions[qno],bg='azure')
  87. qLabel.place(x=40,y=70)
  88. return qLabel
  89.  
  90. def create_options(self,master):
  91. b_val = 0
  92. b = []
  93. ht=75
  94. self.opt_selected = IntVar()
  95. while b_val<4:
  96. btn = Radiobutton(master, text="", variable=self.opt_selected, value=b_val+1,bg = 'azure')
  97. b.append(btn)
  98. ht=ht+25
  99. btn.place(x=30,y=ht)
  100. b_val = b_val + 1
  101. return b
  102.  
  103. def display_q(self, qno):
  104. b_val = 0
  105. print(qno)
  106. self.ques['text'] = str(qno+1)+". "+questions[qno]
  107. for op in options[qno]:
  108. self.opts[b_val]['text'] = op
  109. b_val = b_val + 1
  110. print(l1)
  111.  
  112. def next(self):
  113. self.qno += 1
  114.  
  115. if self.qno >= len(questions):
  116. self.qno -=1
  117. messagebox.showwarning("Warning", "You are at the end.Press Submit to proceed")
  118. else:
  119. l1[self.qno-1]=self.opt_selected.get()
  120. self.opt_selected.set(l1[(self.qno)])
  121. self.display_q(self.qno)
  122.  
  123. def back(self):
  124. l1[self.qno]=self.opt_selected.get()
  125. self.qno -= 1
  126. if self.qno <0:
  127. self.qno +=1
  128. messagebox.showerror("Error", "You are already in the start!!!")
  129. else:
  130. self.display_q(self.qno)
  131. c=l1[self.qno]
  132. print(c,self.qno)
  133. self.opt_selected.set(c)
  134. def Submit(self):
  135. l1[self.qno]=self.opt_selected.get()
  136. x=0
  137. for i in range(10):
  138. if(l1[i]==0):
  139. x+=1
  140. if(x>0 and x!=1):
  141. y = True
  142. y=messagebox.askyesno("Warning","You have not attempted "+str(x)+" questions, Are you sure you want to submit?, You won't be able to make changes again")
  143. elif(x==1):
  144. y=messagebox.askyesno("Warning","You have not attempted "+str(x)+" question, Are you sure you want to submit?, You won't be able to make changes again")
  145. if(y==True or x==0):
  146. s = 0
  147. for i in range(10):
  148. print(answerstemp[i][0],l1[i])
  149. if(l1[i]==answerstemp[i][0]):
  150. s = s+1
  151. print(s)
  152.  
  153.  
  154. #Label(master,text="Congrats!!!..Your score is : "+str(s)+"/10",bg='azure').place(x=200,y=300)
  155. root = Tk()
  156. root.resizable(0,0)
  157. RegObj = Quiz(root)
  158. root.mainloop()
Add Comment
Please, Sign In to add comment