我为我的孩子做了一个小游戏,我只是把它放进了tkinter。我花了很长时间试图查看是否可以删除文本,而我发现的方法并不是我所需要的(即删除())。目前,它可以工作,它只是在垂直方向上保留新行上的文本,因此它变得非常长。
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z"]
win = ["Good work! Let's go again.", "Way to go. Your hard work is paying off!", "Good attention for detail!"]
oops = ["Opps. Keep trying! You'll get it next time.", "That's not right but nice try! Keep working at it."]
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("Learning is fun!")
self.pack(fill=BOTH, expand=1)
mathButton = Button(self, text="Numbers", command=self.math)
letterButton = Button(self, text="Letters", command=self.alphafun)
mathButton.place(x=50, y=250)
letterButton.place(x=250, y=250)
def alphafun(self):
self.x = random.choice(alphabet)
text = Label(self, text=f"Type this letter: {self.x}")
text.pack()
self.user_input = StringVar()
prompt = ttk.Entry(width=14, textvariable=self.user_input)
prompt.place(x=150, y=200)
submitButton = Button(self, text="Submit", command=self.alphacheck)
submitButton.place(x=255, y=200)
submitButton.bind("<Return>", func=self.alphacheck)
def alphacheck(self):
if self.user_input.get() == self.x:
text = Label(self, text=f"\n{random.choice(win)}")
text.pack()
self.alphafun()
else:
text = Label(self, text=f"\n{random.choice(oops)}")
text.pack()
self.alphafun()
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
为简洁起见,我遗漏了一个数学函数。
我希望窗口清除文本,基本上只是返回屏幕看起来像init_window,但也重新运行alphafun函数。
尝试:
self.destroy() - 破坏整个框架(包括小部件)
self.update() - 在alphafun的开头和alphacheck之后
创建单独的create_widget函数后self.destroy()
def create_widgets(self):
mathButton = Button(self, text="Numbers", command=self.math)
letterButton = Button(self, text="Letters", command=self.alphafun)
mathButton.place(x=50, y=250)
letterButton.place(x=250, y=250)
def init_window(self):
self.master.title("Learning is fun!")
# Style().configure("TFrame", background="#333")
self.pack(fill=BOTH, expand=1)
self.create_widgets()
def alphafun(self):
self.destroy()
self.create_widgets()
=====================编辑解决方案:
所以我在init_window中创建了一个标签:
def init_window(self):
self.master.title("Learning is fun!")
Style().configure("TFrame", background="#93F")
self.pack(fill=BOTH, expand=1)
mathButton = ttk.Button(self, text="Numbers", command=self.math)
# mathButton.config(bg="white")
letterButton = ttk.Button(self, text="Letters", command=self.alphafun)
mathButton.place(x=50, y=250)
letterButton.place(x=250, y=250)
### The question label
self.label_question = Label(self, text = '')
self.label_question.place(x=150, y=0)
self.label_question.pack()
self.label_question.configure(font=("Courier", 16), background='#93F')
### The answer label
self.label_answer = Label(self, text='')
self.label_answer.place(x=150, y=50)
self.label_answer.pack()
self.label_answer.configure(font=("Courier", 10), background='#93F')
在检查答案后,它会更新相应的标签:
if self.user_input.get() == self.x:
### updates the label_answer Label in the above code
self.label_answer.configure(text = f"\n{random.choice(win)}")
self.correct_answers += 1
### and because of the SO help, I could add a counter for correct guesses too (label not shown above, but it's the same as the other two)
self.label_counter.configure(text=f"Correct answers: {self.correct_answers}")
self.alphafun()
并在此处更新问题标签:
def alphafun(self):
self.x = random.choice(alphabet)
### Right here, updates the corresponding label
self.label_question.configure(text=f"Type this letter: {self.x}")