Back to: Python Programming
Creating a Graphical User Interface (GUI) with tkinter
Tkinter is a binding to the Tk GUI toolkit for Python. It is the standard Python interface to the Tk GUI toolkit, and is Python’s de facto standard GUI. Tkinter is included with standard Linux, Microsoft Windows and macOS installs of Python.
To use tkinter we first import the module as our first line of code from tkinter import * – this will import everything related to the tkinter module so we can use all of the GUI features that this module has to offer.
One important distinction we need to make is the difference between windows and widgets:
Windows are the containers in which all other GUI elements live.
Widgets are other GUI elements, such as text boxes, images, labels, and buttons.
Our first window
We’ll begin by creating a simple window. We should give our window a unique name like window. In order to instantiate this window we use window = Tk(). In order to display our window we use window.mainloop().
from tkinter import *
window = Tk() # <-- instantiate an instance of a window
window.mainloop() # <-- place window on screen, listen for events
We now have now created our first window. The window.mainloop() displays a window and then listens for an event.

Customising the appearance of our window
To change the size of the window we use the geometry function and specify the size in pixels.
To change the title of the window from the default “tk” in the top left next to a feather image, we use the title function.
To change the feather icon to use an image of our choosing (like a logo), use the iconphoto function after first moving a square image into the project folder and converting it to a python PhotoImage first. The example below demonstrates this process, but effectively we create a new icon variable from the image then pass this variable into the iconphoto function.
Finally we can change the background colour of the window using the config function. We can use the hex value, or many colours mentioned by name will work.
from tkinter import *
window = Tk() # <-- instantiate an instance of a window
window.geometry("600x420") # <-- sets the window to 600 x 420 pixels
window.title("NSC - Software Development") # <-- places a name in the header bar
icon = PhotoImage(file='norwood-logo-sq.png') # <-- converts the PNG file
window.iconphoto(True,icon) # <-- displays the converted PNG image
window.config(background="#caa7e6") # <-- changes the window background colour
window.mainloop()
Outputs a configured window:

Create a GUI app with Tkinter – Step by Step Tutorial – (23m:55s)