Guest User

Untitled

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