我正在尝试创建一个生成tkinter窗口的模块,然后用一组答案打包一个问题。我的问题是让算法一个接一个地产生问题和答案。我的意思是,算法目前只包装答案集,并且不会等到玩家回答刷新并生成新集之后。我多年来一直在考虑这个问题,而且我是tkinter的新手,所以我不知道自己在做什么。我的代码如下:
这是我检查答案是否正确的代码
score = IntVar(window, 0)
score.set(0)
def check_answer(answer):
global r
global order
if answer in order:
global score
s = score.get() + 1
score.set(s)
lbl.config(window, textvariable=score)
else:
lbl.config(text="Wrong")
这是我的代码,它生成答案集并将它们打包到窗口中:
r = len(questions)
order= []
question = StringVar()
for i in range(r):
question.set(questions[i])
selected=tkinter.Label(window, textvariable=question.get())
selected.config(textvariable=question.get())
selected.pack()
for j in answers[i]:
if j != answers[i][0]:
answer = tkinter.Button(window, text=j, command=lambda answer=j: check_answer(answer))
answer.pack()
order.append(i)
lbl = tkinter.Label(window,textvariable=score.get())
lbl.pack()
答案 0 :(得分:1)
我不确定你想要什么,但我猜它是这样的:
因此,您必须在check_answer函数中调用显示问题集的代码。
e.g。 :
def display_question(i):
# your code to display question i
# ...
for j in answers[i]:
# ...
answer = tkinter.Button(window, text=j, command=lambda : check_answer(j, i))
def check_answer(answer, question):
# your code for checking what you want and counting score
# ...
if answer in order:
# ....
display_question(question + 1)
# and at the beginind :
display_question(0)