我在 python 中使用 tkinker 编写了一个脚本。当我运行脚本时,它会收到一个输入并将其打印到控制台。它工作得很好。
我想做的是以这样的方式向我现有的脚本添加任何功能,以便在我按下 get 按钮时填写完 inputbox 后,它将在控制台打印值并自动退出。再一次,我现有的脚本能够打印值。我需要在打印完成后立即退出该按钮。对此的任何帮助将不胜感激。
到目前为止,这是我尝试过的:
from tkinter import *
master = Tk()
e = Entry(master)
e.pack()
e.focus_set()
callback = lambda : get_val(e.get())
get_val = lambda item: print(item) #this extra function is for further usage
Button(master, text="get", width=10, command=callback).pack()
master.mainloop()
这是输入框的样子:
请您参考如下方法:
将回调函数修改为:
def callback():
get_val(e.get()) #Gets your stuff done
master.destroy() #Breaks the TK() main loop
exit() #Exits the python console
Here,
master.destroy()breaks themaster.mainloop()loop and thus terminates the GUI and finallyexit()makes it exit the python console.
