python - 在 wait_variable() 文本不会改变之后
问题描述
我正在做一个问答游戏,在我想通过一个页面并加载新的问题和选项之后,选项不会改变。我正在使用 wait_variable() 方法并传递一个字符串,只会出现新问题。选项保持不变,不会更改为新选项。如果问题出在索引或 wait_variable 中,我不会解决这个问题。
from tkinter import *
def sumbit():
name = entry_name.get()
print(name)
def instructions_Window():
instructions_Window = Toplevel()
instructions_Window.title("instructions")
instructions = Label(
instructions_Window,
text = "WELCOME TO THE TRIVIA GAME!\nYour will answer 10 question, the questions will be about general knowledge.\nMake sure you are doing your best maybe you will on the leaderboard soon!\n GOOD LUCK!!!",
font = ("Staatliches",30))
instructions.pack()
back_but = Button(instructions_Window, text="Back", command=instructions_Window.destroy)
back_but.pack()
def trivia_Window():
def m():
print(x.get())
def clear():
WaitState.set(1)
for widgets in trivia_Window.winfo_children():
widgets.destroy()
trivia_Window = Toplevel()
trivia_Window.title("Q&A")
WaitState = StringVar()
x = StringVar()
for key in questions:
question_number = 1
question_label = Label(trivia_Window, text=key, font=("Arial",40))
question_label.pack()
continue_but = Button(trivia_Window, text="Continue", font=("Arial", 10), padx=20, pady=10, command=clear)
continue_but.pack(anchor=S)
for i in options[question_number-1]:
options_radio = Radiobutton(trivia_Window, text=i, variable=x, value=i, font=("Arial", 20), command=m)
options_radio.pack()
question_number = question_number + 1
continue_but.wait_variable(WaitState)
questions = {
"How old is the universe?": "C",
"Who was the first person in space?": "C",
"In which year the first covid-19 case was discovered?": "C",
"What is the most populated country?": "A"
}
options = [[["A. 5.3 billion years old"], ["B. 13.8 billion years old"], ["C. 13.8 milion years old"], ["D. 241.1 billion years old"]],
[["A. Alan Shepard"], ["B. Neil Armstrong"], ["C. Yuri Alekseyevich Gagarin"], ["D. Ilan Ramon"]],
[["A. 2018"], ["B. 2001"], ["C. 2019"], ["D.2020"]],
[["A. China"],["B. Russia"], ["C. India"], ["D. United States"]]]
window = Tk()
window.title("Home")
window.geometry("1920x1080")
window.iconbitmap("pp.ico")
window.config(bg="#93b4ba")
label_welcome = Label(window, text="Welcome Back To Our Trivia Game!", font=("Akaya Kanadaka",80,"bold"), bg = "#93b4ba")
label_welcome.pack()
label_enter_name = Label(window, text="Enter you name:", font=("Lato",50,"bold"), bg = "#93b4ba", fg="#3038d1")
label_enter_name.pack(side=LEFT)
entry_name = Entry(window,font=("Arial",40))
entry_name.pack(side=LEFT)
sumbit_but = Button(window, text="Sumbit", font=("Arial",10,"bold"), width=15, height=4,command=sumbit, bg="#0f0f0f", fg="white")
sumbit_but.pack(side=LEFT)
quit_but = Button(window, text="Quit", font=("Arial",10,"bold"), width=20, height=10,command=quit,bg="#b5aa72")
quit_but.place(x=0,y=845)
start_but = Button(window, text="Start", font=("Arial",10,"bold"), width=20, height=10,command=trivia_Window ,bg="#a1ad90")
start_but.place(x=1750,y=845)
instructions_but = Button(window, text="Instructions", font=("Arial",10,"bold"), width=20, height=10,command=instructions_Window,bg="#626363")
instructions_but.pack(side=RIGHT)
window.mainloop()
解决方案
question_number
这是因为您在循环的每次迭代中都重置为 1 for key in questions
。
下面是修改后的 for 循环来解决这个问题:
# use enumerate() to set question_number
for question_number, key in enumerate(questions):
question_label = Label(trivia_Window, text=key, font=("Arial",40))
question_label.pack()
continue_but = Button(trivia_Window, text="Continue", font=("Arial", 10), padx=20, pady=10, command=clear)
continue_but.pack(anchor=S)
x.set(' ') # reset selection of radiobuttons
for i in options[question_number]:
options_radio = Radiobutton(trivia_Window, text=i, variable=x, value=i, font=("Arial", 20), command=m)
options_radio.pack()
continue_but.wait_variable(WaitState)
推荐阅读
- node.js - 为什么我无法使用以下 NodeJS 代码上传文件?
- linux - ElasticSearch 随机停止
- java - 如何从 Controller 移动对用户输入的验证
- firebase - 匿名用户呢?
- vue.js - 为什么我无法获取最新版本的 postcss
- python - 使用 fit_desoto 函数时出错,由 optimize.root (SciPy) 引起
- c# - 如何在反射发射类中发射局部变量 init ([0] string V_0)?
- tensorflow2.0 - 使用一种热编码
- postgresql - Postgresql 查询每个范围内的第一个和最后一个
- http - HTTP 上 jpeg 图像的权威“内容类型”是什么?