< Prev  |  Home  |  Next >

Creating a GUI in Tkinter

By now, you’re (hopefully) quite good at solving problems in Python, but you’re probably wondering why all of your programs are text-based. Well, now is the time to work out how to create a Graphical User Interface or GUI.

We’ll be achieving this by making use of a module called Tkinter, and in this first part, we’ll just be making a window look all nice.

You can create an (admittedly dull) GUI window in just 3 lines of code. Specifically, these 3 lines:

Here is what you should see:

An empty GUI

Note: This example was made in Python 3.3. For those of you living in the past (i.e. using an earlier version of Python) just use a capital ‘T’ in the import line:

Or, if you want to be able to easily copy my examples, then just rename the module by using the ‘as’ keyword:

There’s 2 things I don’t like about the resulting window you see above; the title and the window icon. We’ll change both of these things now.

Firstly, let’s change the title of this window with the following line:

Make sure to put this line before the line that draws the window, and you’ll see your title:

Another empty GUI window

But hang on, we can’t see all of the title, so we need to make the window a bit bigger, like this:

All that remains now is to change that ugly ‘tk’ icon in the top-left corner. Creating .ico icon files is easily done. You can either just create a .png file in something like Photoshop and rename it as a .ico file, or just go somewhere like Online Icon Maker to create and download one. Then just make sure that that the .ico file is in the same place as the .py file and add the following line to your GUI program:

I’ll be honest, I’m not great at art. I tried to draw a skull and crossbones but it ended up looking like an alien doing a star-jump. Here’s my final code and displayed window:

Super-cool GUI window


Go to Here to draw your own .ico file and add it to your own window.

< Prev  |  Home  |  Next >