[英]How can I reprogram the 'Ok' button in the Tkinter Messagebox module
我正在使用我的第一个Python GUI,在单击消息的“确定”按钮后,我想从代码中关闭所有以前的窗口
messagebox.showinfo('Access Granted', 'Your data has been retrieved.')
The tkinter dialogs return a string representing what the user clicked on, so it's just a matter of saving that value and checking it afterwards. tkinter对话框返回一个表示用户单击的字符串,因此只需保存该值并随后进行检查即可。 However, since
showinfo
only gives the user one option it's always going to return "ok"
, so there's no need to check the value. 但是,由于
showinfo
仅为用户提供一个选项,因此它将始终返回"ok"
,因此无需检查该值。 Just call your function after the dialog has been displayed: 对话框显示后,只需调用您的函数即可:
def some_function():
messagebox.showinfo('Access Granted', 'Your data has been retrieved.')
root.destroy()
...
button = tk.Button(root, text="Quit", command=some_function)
So, say if your window was called root you would want to first define a function to 'destroy' the window 因此,假设您的窗口被称为根目录,则需要先定义一个“销毁”窗口的函数
def closeWindow():
root.destroy()
Then you'd want to add that command to the button - 然后,您想要将该命令添加到按钮中-
btn = tkinter.Button(text="Click Me!" command=closeWindow)
If you get any more errors, let me know! 如果您还有其他错误,请通知我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.