我正在尝试为用户创建一个简单的弹出文本条目,用户在其中输入文本并点击提交(按钮) . 单击提交后,我希望弹出输入框关闭并继续执行其余代码 . 以下是我从an old post here借来的显示示例代码:
from Tkinter import *
root = Tk()
nameLabel = Label(root, text="Name")
ent = Entry(root, bd=5)
def getName():
print ent.get()
submit = Button(root, text ="Submit", command = getName)
nameLabel.pack()
ent.pack()
submit.pack(side = BOTTOM)
root.mainloop()
print "Rest of the code goes here"
我没有't have much experience with Tkinter so I am not sure where and how exactly to call the appropriate functions for closing the entry box after the user hits '提交' . 我的猜测是它必须在 getName()
函数内?
2 回答
替代方法:
因为你的弹出窗口没有多少你可以在你的GUI中消除几行代码,节省一些CPU并获得几乎相同的输出:
相同的功能,更快,更清洁 .
只是一个想法 .
如果我理解正确,那么您需要做的就是在
getName
函数末尾调用根窗口的destroy方法:这样做相当于手动单击窗口一角的X按钮 .