0

Setup:
System: Windows 10 Home x64
Python version: 3.7.4
Software: Pycharm or IDLE

I'm working on an "App" that basically consists of a quiz game, but when it goes into the loop the program does not wait for any user clicks at answers buttons section, the code just runs at the last question on the list (basically going on every question and not waiting for any button click) that I gave (qaList), displaying the last question and the possible answers of that question in button form.

I wanted to give one question at the time, randomizing the order every time that I run the App, and give the possible answers for the question on button form and wait for the user choice. Then just print on console if is right or wrong and go for the next question, but I can't make it happen.

I choose to go on in a loop because like this when ever I want to add more questions to the program I can do it almost without any effort.

There is any way to get this loop wait for the user choice and just go on when the user click any anwser button?

My (question part) code:

import random
import tkinter as tk

width = 640  # Largura
height = 480  # Altura

# GUI SYSTEM
root = tk.Tk()
canvas = tk.Canvas(root, height=height, width=width)
canvas.pack()

def play_quiz():
    class pergunta_resposta:
        def __init__(self, question, answer, options):
            self.question = question
            self.answer = answer
            self.options = options

    # ===================list with  this order ('question', 'right answer', [3 other possible answers]===================
    qaList = [pergunta_resposta("", "", ["", "", ""]), 
              pergunta_resposta("", "", ["", "", ""]), 
              pergunta_resposta("", "", ["", "", ""]), 
              pergunta_resposta("", "", ["", "", ""]), 
              pergunta_resposta("", "", ["", "", ""]), 
              pergunta_resposta("", "", ["", "", ""]), 
              pergunta_resposta("", "", ["", "", ""])]

    # ===================randomise the order of the questions inside the list===================
    random.shuffle(qaList)

    # ===================for loop to go on every single question===================
    for Item in qaList:
        frame = tk.Frame(root, bg='white')
        frame.place(rely=0.05, relx=0.05, relheight=0.25, relwidth=0.9)
        ask = tk.Label(frame, text=Item.question, font=("Impact", 15))
        ask.pack(expand=True)

        possible = Item.options + [Item.answer]
        random.shuffle(possible)

        # ===================possible answers buttons functions===================
        def button(n):
            global correct
            if possible[n] == Item.answer:
                print('Right')
                correct = 1
            elif possible[n] != Item.answer:
                print('Wrong!')
                correct = 0

        # ===================frame for possible answers buttons===================
        frame1 = tk.Frame(root, bg='white')
        frame1.place(rely=0.35, relx=0.15, relheight=0.4, relwidth=0.7)

        # possible answers buttons
        answer1 = tk.Button(frame1, text=possible[0], font=("Impact", 15), command=lambda: button(0))
        answer1.pack(fill='both', expand=True)

        answer2 = tk.Button(frame1, text=possible[1], font=("Impact", 15), command=lambda: button(1))
        answer2.pack(fill='both', expand=True)

        answer3 = tk.Button(frame1, text=possible[2], font=("Impact", 15), command=lambda: button(2))
        answer3.pack(fill='both', expand=True)

        answer4 = tk.Button(frame1, text=possible[3], font=("Impact", 15), command=lambda: button(3))
        answer4.pack(fill='both', expand=True)

play_quiz()
root.mainloop()
  • You cannot have any of the normal loop constructs in a `tkinter` program to control program flow since the only loop allowed is the `root.mainloop()`. You will need to have a button press event function to update the GUI for each question. – quamrana Nov 10 '19 at 18:23
  • Don't use a for loop for this, use a while loop and then change the question whenever they answer – Hippolippo Nov 10 '19 at 18:24
  • See Bryan Oakley's [answer](https://stackoverflow.com/a/9343402/355230) to [Tkinter — executing functions over time](https://stackoverflow.com/questions/9342757/tkinter-executing-functions-over-time). Personally I've found it useful to think of and code my app as a [finite-state-machine](https://en.wikipedia.org/wiki/Finite-state_machine) (FSM), where the user inputs determine what its next state will be. – martineau Nov 10 '19 at 18:26

0 Answers0