Python 我怎么能在for循环中等待用户点击带有答案的tkinter按钮,而for循环读取列表中每个问题的答案

Python 我怎么能在for循环中等待用户点击带有答案的tkinter按钮,而for循环读取列表中每个问题的答案,python,python-3.x,for-loop,tkinter,Python,Python 3.x,For Loop,Tkinter,设置: 系统:Windows 10 Home x64 Python版本:3.7.4 软件:Pycharm或IDLE 我正在开发一个“应用程序”,它基本上由一个问答游戏组成,但当它进入循环时,程序不会等待任何用户点击答案按钮部分,代码只会运行在列表上的最后一个问题上(基本上是针对每个问题,而不是等待任何按钮点击),我给出了(qaList),以按钮形式显示最后一个问题和该问题的可能答案 我想一次给出一个问题,每次运行应用程序时随机排列顺序,然后在按钮表单上给出问题的可能答案,等待用户选择。然后在控制

设置:
系统:Windows 10 Home x64
Python版本:3.7.4
软件:Pycharm或IDLE

我正在开发一个“应用程序”,它基本上由一个问答游戏组成,但当它进入循环时,程序不会等待任何用户点击答案按钮部分,代码只会运行在列表上的最后一个问题上(基本上是针对每个问题,而不是等待任何按钮点击),我给出了(qaList),以按钮形式显示最后一个问题和该问题的可能答案

我想一次给出一个问题,每次运行应用程序时随机排列顺序,然后在按钮表单上给出问题的可能答案,等待用户选择。然后在控制台上打印是否正确,然后进入下一个问题,但我无法让它发生

我选择在循环中继续,因为像这样,每当我想在程序中添加更多的问题时,我几乎不需要任何努力就可以做到

有没有办法让这个循环等待用户选择,然后在用户单击任何anwser按钮时继续

我的(问题部分)代码:


您不能在
tkinter
程序中使用任何常规循环构造来控制程序流,因为唯一允许的循环是
root.mainloop()
。你需要有一个按钮按下事件功能来更新每个问题的GUI。不要为此使用for循环,使用while循环,然后在他们回答Bryan Oakley的问题时更改问题。就我个人而言,我发现把我的应用程序想象成一个(FSM)并编写代码是很有用的,用户的输入决定了它的下一个状态。
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()