我有以下代码,我认为不会这样做,但我只是初学者。有人可以解释一下如何在进入下一个之前让txt清除。如果我打字,我会得到正确的回复,但我希望在得到下一个回复之前清除它。
import tkinter
def show_data():
txtBox = ent.get()
if txtBox == ("hi" or "hello"):
txt.delete(0.0, END)
txt.insert(0.0, sentense)
elif txtBox == ("i am good"):
txt.delete(0.0, END)
txt.insert(0.0, sentense1)
else:
txt.delete(0.0, END)
txt.insert(0.0, sentence3)
window = tkinter.Tk()
window.configure(background="#18e0d9")
window.title("Project One")
window.geometry("300x300")
sentense = ("Hi! How are you?")
sentense1 = ("I'm glad to hear it")
sentence3 = ("i dont understand")
photo = tkinter.PhotoImage(file = 'title.gif')
w = tkinter.Label(window, image=photo)
lblInst = tkinter.Label(window, text="HOW CAN I HELP YOU TODAY?",
fg="#383a39", bg="#18e0d9", font="bolder")
ent = tkinter.Entry(window, width=38)
btn = tkinter.Button(window, text="submit", fg="#ffffff", bg="#383a39", command=show_data)
txt = tkinter.Text(window, width=29, height=6, bg="#18e0d9")
txt.pack(side="bottom", pady=15)
w.pack()
lblInst.pack(pady=5)
ent.pack(pady=5)
btn.pack(pady=5)
window.mainloop()
答案 0 :(得分:0)
正确使用语法,如下所示:
text = tkinter.Text()
text.insert('1.0', 'template') # add template from the start point
text.delete('1.0', 'end') # delete from start to end, entirely