我想在tkinter做一个测验,我有五个问题。但是,我想等待一个答案被输入到条目小部件。我知道我可能需要一个按钮,但我不知道该怎么做。 到目前为止,我的代码是:
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)
首先,添加一个按钮
这个变量
button_pressed
将告诉我们按钮是否被按下(我们将
button_pressed
设置为StringVar()
,以便.set()
可以在按钮的命令中更改此变量)按下按钮时,它会将变量
button_pressed
设置为“按钮按下”然后,等待按钮按下
此代码将等待变量
button_pressed
更改(为任何内容)最后,检查条目
if answerEntry.get() == aFull[randChoose]:
等最终代码应该如下所示:
你可能想破坏这个按钮,所以在下一个问题上,它不会显示2个按钮
相关问题 更多 >
编程相关推荐