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.
JavaScript
x
2
1
messagebox.showinfo('Correct', 'Correct!nClick OK to continue')
2
Advertisement
Answer
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:
JavaScript
1
14
14
1
import tkinter as tk
2
from tkinter import messagebox
3
4
def go():
5
messagebox.showinfo('Correct', 'Correct!nClick OK to continue')
6
print('yeah')
7
8
root = tk.Tk()
9
10
btn = tk.Button(root, text='click', command=go)
11
btn.pack()
12
13
root.mainloop()
14
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.