Tkinter Introduction

Learn about dialogs as well as the Tkinter structure, which comes bundled with the standard Python distribution.

Introduction to Tkinter

There are several GUI (Graphical User Interface, pronounced “gooey”) systems that can be used with Python. Here, we’ll mainly focus on Tkinter, which comes bundled with the standard Python distribution.

Tkinter is the standard GUI library that comes preinstalled with Python and provides a very simple and easy to use interface that allows users to create GUI applications. It has a wide collection of widgets, including buttons, menus and textfields, each of which can help users create and build GUI elements.

Dialogs in Tkinter

Many programs do not require a full GUI, just a dialog box or two. Tkinter provides a number of these. To use them, import messagebox, simpledialog, and/or filedialog from Tkinter.

Message boxes

Message boxes are used to display messages along with a title using Tkinter’s messagebox dialog box. The following includes a list of messagebox types:

  • messagebox.showinfo(title, message)
  • messagebox.showwarning(title, message)
  • messagebox.showerror(title, message)

    All the above are essentially the same. The difference is which icon is displayed with each type. All provide for a title, a message, and an OK button.

  • result = messagebox.askyesno(title, question)

    This provides No and Yes buttons, which return False or True, respectively.

The following code creates four different message boxes: one with an informational message, one with an error message, one with a warning message, and one with a yes or no question:

Get hands-on with 1400+ tech skills courses.