-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquizstar.py
More file actions
257 lines (219 loc) · 6.47 KB
/
quizstar.py
File metadata and controls
257 lines (219 loc) · 6.47 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# the json module to work with json files
import json
import tkinter
from tkinter import *
import random
# questions = [
# "How many Keywords are there in C Programming language ?",
# "Which of the following functions takes A console Input in Python ?",
# "Which of the following is the capital of India ?",
# "Which of The Following is must to Execute a Python Code ?",
# "The Taj Mahal is located in ?",
# "The append Method adds value to the list at the ?",
# "Which of the following is not a costal city of india ?",
# "Which of The following is executed in browser(client side) ?",
# "Which of the following keyword is used to create a function in Python ?",
# "To Declare a Global variable in python we use the keyword ?",
# ]
# answers_choice = [
# ["23","32","33","43",],
# ["get()","input()","gets()","scan()",],
# ["Mumbai","Delhi","Chennai","Lucknow",],
# ["TURBO C","Py Interpreter","Notepad","IDE",],
# ["Patna","Delhi","Benaras","Agra",],
# ["custom location","end","center","beginning",],
# ["Bengluru","Kochin","Mumbai","vishakhapatnam",],
# ["perl","css","python","java",],
# ["function","void","fun","def",],
# ["all","var","let","global",],
# ]
# load questions and answer choices from json file instead of the file
with open('./data.json', encoding="utf8") as f:
data = json.load(f)
# convert the dictionary in lists of questions and answers_choice
questions = [v for v in data[0].values()]
answers_choice = [v for v in data[1].values()]
answers = [1,1,1,1,3,1,0,1,3,3]
user_answer = []
indexes = []
def gen():
global indexes
while(len(indexes) < 5):
x = random.randint(0,9)
if x in indexes:
continue
else:
indexes.append(x)
def showresult(score):
lblQuestion.destroy()
r1.destroy()
r2.destroy()
r3.destroy()
r4.destroy()
labelimage = Label(
root,
background = "#ffffff",
border = 0,
)
labelimage.pack(pady=(50,30))
labelresulttext = Label(
root,
font = ("Consolas",20),
background = "#ffffff",
)
labelresulttext.pack()
if score >= 20:
img = PhotoImage(file="great.png")
labelimage.configure(image=img)
labelimage.image = img
labelresulttext.configure(text="You Are Excellent !!")
elif (score >= 10 and score < 20):
img = PhotoImage(file="ok.png")
labelimage.configure(image=img)
labelimage.image = img
labelresulttext.configure(text="You Can Be Better !!")
else:
img = PhotoImage(file="bad.png")
labelimage.configure(image=img)
labelimage.image = img
labelresulttext.configure(text="You Should Work Hard !!")
def calc():
global indexes,user_answer,answers
x = 0
score = 0
for i in indexes:
if user_answer[x] == answers[i]:
score = score + 5
x += 1
print(score)
showresult(score)
ques = 1
def selected():
global radiovar,user_answer
global lblQuestion,r1,r2,r3,r4
global ques
x = radiovar.get()
user_answer.append(x)
radiovar.set(-1)
if ques < 5:
lblQuestion.config(text= questions[indexes[ques]])
r1['text'] = answers_choice[indexes[ques]][0]
r2['text'] = answers_choice[indexes[ques]][1]
r3['text'] = answers_choice[indexes[ques]][2]
r4['text'] = answers_choice[indexes[ques]][3]
ques += 1
else:
# print(indexes)
# print(user_answer)
# these two lines were just developement code
# we don't need them
calc()
def startquiz():
global lblQuestion,r1,r2,r3,r4
lblQuestion = Label(
root,
text = questions[indexes[0]],
font = ("Consolas", 16),
width = 500,
justify = "center",
wraplength = 400,
background = "#ffffff",
)
lblQuestion.pack(pady=(100,30))
global radiovar
radiovar = IntVar()
radiovar.set(-1)
r1 = Radiobutton(
root,
text = answers_choice[indexes[0]][0],
font = ("Times", 12),
value = 0,
variable = radiovar,
command = selected,
background = "#ffffff",
)
r1.pack(pady=5)
r2 = Radiobutton(
root,
text = answers_choice[indexes[0]][1],
font = ("Times", 12),
value = 1,
variable = radiovar,
command = selected,
background = "#ffffff",
)
r2.pack(pady=5)
r3 = Radiobutton(
root,
text = answers_choice[indexes[0]][2],
font = ("Times", 12),
value = 2,
variable = radiovar,
command = selected,
background = "#ffffff",
)
r3.pack(pady=5)
r4 = Radiobutton(
root,
text = answers_choice[indexes[0]][3],
font = ("Times", 12),
value = 3,
variable = radiovar,
command = selected,
background = "#ffffff",
)
r4.pack(pady=5)
def startIspressed():
labelimage.destroy()
labeltext.destroy()
lblInstruction.destroy()
lblRules.destroy()
btnStart.destroy()
gen()
startquiz()
root = tkinter.Tk()
root.title("Quizstar")
root.geometry("700x600")
root.config(background="#ffffff")
root.resizable(0,0)
img1 = PhotoImage(file="transparentGradHat.png")
labelimage = Label(
root,
image = img1,
background = "#ffffff",
)
labelimage.pack(pady=(40,0))
labeltext = Label(
root,
text = "Quizstar",
font = ("Comic sans MS",24,"bold"),
background = "#ffffff",
)
labeltext.pack(pady=(0,50))
img2 = PhotoImage(file="Frame.png")
btnStart = Button(
root,
image = img2,
relief = FLAT,
border = 0,
command = startIspressed,
)
btnStart.pack()
lblInstruction = Label(
root,
text = "Read The Rules And\nClick Start Once You Are ready",
background = "#ffffff",
font = ("Consolas",14),
justify = "center",
)
lblInstruction.pack(pady=(10,100))
lblRules = Label(
root,
text = "This quiz contains 10 questions\nYou will get 20 seconds to solve a question\nOnce you select a radio button that will be a final choice\nhence think before you select",
width = 100,
font = ("Times",14),
background = "#000000",
foreground = "#FACA2F",
)
lblRules.pack()
root.mainloop()