-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuizGame.py
152 lines (107 loc) · 2.65 KB
/
QuizGame.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
from tkinter import *
from tkinter import messagebox as mb
import json
class Quiz:
def __init__(self):
self.qno=0
self.disp_title()
self.disp_ques()
self.opt_sel=IntVar()
self.opts=self.radio_buttons()
self.disp_opt()
self.buttons()
self.total_size=len(question)
self.correct=0
def disp_res(self):
wrong_count = self.total_size - self.correct
correct = f"Correct: {self.correct}"
wrong = f"Wrong: {wrong_count}"
score = int(self.correct / self.total_size * 100)
result = f"Score: {score}%"
mb.showinfo("Result", f"{result}\n{correct}\n{wrong}")
def check_ans(self, qno):
if self.opt_sel.get() == answer[qno]:
return True
def next_btn(self):
if self.check_ans(self.qno):
self.correct += 1
self.qno += 1
if self.qno==self.total_size:
self.disp_res()
ws.destroy()
else:
self.disp_ques()
self.disp_opt()
def buttons(self):
next_button = Button(
ws,
text="Next",
command=self.next_btn,
width=10,
bg="#F2780C",
fg="white",
font=("ariel",16,"bold")
)
next_button.place(x=350,y=380)
quit_button = Button(
ws,
text="Quit",
command=ws.destroy,
width=5,
bg="black",
fg="white",
font=("ariel",16," bold")
)
quit_button.place(x=700,y=50)
def disp_opt(self):
val=0
self.opt_sel.set(0)
for option in options[self.qno]:
self.opts[val]['text']=option
val+=1
def disp_ques(self):
qno = Label(
ws,
text=question[self.qno],
width=60,
font=( 'ariel' ,16, 'bold' ),
anchor= 'w',
wraplength=700,
justify='center'
)
qno.place(x=70, y=100)
def disp_title(self):
title = Label(
ws,
text="QUIZ GAME",
width=50,
bg="#F2A30F",
fg="white",
font=("ariel", 20, "bold")
)
title.place(x=0, y=2)
def radio_buttons(self):
q_list = []
y_pos = 150
while len(q_list) < 4:
radio_btn = Radiobutton(
ws,
text=" ",
variable=self.opt_sel,
value = len(q_list)+1,
font = ("ariel",14)
)
q_list.append(radio_btn)
radio_btn.place(x = 100, y = y_pos)
y_pos += 40
return q_list
ws = Tk()
ws.geometry("800x450")
ws.title("Quiz Game")
with open('data.json') as f:
data = json.load(f)
question = (data['question'])
options = (data['options'])
answer = (data[ 'answer'])
quiz = Quiz()
ws.mainloop()