Tkinter:忘记标签,更新它,再次出现?

时间:2014-03-14 22:34:28

标签: python-3.x tkinter

这是我正在尝试编写的拼写蜜蜂计划的一部分。我用数字替换了拼写和定义。我想要做的是当正确的输入给出一个标签以显示“正确”和一个按钮显示“下一步”。当下一个按钮被击中时,应该忘记具有定义的标签,i(我正在使用的计数器的东西)应该增加1并且定义标签应该再次出现,下一个定义就在它上面,准备好了回答。在momemt中,定义标签消失,然后返回相同的定义,而不是列表中的下一个。

from tkinter import *
spelling = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
definition = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
i = 0

gui = Tk()
gui.resizable(0, 0)
app = Frame(gui)
app.grid()
gui.title("test")
gui.geometry("600x400+300+10")

def answercheck():
    global spelling, definition, i
    panswerinput = answerinput.get()
    if spelling[i] == panswerinput:
        if i < 9:
            correctanswer.place(x=310, y=180)
            nextqbutton.place(x=160, y=140)
        else:
            close_window() 
    else:
        wronganswer.place(x=310, y=180)

def nextdefinition():
    global i
    correctanswer.place_forget()
    definitionprint.place_forget()
    i = i + 1
    definitionprint.place(x=310, y=50)
    nextqbutton.place_forget()

# questionlayout widgets
hereisdefinition = Label(bg="white", text="Here is the definition:")
hereisdefinition.config(height=2, width=20)
definitionprint = Label(text=definition[i], fg="yellow", bg="orange")
definitionprint.config(height=2, width=17)
enteranswer = Label(bg="white", text="Please enter your answer:")
enteranswer.config(height=2, width=20)
answerinput = Entry(width=20)
answerenter = Button(text="Enter!", bg="white", command=answercheck)
answerenter.config(height=1, width=17)

# answercheck widgets
wronganswer = Label(fg="red", text="INCORRECT, TRY AGAIN")
correctanswer = Label(bg="green", fg="white", text="CORRECT, WELL DONE")
nextqbutton = Button(text="Next", bg="white", command=nextdefinition)
nextqbutton.config(height=1, width=17)

# starting widgets
hereisdefinition.place(x=160, y=50)
definitionprint.place(x=310, y=50)
enteranswer.place(x=160, y=90)
answerinput.place(x=310, y=100)
answerenter.place(x=310, y=140)

gui.mainloop()

谢谢:)

1 个答案:

答案 0 :(得分:0)

请不要使用&#34;我&#34;,&#34; l&#34;或者&#34; O&#34;作为单个数字变量名称,因为它们看起来像数字。所有几何管理人员都会忘记&#34;忘记&#34;我认为这是你想要的方面。请尝试以下代码段。它使用了一个更容易和更直接的类。哦,effbot在几何管理器上有一些链接http://effbot.org/tkinterbook/

class TestQuiz():
    def __init__(self):
        self.gui = Tk()
        self.gui.resizable(0, 0)
        self.gui.title("test")
        self.gui.geometry("600x400+300+10")

        self.label_and_button()

        self.ctr = 0
        self.next_question()
        self.gui.mainloop()

    def answercheck(self):
        print self.answer_var.get()
        print "answercheck, insert check and notify code"
        self.nextqbutton.grid(row=2, column=1) ## reappears
        self.answer_but.grid_forget()     ## disappears

    def label_and_button(self):
        self.answer_label = Label(bg="white", text="Please enter your answer:")
        self.answer_label.config(height=2, width=20)
        self.answer_label.grid(row=0, column=0)

        self.answer_var = StringVar()
        self.answerinput = Entry(width=20, textvariable=self.answer_var)
        self.answerinput.grid(row=0, column=1)

        self.answer_but = Button(text="Enter!", bg="white",
                                 command=self.answercheck)
        self.answer_but.config(height=1, width=17)
        self.answer_but.grid(row=1, column=0)

        self.nextqbutton = Button(text="Next", bg="white",
                                  command=self.next_question)
        self.nextqbutton.config(height=1, width=17)
        self.nextqbutton.grid(row=2, column=1)
        self.nextqbutton.grid_forget()

    def next_question(self):
        self.nextqbutton.grid_forget()     ## disappears
        self.answer_but.grid(row=1, column=0)  ## reappears
        self.answer_label.config(text=str(self.ctr))
        self.answer_var.set("")
        self.ctr += 1

TQ=TestQuiz()