这里的代码是一个测验。此时,代码会创建一个包含问题和答案的窗口,当按下正确的答案(较大的按钮)时,它会创建另一个窗口,上面有一个“ok”按钮和一条消息“correct”。单击“确定”按钮后,问题窗口将刷新为另一个问题。当您再次单击正确答案时,窗口将再次显示“确定”按钮,但它将有两个按钮。这种情况会一直发生,每次代码循环时都会添加一个“ok”按钮。有什么办法可以解决吗?你知道吗
(我还被告知,有一种更简单的方法可以整体完成这项工作,我实际上首先生成问题和答案,然后将它们放入列表中,然后随机调用它们,这样我就可以确保问题不会重复。然后我可以有“正确”的信息出现在同一个窗口,并禁用按钮,使我不能点击另一个答案。信息将转到下一个有新答案的问题。这可能吗?如果是这样,那将是一个巨大的帮助)。你知道吗
谢谢你的帮助。你知道吗
def quiz_easy():
global question
questions = []
correctanswers = []
possibleanswer1s = []
possibleanswer2s = []
possibleanswer3s = []
easy_questions = open("sup.txt", "r")
count = 1
for line in easy_questions:
if count == 1:
questions.append(line.rstrip())
elif count == 2:
correctanswers.append(line.rstrip())
elif count == 3:
possibleanswer1s.append(line.rstrip())
elif count == 4:
possibleanswer2s.append(line.rstrip())
elif count == 5:
possibleanswer3s.append(line.rstrip())
if count == 5:
count = 1
else:
count = count + 1
print(questions)
print (correctanswers)
randno1 = random.randint(0,len(questions)-1)
master = Tk()
text = Text(master)
text.insert(INSERT, questions[randno1])
text.insert(END, "?")
text.pack()
text.tag_add("here","5.0", "1.4")
text.tag_add("start", "1.8", "1.13")
correct = Tk()
text = Text(correct)
text.insert(INSERT, "Correct")
text.insert(END, "!")
text.pack()
correct.withdraw()
def close_wrong():
quiz_easy()
wrong.destroy()
def correctans():
correct.deiconify()
master.withdraw()
print("Hello")
##This part here is the recurring "ok" button which needs to be fixed
okbutton = Button(correct, text="Ok", command=close_correct)
if not okbutton.winfo_exists():
okbutton = Button(correct, text="Ok", command=close_correct)
okbutton.pack()
def wrong():
wrong = Tk()
text = Text(wrong)
text.insert(INSERT, "Wrong")
text.insert(END, "!")
text.pack()
def well_done():
welldone = Tk()
text = Text(welldone)
text.insert(INSERT, "WHATYOUTALKINGTOME")
text.insert(END, "?!")
text.pack()
okbutton = Button(welldone, text="Ok", command=game_loop)
okbutton.pack()
def close_correct():
global question
if question == 5:
well_done()
correct.destroy()
else:
#quiz_easy()
master.deiconify()
correct.withdraw()
randno2 = random.randint(0,23)
if randno2 == 0:
answer1button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer2button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 1:
answer1button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer2button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 2:
answer1button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer3button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 3:
answer1button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer3button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 4:
answer1button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer4button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 5:
answer1button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer4button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 6:
answer2button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer1button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 7:
answer2button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer1button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 8:
answer2button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer3button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 9:
answer2button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer3button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 10:
answer2button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer4button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 11:
answer2button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer4button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 12:
answer3button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer1button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 13:
answer3button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer1button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 14:
answer3button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer2button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 15:
answer3button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer2button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer4button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 16:
answer3button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer4button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 17:
answer3button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer4button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 18:
answer4button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer1button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 19:
answer4button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer1button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 20:
answer4button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer2button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 21:
answer4button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer2button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer3button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 22:
answer4button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer3button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
elif randno2 == 23:
answer4button = Button(master, text=correctanswers[randno1], width=20, command=correctans)
answer3button = Button(master, text=possibleanswer1s[randno1], command=wrong)
answer2button = Button(master, text=possibleanswer2s[randno1], command=wrong)
answer1button = Button(master, text=possibleanswer3s[randno1], command=wrong)
question = question + 1
answer1button.pack()
answer2button.pack()
answer3button.pack()
answer4button.pack()
master.mainloop()
首先,不要在一个应用程序中生成多个Tk对象。这就是顶级小部件的用途。现在,您只有两个选项来纠正错误。一种是在程序启动时生成一个顶级对象并显示/隐藏它,另一种是在每次需要显示时生成一个新的顶级对象,然后销毁它。为了简单起见,我将演示第二个-这里是您的函数编辑工作(希望如此):
lambda技巧有点脏,但是如果您不愿意为
welldone
使用全局引用,那么它是必要的。你知道吗至于问题的第二部分,我将使用随机模块中的shuffle函数。将问答列表压缩为元组列表(python3的
zip(questions, correctanswers, possibleanswer1s, possibleanswer2s, possibleanswer3s)
或list(zip(...))
大小写),然后对结果进行无序排列。反复阅读并填写问题和答案:对于问题的第三部分,我将不提供任何代码,因为问题太松散了。您可以添加标签或文本小部件,并使用pack\u forget()(如果使用grid,则使用grid\u forget())将其“解包”。当然,你可以再正常打包。你知道吗
相关问题 更多 >
编程相关推荐