|
| 1 | +from tkinter import * |
| 2 | +from random import randint,choice |
| 3 | + |
| 4 | +root = Tk() |
| 5 | +root.geometry("600x500") |
| 6 | +root.title("Maths Quiz") |
| 7 | + |
| 8 | +question = StringVar() |
| 9 | +answer = StringVar() |
| 10 | +givenAnswer = StringVar() |
| 11 | +score = IntVar() |
| 12 | +questionNumber = IntVar() |
| 13 | + |
| 14 | +def generateQuestion(): |
| 15 | + |
| 16 | + global questionLabel |
| 17 | + |
| 18 | + questionNumber.set(questionNumber.get() + 1) |
| 19 | + |
| 20 | + number1 = randint(1 , 10) |
| 21 | + number2 = randint(1 , 10) |
| 22 | + |
| 23 | + operator = choice(['+' , '-' , '*' , '/']) |
| 24 | + |
| 25 | + question.set(str(number1) + operator + str(number2)) |
| 26 | + answer.set(eval(question.get())) |
| 27 | + |
| 28 | + if questionLabel: |
| 29 | + questionLabel.destroy() |
| 30 | + |
| 31 | + questionLabel = Label(root , text=f"Question : {question.get()}" , font=('arial' , 20)) |
| 32 | + questionLabel.grid(row=2 , column=0) |
| 33 | + |
| 34 | +def checkAnswer(): |
| 35 | + |
| 36 | + global scoreLabel |
| 37 | + |
| 38 | + if questionNumber.get() > 10 : |
| 39 | + return |
| 40 | + |
| 41 | + global resultLabel |
| 42 | + |
| 43 | + if resultLabel: |
| 44 | + resultLabel.destroy() |
| 45 | + |
| 46 | + if str(answer.get()) == givenAnswer.get(): |
| 47 | + score.set(score.get() + 1) |
| 48 | + resultLabel = Label(root , text="Correct" , font=('arial' , 20), fg="green") |
| 49 | + resultLabel.grid(row=4 , column=0) |
| 50 | + scoreLabel = Label(root , text=f"Score : {score.get()}" , font=('arial' , 20) , fg="black") |
| 51 | + scoreLabel.grid(row=5 , column=0) |
| 52 | + |
| 53 | + else: |
| 54 | + resultLabel = Label(root , text="Incorrect" , font=('arial' , 20) , fg="red") |
| 55 | + resultLabel.grid(row=4 , column=0) |
| 56 | + |
| 57 | + |
| 58 | + if questionNumber.get() == 10: |
| 59 | + scoreLabel.destroy() |
| 60 | + scoreLabel = Label(root , text=f"Final Score : {score.get()}" , font=('arial' , 20) , fg="black") |
| 61 | + scoreLabel.grid(row=5 , column=0) |
| 62 | + |
| 63 | + else: |
| 64 | + generateQuestion() |
| 65 | + |
| 66 | +def restart(): |
| 67 | + |
| 68 | + global scoreLabel |
| 69 | + scoreLabel.destroy() |
| 70 | + |
| 71 | + score.set(0) |
| 72 | + questionNumber.set(0) |
| 73 | + generateQuestion() |
| 74 | + |
| 75 | + scoreLabel = Label(root , text=f"Score : {score.get()}" , font=('arial' , 20) , fg="black") |
| 76 | + scoreLabel.grid(row=5 , column=0) |
| 77 | +# User Interface |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +headingLabel = Label(root , text="Maths Quiz" , font=('arial' , 25) ) |
| 82 | +headingLabel.grid(row=0 , column=0) |
| 83 | + |
| 84 | +questionScale = Scale(root , from_=0 , to=10 , orient=HORIZONTAL , length=400, variable=questionNumber) |
| 85 | +questionScale.grid(row=1 , column=0) |
| 86 | + |
| 87 | +completeQuestionLabel = Label(root , text="10th question") |
| 88 | +completeQuestionLabel.grid(row=1 , column=1) |
| 89 | + |
| 90 | +questionLabel = Label(root , text=question.get() , font=('arial' , 20)) |
| 91 | +questionLabel.grid(row=2 , column=0) |
| 92 | + |
| 93 | +answerEntry = Entry(root , textvariable=givenAnswer , font=('arial' , 20), width=25) |
| 94 | +answerEntry.grid(row=3 , column=0) |
| 95 | + |
| 96 | +submitButton = Button(root , text="Submit" , fg="yellow", bg="grey", font=('arial' , 15) , command=checkAnswer) |
| 97 | +submitButton.grid(row=3 , column=1) |
| 98 | + |
| 99 | +resultLabel = Label(root , text="Result" , font=('arial' , 20) , fg="blue") |
| 100 | +resultLabel.grid(row=4 , column=0) |
| 101 | + |
| 102 | +scoreLabel = Label(root , text=f"Score : {score.get()}" , font=('arial' , 20) , fg="black") |
| 103 | +scoreLabel.grid(row=5 , column=0) |
| 104 | + |
| 105 | +submitButton = Button(root , text="Restart" , fg="red", font=('arial' , 15), width=35 , command=restart) |
| 106 | +submitButton.grid(row=6 , column=0) |
| 107 | + |
| 108 | +generateQuestion() |
| 109 | + |
| 110 | +root.mainloop() |
0 commit comments