1

I am trying to make a quiz in tkinter, and I have five questions. But, I want to wait for an answer to be inputted to the entry widget. I know I will probably need a button, but I don't know how I would go about doing that. My code so far:

for i in range(5):
        randChoose = random.choice(choose)
        questionLabel = Label(top, text=full[randChoose]).grid(row=0, column=0)
        answerLabel = Label(top, text="Answer:").grid(row=1, column=0)
        answerEntry = Entry(top, borderwidth=5).grid(row=1,column=1)

        if answerEntry.get() == aFull[randChoose]:
            correctLabel = Label(top, text="Correct!",fg="green").grid(row=2,column=0)
            score += 1
            scoreLabel = Label(top, text=f"Your Score is {score}",fg="green").grid(row=2,column=1)
        else:
            wrongLabel = Label(top, text="Incorrect!",fg="red").grid(row=2,column=0)
            scoreLabel = Label(top, text=f"Your Score is {score}",fg="red").grid(row=2,column=1)
        choose.remove(randChoose)
Caleb Shaw
  • 73
  • 1
  • 9
  • you can check the input status by `root.update()` after button is pressed – Zaraki Kenpachi Feb 05 '20 at 19:09
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759). As it stands, you are stacking widgets over and over and ... – stovfl Feb 05 '20 at 20:20

1 Answers1

1

First, add a button

button_pressed = StringVar()

this variable button_pressed will tell us if the button was pressed or not
(we set button_pressed to a StringVar() so .set() can change this variable, in the button's command)

button = Button(app, text="Enter", command=lambda: button_pressed.set("button pressed"))

when the button is pressed it will set the variable button_pressed, to "button pressed"

button.grid(row=1, column=1)

Then, wait until the button is pressed

button.wait_variable(button_pressed)

this code will wait until the varialbe button_pressed is changed (to anything)

Finally, check the entry

if answerEntry.get() == aFull[randChoose]: etc.

Final code should look something like this:

for i in range(5):
        randChoose = random.choice(choose)
        questionLabel = Label(app, text=full[randChoose]).grid(row=0, column=0)
        answerLabel = Label(app, text="Answer:").grid(row=1, column=0)
        answerEntry = Entry(app, borderwidth=5).grid(row=1,column=1)

        button_pressed = StringVar()
        button = Button(app, text="Enter", command=lambda: button_pressed.set("button pressed"))
        button.grid(row=1, column=1)

        button.wait_variable(button_pressed)

        if answerEntry.get() == aFull[randChoose]:
            correctLabel = Label(app, text="Correct!", fg="green").grid(row=2, column=0)
            score += 1
            scoreLabel = Label(app, text="Your Score is {score}", fg="green").grid(row=2, column=1)
        else:
            wrongLabel = Label(app, text="Incorrect!", fg="red").grid(row=2, column=0)
            scoreLabel = Label(app, text="Your Score is {score}", fg="red").grid(row=2, column=1)
        choose.remove(randChoose)

and you probobly want to destroy this button so on the next question it wont display 2 buttons

button.destroy()
DYD
  • 382
  • 2
  • 15