0

So I created the window by tkinter. Every Button is functional, and connected to each other. I made a command to connect my 'equation_text' with 'question_label' so that when I write answer, let's say " 5+7=?" and I press buttons '1' and '2' (which is 12) and I press enter the program is working. But when new question comes (I imported random and made variables a and b which generate random questions) after I press buttons to answer the question the 'question' label isn't working anymore (maybe because I am using If statement). Not only that but my 'score_label' is working for only 1 time too. Please let me know what kind of loop I should use. Here is my code (Ps: Don't mind the design, it is not finished!!)
code

from tkinter import *
import random

def button_press(num):
    global equation_text
    equation_text = equation_text + str(num)
    equation_label.set(equation_text)
def enter():
    global equation_text
    global question_label
    global score_label
    a = str(random.randint(1, 9))
    b = str(random.randint(1, 9))
    question = f'{a} + {b}'
    score = 0
    data = int(a) + int(b)
    question_label.config(text=question)
    if str(equation_text) == str(data):
        score += 1
        score_label.config(text= "Score: "+str(score))

    else:
        pass

def clear():
    global equation_text
    equation_label.set("")
    equation_text = ""

win = Tk()
win.title("Quiz")

win.config(bg='purple')

equation_text = ''
#refresh_icon = PhotoImage(file="C:\\Users\\user\\Pictures\\Saved Pictures\\refresh1.png")
equation_label = StringVar()
question_label = Label(win, font=("Arial",18),width=18,bg='#e042f5',height=2)
question_label.grid(padx=20,pady=7,row=0,column=0)
enter()
label = Label(win, textvariable=equation_label, font=("Consolas", 19), width=18, height=2)
label.grid(pady=7,row=2,column=0)
score_label = Label(win, font=("Arial",18),width=10,bg='#e042f5',height=1)
score_label.grid(row=3,column=0)
#refresh = Button(win,image=refresh_icon,command = refresh)
#refresh.grid(pady=7,row=1,column=0)

button9 = Button(win, text='9', font=("Robotic", 18), height=2, width=8, bg='grey',
                 command=lambda: button_press(9))
button9.grid(pady=3,row=0,column=3)
button8 = Button(win, text='8', font=("Robotic", 18), height=2, width=8, bg='grey',
                 command=lambda: button_press(8))
button8.grid(pady=3,row=0,column=2)
button7 = Button(win, text='7', font=("Robotic", 18), height=2, width=8, bg='grey',
                 command=lambda: button_press(7))
button7.grid(pady=3,row=0,column=1)
button6 = Button(win, text='6', font=("Robotic", 18), height=2, width=8, bg='grey',
                 command=lambda: button_press(6))
button6.grid(pady=3,row=1,column=3)
button5 = Button(win, text='5', font=("Robotic", 18), height=2, width=8, bg='grey',
                 command=lambda: button_press(5))
button5.grid(pady=3,row=1,column=2)
button4 = Button(win, text='4', font=("Robotic", 18), height=2, width=8, bg='grey',
                 command=lambda: button_press(4))
button4.grid(pady=3,row=1,column=1)
button3 = Button(win, text='3', font=("Robotic", 18), height=2, width=8, bg='grey',
                 command=lambda: button_press(3))
button3.grid(pady=3,row=2,column=3)
button2 = Button(win, text='2', font=("Robotic", 18), height=2, width=8, bg='grey',
                 command=lambda: button_press(2))
button2.grid(pady=3,row=2,column=2)
button1 = Button(win, text='1', font=("Robotic", 18), height=2, width=8, bg='grey',
                 command=lambda: button_press(1))
button1.grid(pady=3,row=2,column=1)
button_enter = Button(win, text='Enter', font=("Robotic", 18), height=2, width=8, bg='green',
                      command=enter)
button_enter.grid(pady=3,row=3,column=3)
button0 = Button(win, text='0', font=("Robotic", 18), height=2, width=8, bg='grey',
                 command=lambda: button_press(0))
button0.grid(pady=3,row=3,column=2)
button_delete = Button(win, text='Delete', font=("Robotic", 18), height=2, width=8, bg='red',
                       command=clear)
button_delete.grid(pady=3,row=3,column=1)

#win.resizable(False,False)
win.mainloop()

code I tried to use If statement, which I think it is the problem itself. I want the result to be like this "When question comes, user needs to press buttons to input the answer. Then program needs to check the input with output. If it is correct +1 score, if not 0 score. After every answer(Using "Enter" button) I want the program to generate new a and b and to check with input every time. I don't need a count down system or smt, I can make it myself, I don't want to bother anyone."

1 Answers1

0

The first time you call enter(), you create a variable data made up of the sum of two integers. You then do the following comparison:

if str(equation_text) == str(data): 

But hey, I haven't yet entered nothing. And you are comparing now, what's in the equation text, with the actual "data" value.

Ok, let's enter the number I think it is, and hit enter again.

Now, upon cliking enter, I create again a new local variable called data, made up of two new random numbers. I then compare what I entered with the new data (not with the old data!).

And that's where your problem lies. But not only that, every time you call enter, score is reset to 0 value!

What options do you have? Well, following the styling of your program, you can declare data and score as global variables; compare data first with the text, and then create the new data value for the next math quiz.

Also, it would be more convenient, that when I hit enter, the text is cleared:

def enter():
    global equation_text
    global question_label
    global score_label
    global score
    global data

    if str(equation_text) == str(data):
        score += 1
        score_label.config(text= "Score: "+str(score))
    else:
        pass

    a = str(random.randint(1, 9))
    b = str(random.randint(1, 9))
    question = f'{a} + {b}'
    data = int(a) + int(b)
    question_label.config(text=question)

    clear()

and then when you call enter() in the main program, just change that for the initialization of data:

equation_label = StringVar()
question_label = Label(win, font=("Arial",18),width=18,bg='#e042f5',height=2)
question_label.grid(padx=20,pady=7,row=0,column=0)
score=0
a = str(random.randint(1, 9))
b = str(random.randint(1, 9))
question = f'{a} + {b}'
data = int(a) + int(b)
question_label.config(text=question)
mamg2909
  • 96
  • 4