-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
192 lines (158 loc) · 6.78 KB
/
main.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
"""
This is the functional unit of the project where all the logic for cheaking for correct answer
"""
class Question:
"""
This class is intended for making, displaying and removing the questions seen on the quize page.
"""
def __init__(self,frame,hight,width,quest):
self.options=list(quest["dis"])
self.options.append(quest["ans"])
shuffle(self.options)
self.ans=quest["ans"]
self.QuestionLabel=Label(frame,text=quest["ques"],height=2,font=("Times New Roman",35,'italic'),bg='#ebac4d',wraplength=1000, justify="center")
self.choice=StringVar()
self.option1=Radiobutton(frame, text=self.options[0],font=("Times New Roman",35),bg='#ebac4d',wraplength=500, variable=self.choice, value=self.options[0])
self.option2=Radiobutton(frame, text=self.options[1],font=("Times New Roman",35),bg='#ebac4d',wraplength=500, variable=self.choice, value=self.options[1])
self.option3=Radiobutton(frame, text=self.options[2],font=("Times New Roman",35),bg='#ebac4d',wraplength=500, variable=self.choice, value=self.options[2])
self.option4=Radiobutton(frame, text=self.options[3],font=("Times New Roman",35),bg='#ebac4d',wraplength=500, variable=self.choice, value=self.options[3])
self.playerans=None
def show(self):
self.QuestionLabel.place(relx=0.5,rely=0.15,anchor=N)
self.option1.place(relx=0.25,rely=0.30,anchor=N)
self.option2.place(relx=0.75,rely=0.30,anchor=N)
self.option3.place(relx=0.25,rely=0.75,anchor=S)
self.option4.place(relx=0.75,rely=0.75,anchor=S)
def Destroy(self):
self.playerans=self.choice.get()
#self.playeranswer=self.choice
self.QuestionLabel.place_forget()
self.option1.place_forget()
self.option2.place_forget()
self.option3.place_forget()
self.option4.place_forget()
global start_frame,question_fram,time_fram,ques_list_fram
def main():
"""
This is functional unit of the program. In here we build the main quiz page.
"""
global cur
cur=0
def timer():#This is the functional timer for the project.
global Time
Time=60
while Time!=0:
time=Label(time_fram,text="%d:%d"%(Time//60,Time%60),width=300,height=3,font=("Times New Roman",80),bg='#ebac4d')
time.pack()
Time-=1
sleep(1)
time.destroy()
result()
def next():
global cur
try:
if 0<=cur<9:
# print("1Next=",cur)
Quest_list[cur].Destroy()
cur+=1
Quest_list[cur].show()
#print("2Next=",cur)
else:
print(cur)
cur=9
Quest_list[cur].show()
messagebox.showinfo(title="Quize Project", message="You Have reached the end of the quiz")
except:
cur=0
Quest_list[cur].show()
def prev():
global cur
try:
if 0<cur<=9:
#print("1Prev=",cur)
Quest_list[cur].Destroy()
cur=cur-1
Quest_list[cur].show()
#print("2Prev=",cur)
else:
messagebox.showinfo(title="Quize Project", message="You are at the begining of the quiz")
except:
cur=0
Quest_list[cur].show()
def result():
ques_list_fram.destroy()
question_fram.destroy()
time_fram.destroy()
count=0
for i in range(10):
if Quest_list[i].playerans==Quest_list[i].ans:
count+=1
messagebox.showinfo(title="Quize Project", message="You got %d out 10 coreect"%(count))
home()
#DO NOT CHANGE THIS ORDER(The arrangement of the frames will mess up)
global question_fram,time_fram,ques_list_fram
question_fram=Frame(root,height=scrh,width=scrw*0.66666,bg="#ebac4d")
time_fram=Frame(root,height=scrh*0.25,width=scrw*(1-0.66666),bg="#ebac4d")
ques_list_fram=Frame(root,height=scrh*0.75,width=scrw*(1-0.66666),bg="#ebac4d")
question_fram.pack(side=LEFT)
time_fram.pack(side=TOP)
ques_list_fram.pack(side=BOTTOM)
next_but= Button(ques_list_fram,text="Next",width=10,font=("Courier",25,"bold"),activebackground="#065e47",bg='#94ffe2', command=lambda:next())
prev_but= Button(ques_list_fram,text="Prevous",width=10,font=("Courier",25,"bold"),activebackground="#065e47",bg='#94ffe2',command=lambda:prev())
submit_but= Button(ques_list_fram,text="Submit",width=10,font=("Courier",25,"bold"),activebackground="#065e47",bg='#94ffe2',command=lambda:result())
next_but.place(relx=0.5,rely=0.3,anchor=N)
prev_but.place(relx=0.5,rely=0.5,anchor=N)
submit_but.place(relx=0.5,rely=0.7,anchor=N)
#Creating a list of questions
global Quest_list
Quest_list=list()
for i in range(10):
Quest_list.append(Question(question_fram,scrh-scrh*0.20,scrw*0.66666,question[i]))
Quest_list[cur].show()
#Timer Starts
t1=threading.Thread(target=timer)
t1.start()
def home_to_main():
start_frame.destroy()
main()
#Importing Libraires
from tkinter import *
from time import sleep
import threading
import json
from random import randint,shuffle
from tkinter import messagebox
def home():#defining The home page
global start_frame
start_frame=Frame(root,height=scrh,width=scrw,bg="#ebac4d")
start_frame.pack()
my_label=Label(start_frame, text="Quiz Project", font=("Times New Roman", 70, "bold"),bg="#ebac4d")
my_label.place(relx =0.5, rely = 0.1, anchor=N)
exitbut=Button(start_frame,text="Exit", width =10, font=("Times New Roman",25,"bold"),activebackground="#247371",bg="#4debe8",command=lambda:root.destroy())
exitbut.place(relx=0.5,rely=0.7,anchor=S)
starbut=Button(start_frame,text="Start", width =10, font=("Times New Roman",25,"bold"),activebackground="#247371",bg="#4debe8",command=lambda:home_to_main())
starbut.place(relx=0.5,rely=0.5,anchor=S)
#Preprocessing
root=Tk()
root.title("QUIZOOO")
scrh=root.winfo_screenheight() # Getting screen height
scrw=root.winfo_screenwidth() # Getting Screen width
root.geometry("%dx%d"%(scrw,scrh))
"""
In this part we are choosing the questions
"""
global question
question=[]
with open("question.json") as quest:
quest=json.load(quest)
for i in range(10):
#This part is done so no same question appear twice
while(True):
k=randint(0,len(quest)-1)
if quest[k] not in question:
question.append(quest[i])
break
shuffle(question)
home()
#main()
root.mainloop()