2013-09-21 189 views
0

我的问题是,我第一次运行该程序时,它的工作正常,但是当我修改我的查询并点击“Go!”时按钮,没有任何反应。我希望它做了同样的事情,以便当我输入一个新的查询时,它重新加载了与最后一个查询相对应的信息的文本框。Tkinter只能工作一次

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() 

有些想法?

+1

'answer_box' is undefined - 我认为这不是您的完整代码?另外,当我用'query'替换'answer_box(query)'时,我可以运行它,但重复使用'go_button'会导致新的文本框被添加到下面的新查询文本中。你确定没有发生?它会发生在你的屏幕上吗?另外,你是否打算在每次点击'go_button'时创建一个新的文本框? – Brionius

回答

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()