简体   繁体   中英

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. 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. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM