I have made part of a quiz game and realize that if I continue down this route my code could get huge, is there a way to simplify my code or make it more efficient? The first couple of lines of MathsQuestions.txt are:
1.Work out the value of 3 × 8? a)12, b)14, c)16, d)18,
2.Work out the value of 4 × 2? a) 6, b) 8, c)12, d)14,
3.Work out the value of 8 × 2? a) 6, b)12, c)14, d)16,
import tkinter
import tkinter as tk
from tkinter.ttk import *
from tkinter import *
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(MainMenu)
def switch_frame(self, frame_class):
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
class MainMenu(tk.Frame):
def __init__(self, master):
global photo
global photo2
global photo3
global photo4
global photo5
global photo6
tk.Frame.__init__(self, master)
lbl = tk.Label(self, text="MainMenu", font=('Verdana', 40, "bold"))
lbl.grid(row=0, column=2)
#Maths Picture/Button
photo = PhotoImage(file = "MathsPicture.png")
photoimage = photo.subsample(3,3)
button = tk.Button(self, image = photo, command=lambda:[master.switch_frame(MathsQ1), MathsScoreUpdate()])
button.grid(row=1, column=1)
Mathsscore = 0
def MathsScoreUpdate(event=None):
global Mathsscore
Mathsscore += 5
x = open("MathsQuestions.txt", "r", errors = "ignore")
MathsQuestions = x.read()
MathsQ = MathsQuestions.split(",")
x.close()
#symbols
#r = row
#q = question
#a = answer
class MathsQ1(tk.Frame):
global Mathsscore
def __init__(self, master):
a = 2
r = 4
q = 0
tk.Frame.__init__(self, master)
self.pack(expand=True, anchor='n')
lbl = tk.Label(self, text=Mathsscore , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=1)
lbl = tk.Label(self, text="Score:" , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=0)
lbl = tk.Label(self, text="Q1/10", font=('Verdana', 30, "bold"))
lbl.grid(row=0, column=0)
lbl = tk.Label(self, text=MathsQ[q], font=('Verdana', 20, "bold"))
lbl.grid(row=2, column=3, columnspan=2)
btn = tk.Button(self, text=MathsQ[1] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda:[master.switch_frame(MathsQ2), MathsScoreUpdate()])
btn.grid(row=3, column=3)
for i in range(3):
btn = tk.Button(self, text=MathsQ[a] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda: master.switch_frame(MathsQ2))
btn.grid(row=r, column=3)
r = r + 1
a = a + 1
lbl = tk.Label(self, text="Maths Category", font=("Verdana", 25))
lbl.grid(row=0, column = 5, sticky="ne")
print(score)
print("Physics Question 1")
class MathsQ2(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack(expand=True, anchor='n')
a = 7
r = 4
q = 5
lbl = tk.Label(self, text=Mathsscore , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=1)
lbl = tk.Label(self, text="Score:" , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=0)
lbl = tk.Label(self, text="Q2/10", font=('Verdana', 30, "bold"))
lbl.grid(row=0, column=0)
lbl = tk.Label(self, text=MathsQ[q], font=('Verdana', 20, "bold"))
lbl.grid(row=2, column=3, columnspan=2)
btn = tk.Button(self, text=MathsQ[6] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda:[master.switch_frame(MathsQ3), MathsScoreUpdate()])
btn.grid(row=3, column=3)
for i in range(3):
btn = tk.Button(self, text=MathsQ[a] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda: master.switch_frame(MathsQ3))
btn.grid(row=r, column=3)
r = r + 1
a = a + 1
lbl = tk.Label(self, text="Maths Category", font=("Verdana", 25))
lbl.grid(row=0, column = 5, sticky="ne")
print(score)
class MathsQ3(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack(expand=True, anchor='n')
a = 12
r = 4
q = 10
lbl = tk.Label(self, text=Mathsscore , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=1)
lbl = tk.Label(self, text="Score:" , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=0)
lbl = tk.Label(self, text="Q2/10", font=('Verdana', 30, "bold"))
lbl.grid(row=0, column=0)
lbl = tk.Label(self, text=MathsQ[q], font=('Verdana', 20, "bold"))
lbl.grid(row=2, column=3, columnspan=2)
btn = tk.Button(self, text=MathsQ[11] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda:[master.switch_frame(MathsQ4), MathsScoreUpdate()])
btn.grid(row=3, column=3)
for i in range(3):
btn = tk.Button(self, text=MathsQ[a] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda: master.switch_frame(MathsQ4))
btn.grid(row=r, column=3)
r = r + 1
a = a + 1
lbl = tk.Label(self, text="Maths Category", font=("Verdana", 25))
lbl.grid(row=0, column = 5, sticky="ne")
print(score)
class MathsQ4(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack(expand=True, anchor='n')
a = 17
r = 4
q = 15
lbl = tk.Label(self, text=Mathsscore , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=1)
lbl = tk.Label(self, text="Score:" , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=0)
lbl = tk.Label(self, text="Q2/10", font=('Verdana', 30, "bold"))
lbl.grid(row=0, column=0)
lbl = tk.Label(self, text=MathsQ[q], font=('Verdana', 20, "bold"))
lbl.grid(row=2, column=3, columnspan=2)
btn = tk.Button(self, text=MathsQ[16] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda:[master.switch_frame(MathsQ5), MathsScoreUpdate()])
btn.grid(row=3, column=3)
for i in range(3):
btn = tk.Button(self, text=MathsQ[a] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda: master.switch_frame(MathsQ5))
btn.grid(row=r, column=3)
r = r + 1
a = a + 1
lbl = tk.Label(self, text="Maths Category", font=("Verdana", 25))
lbl.grid(row=0, column = 5, sticky="ne")
print(score)
class MathsQ5(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack(expand=True, anchor='n')
a = 22
r = 4
q = 20
lbl = tk.Label(self, text=Mathsscore , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=1)
lbl = tk.Label(self, text="Score:" , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=0)
lbl = tk.Label(self, text="Q2/10", font=('Verdana', 30, "bold"))
lbl.grid(row=0, column=0)
lbl = tk.Label(self, text=MathsQ[q], font=('Verdana', 20, "bold"))
lbl.grid(row=2, column=3, columnspan=2)
btn = tk.Button(self, text=MathsQ[21] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda:[master.switch_frame(MathsQ6), MathsScoreUpdate()])
btn.grid(row=3, column=3)
for i in range(3):
btn = tk.Button(self, text=MathsQ[a] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda: master.switch_frame(MathsQ6))
btn.grid(row=r, column=3)
r = r + 1
a = a + 1
lbl = tk.Label(self, text="Maths Category", font=("Verdana", 25))
lbl.grid(row=0, column = 5, sticky="ne")
print(score)
class MathsQ6(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack(expand=True, anchor='n')
a = 27
r = 4
q = 25
lbl = tk.Label(self, text=Mathsscore , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=1)
lbl = tk.Label(self, text="Score:" , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=0)
lbl = tk.Label(self, text="Q2/10", font=('Verdana', 30, "bold"))
lbl.grid(row=0, column=0)
lbl = tk.Label(self, text=MathsQ[q], font=('Verdana', 20, "bold"))
lbl.grid(row=2, column=3, columnspan=2)
btn = tk.Button(self, text=MathsQ[26] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda:[master.switch_frame(MathsQ7), MathsScoreUpdate()])
btn.grid(row=3, column=3)
for i in range(3):
btn = tk.Button(self, text=MathsQ[a] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda: master.switch_frame(MathsQ7))
btn.grid(row=r, column=3)
r = r + 1
a = a + 1
lbl = tk.Label(self, text="Maths Category", font=("Verdana", 25))
lbl.grid(row=0, column = 5, sticky="ne")
print(score)
class MathsQ7(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack(expand=True, anchor='n')
a = 32
r = 4
q = 30
lbl = tk.Label(self, text=Mathsscore , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=1)
lbl = tk.Label(self, text="Score:" , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=0)
lbl = tk.Label(self, text="Q2/10", font=('Verdana', 30, "bold"))
lbl.grid(row=0, column=0)
lbl = tk.Label(self, text=MathsQ[q], font=('Verdana', 20, "bold"))
lbl.grid(row=2, column=3, columnspan=2)
btn = tk.Button(self, text=MathsQ[31] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda:[master.switch_frame(MathsQ8), MathsScoreUpdate()])
btn.grid(row=3, column=3)
for i in range(3):
btn = tk.Button(self, text=MathsQ[a] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda: master.switch_frame(MathsQ8))
btn.grid(row=r, column=3)
r = r + 1
a = a + 1
lbl = tk.Label(self, text="Maths Category", font=("Verdana", 25))
lbl.grid(row=0, column = 5, sticky="ne")
print(score)
class MathsQ8(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack(expand=True, anchor='n')
a = 37
r = 4
q = 35
lbl = tk.Label(self, text=Mathsscore , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=1)
lbl = tk.Label(self, text="Score:" , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=0)
lbl = tk.Label(self, text="Q2/10", font=('Verdana', 30, "bold"))
lbl.grid(row=0, column=0)
lbl = tk.Label(self, text=MathsQ[q], font=('Verdana', 20, "bold"))
lbl.grid(row=2, column=3, columnspan=2)
btn = tk.Button(self, text=MathsQ[36] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda:[master.switch_frame(MathsQ9), MathsScoreUpdate()])
btn.grid(row=3, column=3)
for i in range(3):
btn = tk.Button(self, text=MathsQ[a] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda: master.switch_frame(MathsQ9))
btn.grid(row=r, column=3)
r = r + 1
a = a + 1
lbl = tk.Label(self, text="Maths Category", font=("Verdana", 25))
lbl.grid(row=0, column = 5, sticky="ne")
print(score)
class MathsQ9(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack(expand=True, anchor='n')
a = 42
r = 4
q = 40
lbl = tk.Label(self, text=Mathsscore , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=1)
lbl = tk.Label(self, text="Score:" , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=0)
lbl = tk.Label(self, text="Q2/10", font=('Verdana', 30, "bold"))
lbl.grid(row=0, column=0)
lbl = tk.Label(self, text=MathsQ[q], font=('Verdana', 20, "bold"))
lbl.grid(row=2, column=3, columnspan=2)
btn = tk.Button(self, text=MathsQ[41] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda:[master.switch_frame(MathsQ10), MathsScoreUpdate()])
btn.grid(row=3, column=3)
for i in range(3):
btn = tk.Button(self, text=MathsQ[a] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda: master.switch_frame(MathsQ10))
btn.grid(row=r, column=3)
r = r + 1
a = a + 1
lbl = tk.Label(self, text="Maths Category", font=("Verdana", 25))
lbl.grid(row=0, column = 5, sticky="ne")
print(score)
class MathsQ10(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack(expand=True, anchor='n')
a = 47
r = 4
q = 45
lbl = tk.Label(self, text=Mathsscore , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=1)
lbl = tk.Label(self, text="Score:" , font=("Verdana", 30, "bold"))
lbl.grid(row=1, column=0)
lbl = tk.Label(self, text="Q2/10", font=('Verdana', 30, "bold"))
lbl.grid(row=0, column=0)
lbl = tk.Label(self, text=MathsQ[q], font=('Verdana', 20, "bold"))
lbl.grid(row=2, column=3, columnspan=2)
btn = tk.Button(self, text=MathsQ[46] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda:[master.switch_frame(MathsEnding), MathsScoreUpdate()])
btn.grid(row=3, column=3)
for i in range(3):
btn = tk.Button(self, text=MathsQ[a] , font=("Verdana", 35, "bold"), height =1, width = 15, command=lambda: master.switch_frame(MathsEnding))
btn.grid(row=r, column=3)
r = r + 1
a = a + 1
lbl = tk.Label(self, text="Maths Category", font=("Verdana", 25))
lbl.grid(row=0, column = 5, sticky="ne")
print(score)
class MathsEnding(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack(expand=True, anchor='center')
lbl = tk.Label(self, text="Congrats on completing the maths quiz, you can see you score below.\n You can either go back to the main menu or try the quiz again" , font=("Verdana", 15, "bold"))
lbl.grid(row=0, column=0, columnspan=2)
btn = tk.Button(self, text="Go back to MainMenu" , font=("Verdana", 20, "bold"), height =1, width = 18, command=lambda:master.switch_frame(MainMenu))
btn.grid(row=1, column=0)
btn = tk.Button(self, text="Restart the Quiz" , font=("Verdana", 20, "bold"), height =1, width = 15, command=lambda:master.switch_frame(MathsQ1))
btn.grid(row=1, column=1)
lbl = tk.Label(self, text=["Score", Mathsscore] , font=("Verdana", 30, "bold"))
lbl.grid(row=2, column=0)