python - 如何在 for 循环中等待用户单击 tkinter 按钮并给出一些答案,该循环会读取列表中的每个问题和答案
问题描述
设置:
系统:Windows 10 Home x64
Python 版本:3.7.4
软件:Pycharm 或 IDLE
我正在开发一个基本上由问答游戏组成的“应用程序”,但是当它进入循环时,程序不会等待任何用户点击答案按钮部分,代码只会在列表中的最后一个问题处运行(基本上继续我给出的每个问题(而不是等待任何按钮单击)(qaList),以按钮形式显示最后一个问题和该问题的可能答案。
我想当时给出一个问题,每次运行应用程序时随机排序,并在按钮形式上给出问题的可能答案,等待用户选择。然后在控制台上打印是对还是错,然后继续下一个问题,但我做不到。
我选择循环进行,因为像这样,当我想向程序添加更多问题时,我几乎可以毫不费力地做到这一点。
有什么方法可以让这个循环等待用户选择,然后在用户单击任何 anwser 按钮时继续?
我的(问题部分)代码:
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()
解决方案
推荐阅读
- javascript - 如何将 JavaScript 函数引用到 HTML 文件中
- r - 获得加权因子分数
- python - 使用 Python 在 JSON 对象中逐行插入值
- react-native - 使用 route.params 传递图像 React native
- python - ImportError:无法从 'network' 导入名称 'Conv2d' (G:\Saadain\Anaconda\envs\SCALNet\lib\site-packages\network.py)
- spring-boot - 由于加载基于 application-dev.properties 的 url,Spring boot 无法启动
- sql - 消息 156,级别 15,状态 1,第 7 行关键字“BETWEEN”附近的语法不正确
- javascript - 控制台中的文本显示不正确
- javascript - 创建函数后如何将局部变量定义为函数
- javascript - TypeError:无法读取 useState ReactJS 未定义的属性“地图”