-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdate_mcq.py
236 lines (173 loc) · 6.24 KB
/
Update_mcq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from io import BytesIO
import requests as r
# import required classes from tkinter
from tkinter import IntVar,Button, Label, Radiobutton, Tk, Entry
from PIL import Image, ImageTk
# and import messagebox as mb from tkinter
from tkinter import messagebox as mb
#import json to use json file for data
import json
class Quiz:
def __init__(self):
self.name_label=self.add_label("Name",50,50)
self.age_label=self.add_label("Age",50,100)
self.name = self.add_textbox(100,50,placeholder='name')
self.age = self.add_textbox(100,100,placeholder='age')
self.buttons(place_next=False)
self.entr = self.entry_button()
def add_label(self, display_text,x,y):
label= Label(gui, text=display_text, width=20,
font=( 'ariel' ,12, 'bold' ), anchor= 'w' )
label.place(x=x,y=y)
return label
def start(self):
self.name.destroy()
self.age.destroy()
self.name_label.destroy()
self.age_label.destroy()
self.entr.destroy()
self.q_no=0
self.img = None
self.display_image()
self.display_title()
self.display_question()
self.back_button()
self.opt_selected=IntVar()
self.opts=self.radio_buttons()
self.display_options()
self.next,self.quit = self.buttons()
self.data_size=len(question)
self.correct=0
def show_msg(self,msg):
mb.showinfo(msg)
def display_result(self):
result = f"Thank you for answering"
mb.showinfo("Response collected", f"{result}")
def check_ans(self, q_no):
user_choice[q_no] = self.opt_selected.get()
if self.opt_selected.get() == answer[q_no]:
return True
def next_btn(self):
if self.q_no == len(question)-2:
self.next['text']='Submit'
if self.check_ans(self.q_no):
self.correct += 1
result_ans[self.q_no]=1
else:
result_ans[self.q_no]=0
self.q_no += 1
if self.q_no==self.data_size:
self.display_result()
gui.destroy()
else:
self.display_question()
self.display_image()
self.display_options()
def add_textbox(self,x,y,placeholder="text"):
textbox = Entry(text=placeholder)
textbox.place(x=x,y=y)
return textbox
def buttons(self,place_next=True):
next_button = None
if place_next:
next_button = Button(gui, text='Next',command=self.next_btn,
width=10,bg="black",fg="white",font=("ariel",16,"bold"))
next_button.place(x=350,y=580)
quit_button = Button(gui, text="Quit", command=gui.destroy,
width=5,bg="black", fg="white",font=("ariel",16," bold"))
quit_button.place(x=700,y=50)
return next_button, quit_button
def back_button(self):
back_btn = Button(gui, text='Back',command=self.go_back,
width=10,bg="black",fg="white",font=("ariel",16,"bold"))
back_btn.place(x=150,y=580)
def go_back(self):
if self.q_no==0:
return
self.q_no-=1
self.display_question()
self.display_image()
self.display_options()
def entry_button(self):
entry_button = Button(gui, text="Entry",command=self.evaluate,
width=10,bg="black",fg="white",font=("ariel",16,"bold"))
entry_button.place(x=200,y=200)
return entry_button
def evaluate(self):
n= self.name.get()
a= self.age.get()
try:
a= int(a)
except ValueError:
self.show_msg('input age is not a number')
gui.destroy()
return
if n.strip() == '':
self.show_msg('please enter valid name')
if a<13 or a>100:
self.show_msg('your age is not suitable for this quiz')
gui.destroy()
else:
self.start()
def display_options(self):
val=0
self.opt_selected.set(0)
for option in options[self.q_no]:
self.opts[val]['text']=option
val+=1
for count in range(1,5):
if user_choice[self.q_no] == count:
self.opt_selected.set(count)
def display_question(self):
q_no = Label(gui, text=question[self.q_no], width=100,
font=( 'ariel' ,16, 'bold' ), anchor= 'w' )
q_no.place(x=70, y=100)
def display_image(self):
url = imgs[self.q_no]
u = r.get(url)
raw_data = u._content
u.close()
im = Image.open(BytesIO(raw_data))
size = (200,200)
im = im.resize(size)
image = ImageTk.PhotoImage(im)
self.img = image
img = Label(image=image,height=200,width=200)
img.place(x=100,y=150)
def display_title(self):
# The title to be shown
title = Label(gui, text="Help us understand you better",
width=50, bg="black",fg="white", font=("ariel", 20, "bold"))
# place of the title
title.place(x=0, y=2)
def radio_buttons(self):
q_list = []
# position of the first option
y_pos = 350
# adding the options to the list
while len(q_list) < 2:
radio_btn = Radiobutton(gui,text=" ",variable=self.opt_selected,
value = len(q_list)+1,font = ("ariel",14))
# adding the button to the list
q_list.append(radio_btn)
# placing the button
radio_btn.place(x = 100, y = y_pos)
# incrementing the y-axis position by 40
y_pos += 40
# return the radio buttons
return q_list
gui = Tk()
gui.geometry("800x700")
gui.title("Survey")
# get the data from the json file
with open('questions.json') as f:
data = json.load(f)
# set the question, options,imgs links and answer
question = (data['question'])
options = (data['options'])
answer = (data[ 'answer'])
imgs = (data['imgs'])
result_ans={}
user_choice = {item:0 for item in range(len(question))}
quiz = Quiz()
gui.mainloop()