The library

Python, being a very powerful programming language provides many different libraries to create GUIs or Graphic User Interfaces. Some of them are :-

We have a lot of options to choose from and in this guide we will be going through Tkinter. Tkinter is a powerful python library that allows users to develop GUIs in a very fast and effective way. It seems a bit tricky but i am sure if you go through this guide well, you will be able to understand all the basics and build your own GUIs. So lets get started.

Installing Tkinter

Here's the command for anaconda distribution - install -c anaconda tk

Copy paste this in the command prompt and you are good to go.

Getting started

Let's take a look at some basic but very important tkinter widgets which we will use later on in this guide to make some projects.

1) Window creation - Our very first step is creating a window in which we will be carrying out our different tasks.

We have successfully created the window with the “tk.Tk()” and the mainloop is something that runs in the background and helps in keeping the window open unless we close it manually. Our window looks like this -

2)Buttons - Now that we have our window, it's time to add some widgets in it to make it look a bit interactive. We will be adding a button to our window which won't do anything at the moment. Here's how you do it:-

The result looks something like this :-

Don’t get affected by the “.grid” method in the above photo, we will talk about what grids are and what they do later when we talk about alignment and other stuff. But until then we have our very basic button ready. It's not very attractive at the moment but it does the job. Next let's take a look at labels.

3)Labels - These act as a display box that can display one line messages or in some cases even images. Let's take a look at how to implement them -

So, above we define a method “displaymessage” in which we create a label that just displays text for us. We use the button that we created previously and pass the method displaymessage in it in order to make the label work and print our text. Command is what helps us pass the method in the button.

The results are as follows :-

4)Title and Window Icon - We want our window to have a title that we would like to see.So lets replace the “tk” and the icon in our window. That can be done as follows :-

We use the “.title” method to change our window title and we use the “iconbitmap” method to change the window icon. An important point to remember while changing the window icon/logo we need to use a “.ico” format image for it to work.

Here are the results:-

5)Entry - In this we will see how the input fields work in Tkinter and also look at the geometry of our window in order to get a better understanding of placement of our widgets. Let's first see how the Entry works -

Now it looks like there's a lot going on in this so let's break it down step by step. Here we are trying to create a simple login window. All it does is , takes input of username and password from the user and we can click the button to login. Easy. Alright , now let's take a look at the labels and entry. We have defined variables ‘label’ and ‘label1’ that display the message ‘username’ and ‘password’ respectively for the user to enter. We then defined variables ‘entry’ and ‘entry1’ to take the inputs from the user. ‘Entry’ is a method that helps us incorporate an input section in our window. And then we have our button which we have talked about previously. Now let's talk about the geometry of our window -

So now that we are clear with our window geometry and placement of our widgets , let's take a look at the results of our simple login window.

6)Check-Button - These basically work as buttons that the user can toggle between. Its basically a check box. A user can choose multiple options at once. Here's how you can make your own checkbutton/checkbox -

And the final result looks something like this -

7)Menu - The last thing that we will take a look at is our Menu Bar that is functional. So here's how you make it.

The "add_cascade()" method creates a new menu by associating a given menu to a parent menu. The "add_command()" adds an item to the menu. The "add_separator()" method adds a separator line to the menu.

Here's the final Result -

Congratulations! These are pretty much all the methods that you need to know in order to make a fully functional GUI that actually does something. There are tons of other methods but to make a fully functional GUI these are sufficient.

Reference Links

Had some trouble understanding the above concepts? Here are some links to help you out.

Now that you are done with learning why not apply it? Head over to the Projects Section to do some projects and sharpen you tkinter skills.

Projects Section Home