Python Message Box


In this section, you'll learn about string variables in Python. To make things a little more exciting, we'll use a message box instead of printing things out to the console.

Python has the ability to access code libraries. There are lots of code libraries and they do a wide range of things. One of the things that code libraries can do is to help you create graphic user interface elements, like user form, buttons, and message boxes. The GUI library that we're going to use is called TKinter.

You have to import libraries. This is nothing more complicated that typing to word import followed by which library you want.

To test this out, start a new project by clicking File > New Project from the menu at the top of PyCharm. In the Create Project dialogue box, type a name for your project in place of untitled. We've called ours PythonStrings:

The Create a  Python project dialog box

Click the Create button at the bottom to create your new Python project.

You now need a Python file to go in your project. Click the File menu in PyCharm again and select New. From the popup menu, select Python File:

The New Python File popup menu

You can also right-click the project name at the top of the PyCharm explorer window on the left:

Shortcut menu to create a new Python File

Type a name for your file. Call it StringVars and click OK.

When you get the blank file opening up, type the following:

from tkinter import messagebox

If this doesn't give you any red underlines, then you're using Python 3 and you're good to go. You can skip ahead to the Message Box section.

If you only have Python 2, however, try this line instead:

import tkMessageBox

If you're getting problem with tkinter being underlined in red, try an import statement on the first line, just before tkMessageBox:

import tkinter
import tkMessageBox

On some systems, you may even need a capital T on the first line: import Tkinter.

If you're using a Mac, then you may need an underline character at the start of tkinter, if you're using an older Python version:

import _tkinter

On Windows, some users have reported that even this doesn't work. If you hover your mouse over the red underlined tkinter, you may see a message about "unused import statement" or "unresolved reference". If you do, close your project down using File > Close Project. Now navigate to where you PyCharm projects are stored. The default location is:


Double click the PycharmProjects folder and locate the project folder you have just created:

Windows Explorer window showing PyCharm folder

Inside of this folder, locate the .idea folder:

Window Explorer showing .idea folder

Click on this folder then delete it.

Back in PyCharm, open up your project again. Hopefully, this will have cured the problem.

Tkinter Message Boxes

Let's see how the message box works. If you have version 3 of Python, type the following:

from tkinter import messagebox

messagebox.showinfo("Title Here", "Message Here")

For version 2 of Python, type this instead:

import tkMessageBox
tkMessageBox.showinfo("Title Here", " Message Here ")

Give it a try. Click the Run item on the menu at the top of PyCharm. From the menu that appears, select Run. You may get another popup box asking you to choose a configuration. Select the StringVars item. What you should see when you run your file is a white form in the background:

Blank Tkinter form

You should also see your message box (Mac users might see a feather icon in the dock area at the bottom of the screen. Click the feather to see your message box):

Default Tkinter message box

There are lots of ways to use the message box, and you'll meet some of them later in the course. For now, we'll just use showinfo after our message boxes.

messagebox.showinfo("Title Here", "Message Here")

messagebox.showinfo("Title Here", "Message Here")

In between the round brackets of showinfo we have two items, separated by a comma. The first item is used for the title of the message box and the second item is the message itself.


Now that you have a Tkinter message box, we'll use them with string variables in the next lesson.

Python String Variables >