Python Tutorial: How to create a Python GUI using Tkinter

The following is a short article on the basics of creating graphical user interfaces (GUI) in Python by leveraging Tkinter (interface to the Tk GUI toolkit). We’ll cover fundamentals such as creating a brand new window, centring the window on the screen, sizing it to desired dimensions, as well as giving the window a title and configuring it with its own icon. We also cover the basics of placing a new label and button on the window and wiring a new function that changes the text of our label with each button click.

Here is a video that demonstrates the entire process:

History of Tkinter

In 1988, the Tcl (Tool Command Language) programming language was created. It was cross-platform, dynamic, open source. However, it was missing GUI capabilities, so in 1991 a new Tk extension came to Tcl, enabling programmers to build GUIs (graphical user interfaces) natively in Tcl. That’s the reason you see Tcl often written as Tcl/Tk. Now, Tk was awesome, as it provided Tcl developers with all of the basic widgets such as canvas, textbox, button, label, etc. , needed for the development of native look/feel apps in Windows, Linux, Mac OS, Unix. At that time, we’re talking pre-year-2000, Python was also in the stage where it was mostly used without GUI. So, in 1999, Fredrik Lundh wrote a Tkinter (TK + Interface) as a Python binding/interface to the Tk GUI toolkit and shortly after the year 2000,  Tkinter became so popular that now it’s de facto a standard Python GUI package. As of Python 3+, Tkinter is actually shipped as part of the Linux, Windows and Mac OS installs of Python.

How to create GUI (a new window) using Tkinter

Here is the code with explanation:

Initialize Tkinter:

Initialize the main window

Size windows to 400 x 400px

Give a window a new title

Set Icon

Show Window is done through the following command:


Centering Window on the Screen

Following is the function to find the screen dimensions, calculate the center and set geometry:

To center the window on the screen, simply call your function like this:


Placing Label and Button on the Window

Now, let’s place a ‘My Label’ label on the window at coordinates x=10 and y=10 (top right-hand corner)

Placing Button uses a similar approach:


Handling a Button Click

Now, let’s create a new function that changes the text of our label with each button click, increasing the counter with each press of the button.

We need to create a new ‘counter’ variable outside of our function first:

Define button press function

Then add the function to the button command:

Now, whenever we press the button, we’ll see the label changed and our counter increasing


Source Code

The source code is on Github:

And also here:



Facebook Comments