无法使按钮(使用tkinter创建)自动退出

2024-09-27 07:34:40 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这就是inputbox的样子:

enter image description here


Tags: lambdafromimport功能master脚本gettkinter
2条回答

维护lambda语法:

callback = lambda : (print(e.get()), master.destroy())

关键是调用master.destroy()。你知道吗

callback函数修改为:

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 the master.mainloop() loop and thus terminates the GUI and finally exit() makes it exit the python console.

相关问题 更多 >

    热门问题