Firstly, we are going to make a message box that is simple and will display just a text. Nothing that you can interact with, taking any inputs or pressing any buttons. Now, in the tkinter.messagebox class, there is a function called showinfo()
This function takes a couple of parameters. Some of them are optional, some of them are necessary. Firstly, it asks for the title of the window. Till now, the title for the window was Tk by default but now we can specify our own title. It is the text that is displayed on the top of the window beside the close button. Secondly, it asks for the text we want to be displayed on the message box.
tkinter.messagebox.showinfo('code in python', 'this is the tkinter tutorial')
Now we are going to make a message box that will give us a couple of options i.e., yes or no. we have to store it in a variable so that we can us it later in the program. The function used for this type of message box is askquestion(). Its first parameter asks for the window title. The second one asks for the yes or no question.
ans = tkinter.messagebox.askquestion('quiz', 'do you like python')
Now we can make a basic if statement. Make sure you have a ‘==’ sign. Never put is or ‘=’ sign.
if ans == 'yes': print("welcome to codeinpython.com")
Now, whenever you will press yes button, it will process the tkinter.messagebox.askquestion and analyze the answer and display the result accordingly.
Final Code – Message Box
Full program for message box is,
from tkinter import * import tkinter.messagebox var = Tk() tkinter.messagebox.showinfo('code in python', 'this is the tkinter tutorial') ans = tkinter.messagebox.askquestion('quiz', 'do you like python') if ans == 'yes': print("welcome to codeinpython.com") var.mainloop()