不需要的按钮被添加到Tkinter测验

时间:2015-03-11 15:29:59

标签: python tkinter

这里的代码是一个测验。目前,代码创建了一个包含问题和答案的窗口,当按下正确答案时(较大的按钮),它会创建另一个窗口,其中包含" ok"它上面的按钮和一条消息说"正确"。一旦" ok"单击按钮,问题窗口将刷新另一个问题。当您再次单击正确的答案时,窗口将再次出现,并显示" ok"按钮除了它将有两个按钮。这将继续发生,再添加一个" ok"每次代码循环时按钮。无论如何要解决它?

(我还被告知有更简单的方法可以做到这一点,我实际上先生成问题和答案,然后将它们放入列表然后随机调用它们以便我可以确保问题不在&# 39;重复。然后我可以在同一个窗口中显示"正确的"消息并禁用按钮,这样我就无法点击另一个答案。消息将变为下一个问题新的答案。这可能吗?如果是这样,那将是一个巨大的帮助)。

感谢您的帮助。

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() 

1 个答案:

答案 0 :(得分:1)

首先,永远不要在一个应用程序中创建多个Tk对象。这就是Toplevel小部件的用途。现在,您可以选择两种方法来纠正错误。一种是在程序启动时显示一个顶层对象并显示/隐藏它,另一种是每次需要显示时创建一个新的顶层,然后将其销毁。为了简单起见,我将演示第二个 - 这是您编辑的功能(希望如此):

def well_done():
    welldone = Toplevel()
    text = Text(welldone)
    text.insert(INSERT, "WHATYOUTALKINGTOME")
    text.insert(END, "?!")
    text.pack()
    okbutton = Button(welldone, text="Ok", command=(lambda: welldone.destroy() or game_loop()))

    okbutton.pack()

lambda技巧有点脏,但如果你不愿意使用welldone的全局引用,那就是必要的。

至于你问题的第二部分,我将使用随机模块中的shuffle函数。将您的问题和答案列表压缩到元组列表({3}的zip(questions, correctanswers, possibleanswer1s, possibleanswer2s, possibleanswer3s)list(zip(...))案例),然后将结果洗牌。迭代它并填写问题和答案:

zipped=zip(questions, correctanswers, possibleanswer1s, possibleanswer2s, possibleanswer3s)
for q, ca, ica1, ica2, ica3 in random.shuffle(zipped):
    text.insert(INSERT, q)
    answer1button = Button(master, text=ca, width=20, command=correctans)
    answer2button = Button(master, text=ica1, command=wrong)
    answer3button = Button(master, text=ica2, command=wrong)
    answer4button = Button(master, text=ica3, command=wrong)
    question = question + 1

对于问题的第三部分,我不会提供任何代码,因为问题太松散了。您可以添加Label或Text小部件,并使用pack_forget()(如果使用grid,则使用grid_forget())来“解包”它。当然,你可以正常包装它。