【发布时间】:2016-03-03 15:41:05
【问题描述】:
这里是 Python 的新手。 我正在编写这个简单的 GUI:
from Tkinter import *
root = Tk()
def print_name():
return 'Here is Steve'
here_goes_text = print_name()
button = Button(root,text='Say hello',command = print_name)
button.pack()
text = Text(root)
text.insert(END,here_goes_text)
text.pack()
root.mainloop()
我希望在我点击“打个招呼”按钮后,Gui 会在下面的文本小部件上显示“嗨,我是史蒂夫”。 不幸的是,现在当我运行该程序时,它会立即在下面的文本中显示“嗨,我是史蒂夫”,如果我点击“打个招呼”按钮,什么也不会发生。
我做错了什么?
【问题讨论】:
-
它会立即显示,因为您正在立即插入它 (
text.insert(END,here_goes_text)) -
嗨,布莱恩·奥克利,好的,我明白了。但是我还是不知道怎么解决这种问题,
-
将在按钮单击时插入文本的行移到单击按钮时调用的函数内。此外,您不能期望在事件之后返回任何值。
标签: python-2.7 user-interface tkinter