I am doing a quiz game and after I want to pass a page and load new question and options and the options won't change.
I am using the wait_variable()
method and pass a string and just the new question will appear.
The options stay the same and won't change to the new ones.
I don't if the problem is in the index or the wait_variable
I couldn't fix that.
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()
It is because you reset question_number
to 1 in each iteration of for key in questions
loop.
Below is the modified for loop to fix the issue:
# 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)