Creating a GUI

Python has a lot of modules that can be used to create GUIs, but the one we'll be using today is called Tkinter, as it is included with the standard Python download.

If you've already followed the instructions in earlier challenges to download and install Python, then you can already use Tkinter. Otherwise, you can find those instructions here. This challenge is based on a wonderful tutorial from Real Python, which you can find here.

Tkinter has several strengths. It's cross-platform, meaning the same code will work on a variety of different operating systems - it is compatible with both Windows and Mac, for example. Visual elements of the Tkinter GUI use built-in operating system functions, so apps built with Tkinter look like they belong on the system where they're run.

The most basic part of a Tkinter GUI is the window. Windows are the containers in which all other GUI elements live. These other GUI elements, such as text boxes, labels, and buttons, are known as widgets. Widgets are contained inside of windows.

First, create a window that contains a single widget. Open up the Python IDLE and follow along!

With your Python shell open, the first thing you need to do is import the Python GUI Tkinter module:

>>> import tkinter as tk

A window is an instance of Tkinter’s Tk class. Go ahead and create a new window and assign it to the variable window:

>>> window = tk.Tk()

When you execute the above code, a new window pops up on your screen. How it looks depends on your operating system:

Throughout the rest of this challenge, we'll use Windows screenshots.