pythontkinter

Can I call a function by clicking 'OK' in messagebox.showinfo?


I have added a message box in my code and I want to call a function defined earlier when clicking the 'OK' button in the message box. Is there any way to achieve this? Thanks in advance.

messagebox.showinfo('Correct', 'Correct!\nClick OK to continue')

Solution

  • When you make a showinfo box, it actually halts your script until you press OK. All you will need to do is put the function you want right after it.

    See this example:

    import tkinter as tk
    from tkinter import messagebox
    
    def go():
        messagebox.showinfo('Correct', 'Correct!\nClick OK to continue')
        print('yeah')
    
    root = tk.Tk()
    
    btn = tk.Button(root, text='click', command=go)
    btn.pack()
    
    root.mainloop()
    

    If you run that, you will see the print('yeah') only happens when the user clicks OK. So, you can do something similar and call the function where I have put the print.