Python 3.x Tkinter:忘记标签,更新它,再次出现?
这是我正在编写的拼写比赛程序的一部分。我用数字代替了拼写和定义。我要做的是,当正确的输入被赋予一个标签,显示为“正确”,并显示一个按钮,显示为“下一步”。当按下“下一步”按钮时,应该忘记带有定义的标签,然后i(我正在使用的计数器)应该增加1,并且定义标签应该再次出现,上面有下一个定义,准备回答。此时,定义标签消失,然后返回相同的定义,而不是列表中的下一个定义Python 3.x Tkinter:忘记标签,更新它,再次出现?,python-3.x,tkinter,Python 3.x,Tkinter,这是我正在编写的拼写比赛程序的一部分。我用数字代替了拼写和定义。我要做的是,当正确的输入被赋予一个标签,显示为“正确”,并显示一个按钮,显示为“下一步”。当按下“下一步”按钮时,应该忘记带有定义的标签,然后i(我正在使用的计数器)应该增加1,并且定义标签应该再次出现,上面有下一个定义,准备回答。此时,定义标签消失,然后返回相同的定义,而不是列表中的下一个定义 from tkinter import * spelling = ["1", "2", "3", "4", "5", "6", "7",
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()
从tkinter导入*
拼写=[“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”]
定义=[“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”]
i=0
gui=Tk()
gui.可调整大小(0,0)
app=Frame(gui)
app.grid()
标题(“测试”)
图形用户界面几何图形(“600x400+300+10”)
def answercheck():
全局拼写、定义、i
panswerinput=answerinput.get()
如果拼写[i]==PANSWER输入:
如果i<9:
回答正确。位置(x=310,y=180)
nextqbutton.place(x=160,y=140)
其他:
关闭窗口()
其他:
回答错误。位置(x=310,y=180)
def nextdefinition():
全球i
回答正确。把你忘了
定义打印。放置_忘记()
i=i+1
定义打印位置(x=310,y=50)
nextqbutton.place_忘记()
#问题布局小部件
hereisdefinition=标签(bg=“白色”,text=“定义如下:”)
hereisdefinition.config(高度=2,宽度=20)
定义打印=标签(文本=定义[i],fg=“黄色”,bg=“橙色”)
definitionprint.config(高度=2,宽度=17)
输入答案=标签(bg=“白色”,text=“请输入您的答案:”)
enteranswer.config(高度=2,宽度=20)
应答输入=输入(宽度=20)
answerenter=按钮(text=“Enter!”,bg=“white”,command=answercheck)
answerenter.config(高度=1,宽度=17)
#应答检查小部件
错误答案=标签(fg=“红色”,text=“不正确,请重试”)
correctanswer=标签(bg=“绿色”,fg=“白色”,text=“正确,做得好”)
nextqbutton=Button(text=“Next”,bg=“white”,command=nextdefinition)
nextqbutton.config(高度=1,宽度=17)
#启动小部件
此处为定义位置(x=160,y=50)
定义打印位置(x=310,y=50)
输入答案。位置(x=160,y=90)
应答输入位置(x=310,y=100)
回答输入位置(x=310,y=140)
gui.mainloop()
谢谢:)请不要使用“i”、“l”或“O”作为单位数变量名,因为它们可能看起来像数字。所有的几何管理者都有一个“忘记”的方面,我认为这就是你想要的。请尝试以下代码段。它使用了一个更简单、更直接的类。哦,effbot有一些关于几何管理器的链接
你是要我们调试你的程序,还是有什么特别的问题?我不认为这是一个bug,只是我糟糕的编码技能。我正在寻找解决我问题的方法(上图)
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()