this is a small part of my code which for some reason isnt working, the radio button – user_ans more specifically always return none, when before it returned the actual response- i have no idea what happened and i cant revert it. here is the code.
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import *
import tkinter
from time import *
import time
import PIL
from PIL import *
#---- ----#
Qright = 0
Qwrong = 0
current_question = 0
pink = 0
def Quizz():
global pink
menu.withdraw()
# define question dictionary
question = {
"2+3": ['2', '3', '5', '9'],
"2-1": ['2', '1', '4'],
"3+3": ['3', '6', '9', '7'],
"2+5": ['2', '3', '7', '9'],
"2-2": ['2', '1', '0'],
"3+1": ['3', '6', '4', '7'],
"2+6": ['2', '3', '8', '9'],
"2-6": ['2', '1', '-4'],
"3+6": ['3', '6', '9', '7'],
"3+2": ['3', '6', '5', '7']
}
# define answer list
ans = ['5', '1', '6', '7', '0', '4', '8', '-4', '9', '5']
def start_quiz():
print("stage6")
start_button.forget()
print("stage7")
next_button.pack(pady = 10)
print("stage8")
next_question()
def next_question():
print("stage9")
global current_question
if current_question < len(question):
print("stage10")
# get key or question that need to be printed
check_ans()
print("stage14")
user_ans.set('None')
print("stage15")
c_question = list(question.keys())[current_question]
print("stage16")
# clear frame to update its content
clear_frame()
print("stage17")
# printing question
Label(f1, text=f"Question : {c_question}", font="calibre 12 normal").pack(anchor=NW)
print("stage18")
# printing options
for option in question[c_question]:
print("stage19")
Radiobutton(f1, text=option, variable=user_ans, value=option).pack(anchor=NW)
print("stage20")
print(user_ans.get())
print("-")
print(user_ans.get())
print("-")
current_question += 1
else:
next_button.forget()
check_ans()
clear_frame()
output = f"Your Score is {user_score.get()} out of {len(question)}"
Label(f1, text=output, font="calibre 25 bold").pack()
Label(f1, text="Thanks for Participating",font="calibre 18 bold").pack()
'''f = open("results.txt", "a")
f.write(f"n {user_score.get()}/{len(question)}")
f.close()
exit_button = Button(root, text="Return to menu", command=lambda:[deiconify,root.destroy])
exit_button.pack(pady=20)'''
def check_ans():
print("stage11")
temp_ans = user_ans.get()
print("stage12")
print(temp_ans)
print(ans[current_question-1])
if temp_ans != 'None' and temp_ans == ans[current_question-1]:
print("stage13")
user_score.set(user_score.get()+1)
else:
return
def clear_frame():
for widget in f1.winfo_children():
widget.destroy()
if __name__ == "__main__":
print("stage0")
root = Tk()
print("stage1")
# setup basic window
root.title("Main Quiz")
root.geometry("850x520")
root.minsize(800, 400)
root.wm_attributes('-transparentcolor', '#000001')
user_ans = StringVar()
user_ans.set('None')
user_score = IntVar()
user_score.set(0)
print("stage2")
print(pink)
if pink == 0:
Label(root, text="Quiz", font="calibre 20 bold").pack()#could add background="colour"
else:
Label(root, text="Quiz", font="calibre 20 bold",background=("pink")).pack()#could add background="colour"
root.configure(background='pink')
print("stage3")
#Label(root, text="", font="calibre 10 bold").pack()
print("stage4")
start_button = Button(root, text="Start Quiz",command=start_quiz)
start_button.pack(pady = 10)
print("stage5")
f1 = Frame(root)
f1.pack(side=TOP, fill=X)
next_button = Button(root, text="Next Question",command=next_question)
root.mainloop()
Ive tried to replace the radio button but i need to use it.
ive tried replacing check_ans with a new function and other options but nothing is working. I just cant fix it and this will take ages to fix i am just a student and all my searches are returning solutions with ttk.Radiobutton which i think doesnt apply to me. My code is very specific and its hard to search so i just need personal oppinions. Thanks!