Code in Python
Machine Learning and Deep Learning Blogs
search

How to create a Message Box in Tkinter

In this chapter, we will learn how to make Tkinter message box. It is equivalent to alert in javascript, pop-ups that appear on the screen. They can either give you a plain text, like a piece of information or they, can also ask you a question like “do you want to leave this site”, yes or no. To create a message box, the first thing we have to do is to import tkinter.messagebox.

import tkinter.messagebox 

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() 

message box 1

message box 2

Rahul Choudhary
Follow me

Rahul Choudhary

Rahul is a Python enthusiast. He likes to explore whatever is new in Python world. Rahul is a car fanatic and a serial chiller. Follow him on Facebook and Twitter.
Rahul Choudhary
Follow me

Latest posts by Rahul Choudhary (see all)

4 Comments

  1. Howdy! Do you know if they make any plugins to assist with SEO?
    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains.
    If you know of any please share. Kudos!

  2. I adore reading through and I think this website got some really utilitarian stuff on it! .

  3. I would like to show my appreciation to you for rescuing me from this type of incident. After searching through the the web and finding proposals which were not productive, I thought my entire life was over. Being alive without the presence of strategies to the problems you have sorted out by means of your entire short article is a crucial case, and the ones that might have in a wrong way damaged my career if I had not encountered your web site. Your actual know-how and kindness in controlling all the stuff was very useful. I’m not sure what I would have done if I had not encountered such a solution like this. I can also at this time relish my future. Thank you so much for this high quality and results-oriented guide. I will not hesitate to propose your web sites to any individual who requires support about this area.

Leave a Reply

Your email address will not be published.

*

© 2018 Code in Python