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)

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

  4. I intended to post you this bit of note in order to give thanks once again over the splendid solutions you have featured in this case. This has been really wonderfully generous of you to provide extensively precisely what a lot of people could possibly have offered for sale for an e-book to generate some dough on their own, specifically seeing that you could possibly have done it if you wanted. These pointers also acted like a fantastic way to realize that some people have the same zeal like my personal own to realize very much more with respect to this issue. I believe there are millions of more pleasurable moments in the future for individuals who go through your site.

  5. Ꮋi theгe, yes this piece of wriing is genuinely pleasant ɑnd Ι havе learned ⅼot of things frоm it regarding blogging.
    tһanks.

  6. “I am just starting to learn about all of this. Thanks for your help!”

  7. This design is steller! You most certainly know how to keep a reader
    entertained. Between your wit and your videos,
    I was almost moved to start my own blog (well, almost…HaHa!) Fantastic
    job. I really loved what you had to say, and
    more than that, how you presented it. Too cool!

  8. Thank you for this informative read, I have shared iit on Facebook.

  9. I can’t believe how great this site is. You keep up the good work. That’s my advice pal.

  10. Just fabulous. Who wrote this and how can we get more?

  11. “Great post, thanks for the_ information!”

  12. Sorry for the huge review, but I’m really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it’s the right choice for you.

  13. hey there and thank you for your information – I’ve definitely
    picked up something new from right here. I did however expertise some technical issues using this site,
    since I experienced to reload the site many times previous to I
    could get it to load correctly. I had been wondering if your web hosting is OK?
    Not that I am complaining, but slow loading instances times will often affect your placement in google and could damage your quality score if ads and
    marketing with Adwords. Well I’m adding this RSS to
    my e-mail and could look out for much more of your respective interesting content.

    Make sure you update this again very soon.

  14. Wow, beautiful portal. Thnx …

  15. I all the time emailed this web site post page to all my associates, as if like to read it
    afterward my contacts will too.

  16. Pretty section of content. I just stumbled upon your
    weblog and in accession capital to assert that I acquire in fact enjoyed account
    your blog posts. Anyway I will be subscribing to your feeds and even I achievement you access consistently fast.

  17. Greetings! I know this is kind of off topic but
    I was wondering which blog platform are you using
    for this site? I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking at alternatives for another platform.
    I would be awesome if you could point me in the direction of a good platform.

  18. Hi there friends, how is the whole thing, and what you want to say on the topic
    of this article, in my view its actually awesome in support of me.

  19. Magnificent beat ! I would like to apprentice at the same time as you amend your website, how could i
    subscribe for a blog website? The account aided me
    a acceptable deal. I had been tiny bit acquainted of this
    your broadcast offered shiny transparent idea

  20. Hello! This post couldn’t be written any better! Reading this
    post reminds me of my good old room mate! He always kept
    talking about this. I will forward this page to him. Pretty sure he will have a good read.
    Thanks for sharing!

  21. Great blog you have got here.. It’s hard to find quality writing like yours nowadays.
    I truly appreciate people like you! Take care!!

  22. Your means of describing all in this paragraph is actually nice, all be
    capable of easily know it, Thanks a lot.

  23. Hi! Do you use Twitter? I’d like to follow you if that would be ok. I’m undoubtedly enjoying your blog and look forward to new updates.

Leave a Reply

Your email address will not be published.

*

© 2018 Code in Python