6
\$\begingroup\$

GUII 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)

\$\endgroup\$
12
  • \$\begingroup\$ Why do you have all of the "..." at the end? If it's to circumvent restrictions on having more code than non-code, there's a reason for that restriction. This type of action goes against site policy. \$\endgroup\$ – Bryan Oakley Oct 31 '20 at 17:55
  • \$\begingroup\$ Welcome to code review! I have edited your title so that it only states what your code does. Anything else belongs in the body. Find more in How to Ask \$\endgroup\$ – Parekh Oct 31 '20 at 17:57
  • \$\begingroup\$ Please provide us with an example of the first few lines of MathsQuestions.txt \$\endgroup\$ – Bryan Oakley Oct 31 '20 at 18:05
  • \$\begingroup\$ @BryanOakley I believe this question is off-topic, since this is a "part" of his project which he hasn't finished yet \$\endgroup\$ – Parekh Oct 31 '20 at 18:07
  • 1
    \$\begingroup\$ @AryanParekh: I don't think it's off topic. While the OP says it's "part of" a program, I think they mean it's a complete program, except that they want to add more questions. \$\endgroup\$ – Bryan Oakley Oct 31 '20 at 18:16
1
\$\begingroup\$

Sure there are lots of ways to improve it! Let's begin with the small stuff and work our way up:

Undefined "score"

Either define it and then print it everywhere if you want, or just remove it. I removed it below.

General style

Have a look at PEP 8 for a good start on Python code style. Use vertical spacing, separate comment text from the hash, no spaces in keyword arguments, name functions in snake case, that kind of stuff.

Imports

Discard the ones you don't use, and generally try to avoid star imports, because that can lead to name clashes and confusion. Let's just use a single import:

import tkinter as tk

The big payoff: don't repeat yourself

Keep it DRY. When you see similar code twice, think about ways to write it only once. If you see it three times, you should definitely try to do something about that. Ten times? Boy oh boy. Fight that CTRL+C/CTRL+V instinct.

By parameterising the frame creation as follows, we can drastically reduce the complexity of creating multiple question frames.

class Question(tk.Frame):
    def __init__(self, master, r, q, a, q_number, q_text, switch_to):
        super().__init__(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=f"Q{q_number}/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=q_text,
            font=("Verdana", 35, "bold"),
            height=1,
            width=15,
            command=lambda: [self.master.switch_frame(switch_to), maths_score_update()]
        )
        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: self.master.switch_frame(switch_to)
            )
            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")

Now creating frames is as easy as 1-2-3.

class MathsQ1(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=0, a=2, q_number=1, q_text=MathsQ[1], switch_to=MathsQ2
        )
        print("Physics Question 1")


class MathsQ2(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=5, a=7, q_number=2, q_text=MathsQ[6], switch_to=MathsQ3
        )


class MathsQ3(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=10, a=12, q_number=2, q_text=MathsQ[11], switch_to=MathsQ4
        )


class MathsQ4(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=15, a=17, q_number=2, q_text=MathsQ[16], switch_to=MathsQ5
        )


class MathsQ5(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=20, a=22, q_number=2, q_text=MathsQ[21], switch_to=MathsQ6
        )


class MathsQ6(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=25, a=27, q_number=2, q_text=MathsQ[26], switch_to=MathsQ7
        )


class MathsQ7(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=30, a=32, q_number=2, q_text=MathsQ[31], switch_to=MathsQ8
        )


class MathsQ8(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=35, a=37, q_number=2, q_text=MathsQ[36], switch_to=MathsQ9
        )


class MathsQ9(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=40, a=42, q_number=2, q_text=MathsQ[41], switch_to=MathsQ10
        )


class MathsQ10(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=45, a=47, q_number=2, q_text=MathsQ[46], switch_to=MathsEnding
        )

This should pretty much achieve the exact same functionality, but as we can see the question number stays the same across most questions. You should probably have it running from 1 to 10. You should also be able to abstract it some more yourself, maybe by extracting the font styles or creating and placing labels, but this is a start. Indeed I think the switch_to argument is not needed if you just pass the correct question number and use it as an index. And looks like the indices to MathsQ and q and a have patterns (q = 5i, a = 5i + 2, qt = 5i + 1), so they could be just calculated instead of inputting them by hand.

Here's the final code dump just for completeness, but try changing the code yourself! Because the question and picture files were not provided, I can't promise this will run 100 %, but it should be ok at least with some debugging.

import tkinter as tk


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):
        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 = tk.PhotoImage(file="MathsPicture.png")
        photoimage = photo.subsample(3, 3)
        button = tk.Button(
            self,
            image=photo,
            command=lambda: [master.switch_frame(MathsQ1), maths_score_update()]
        )
        button.grid(row=1, column=1)


Mathsscore = 0


def maths_score_update(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 Question(tk.Frame):
    def __init__(self, master, r, q, a, q_number, q_text, switch_to):
        super().__init__(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=f"Q{q_number}/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=q_text,
            font=("Verdana", 35, "bold"),
            height=1,
            width=15,
            command=lambda: [self.master.switch_frame(switch_to), maths_score_update()]
        )
        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: self.master.switch_frame(switch_to)
            )
            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")


class MathsQ1(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=0, a=2, q_number=1, q_text=MathsQ[1], switch_to=MathsQ2
        )
        print("Physics Question 1")


class MathsQ2(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=5, a=7, q_number=2, q_text=MathsQ[6], switch_to=MathsQ3
        )


class MathsQ3(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=10, a=12, q_number=2, q_text=MathsQ[11], switch_to=MathsQ4
        )


class MathsQ4(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=15, a=17, q_number=2, q_text=MathsQ[16], switch_to=MathsQ5
        )


class MathsQ5(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=20, a=22, q_number=2, q_text=MathsQ[21], switch_to=MathsQ6
        )


class MathsQ6(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=25, a=27, q_number=2, q_text=MathsQ[26], switch_to=MathsQ7
        )


class MathsQ7(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=30, a=32, q_number=2, q_text=MathsQ[31], switch_to=MathsQ8
        )


class MathsQ8(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=35, a=37, q_number=2, q_text=MathsQ[36], switch_to=MathsQ9
        )


class MathsQ9(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=40, a=42, q_number=2, q_text=MathsQ[41], switch_to=MathsQ10
        )


class MathsQ10(Question):
    def __init__(self, master):
        super().__init__(
            master, r=4, q=45, a=47, q_number=2, q_text=MathsQ[46], switch_to=MathsEnding
        )


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."
                "\nYou 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)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.