我的问题是,我第一次运行程序时它运行正常,但是当我修改我的查询并点击“Go!”时再次按钮,没有任何反应。我希望它也这样做,这样当我输入一个新的查询时,它会重新加载文本框,其中包含与上一个查询相对应的信息。
from Tkinter import *
root = Tk()
#Here's an entry box
search_label = Label(root, text="Enter search here:")
search_entry = Entry(root)
search_label.pack()
search_entry.pack()
#This happens when you hit "go!"
def go():
#It opens a text box in which the answer required is written.
query=search_entry.get()
bibliography = Text(root)
bibliography.insert(INSERT, answer_box(query))
bibliography.pack()
#This is the "go!" button
go_button = Button(root, text="Go!", width=10, command=go)
go_button.pack()
root.mainloop()
一些想法?
答案 0 :(得分:1)
每次单击按钮时,您的代码都会创建文本小部件。而不是它,只创建一次文本小部件。然后,清除它并插入答案文本。
from Tkinter import *
root = Tk()
search_label = Label(root, text="Enter search here:")
search_entry = Entry(root)
search_label.pack()
search_entry.pack()
def answer_box(query):
return query
def go():
query=search_entry.get()
bibliography.delete('1.0', END)
bibliography.insert(INSERT, answer_box(query))
go_button = Button(root, text="Go!", width=10, command=go)
go_button.pack()
bibliography = Text(root)
bibliography.pack()
root.mainloop()