Python Tkinter Message Boxes

Need to pop up a message box for your python program? Try tkinter. It has a built in widget for providing output details for a program. Check out our first tkinter tutorial for how to do basic setup before you get started with this message box tutorial.


Step 1: Import the tkinter and tkinter messagebox modules.

import tkinter as tk
import tkinter.messagebox as mb

Step 2: Create some message boxes. There are several different types of messages boxes that can be created: Information, Warning, Error, and Questions. See the code and result below.

mb.showinfo('Hey, Dummy','This is an information box.')
mb.showwarning('Warning, Dummy', "Warning, you're about to break something!")
mb.showerror('Error, Dummy', 'Error, I told you not to do that, now its broken')
mb.askyesnocancel('Dummy', 'Okay do you want me to fix it?')
Python tkinter Message Boxes

There are a few other options as well, check them out at the python.org docs.


As always, here is the full code:

import tkinter as tk
import tkinter.messagebox as mb

mb.showinfo('Hey, Dummy','This is an information box.')
mb.showwarning('Warning, Dummy', "Warning, you're about to break something!")
mb.showerror('Error, Dummy', 'Error, I told you not to do that, now its broken')
mb.askyesnocancel('Dummy', 'Okay do you want me to fix it?')

That’s it! You’ve successfully created message boxes in python using tkinter. There are more details we’ll cover in our next few posts about how to get input from a message box, how to read which of the buttons was clicked, and how to cause a message box to pop up when you click something in an application window, so stay tuned!

John

Leave a Reply

Your email address will not be published. Required fields are marked *