如何重新编程 Tkinter Messagebox 模块中的 'Ok' 按钮

How can I reprogram the 'Ok' button in the Tkinter Messagebox module

我正在开发我的第一个 python GUI,我想在单击消息 'OK' 按钮后从代码中关闭所有以前的 windows =12=]

messagebox.showinfo('Access Granted', 'Your data has been retrieved.')

所以,如果你的 window 被称为 root,你会想首先定义一个函数来 'destroy' window

def closeWindow():
    root.destroy()

然后你想将该命令添加到按钮 -

btn = tkinter.Button(text="Click Me!" command=closeWindow)

如果您还有任何错误,请告诉我!

tkinter 对话框 return 表示用户单击内容的字符串,因此只需保存该值并在之后进行检查即可。然而,由于 showinfo 只给用户一个选项,它总是会 return "ok",所以没有必要检查这个值。显示对话框后调用您的函数即可:

def some_function():
    messagebox.showinfo('Access Granted', 'Your data has been retrieved.')
    root.destroy()
...
button = tk.Button(root, text="Quit", command=some_function)