Starting Tkinter – Creation Of Basic Window

March 30, 2018

Anindita Guha

How do you codify a whole application and make people use the complex programs naively? The only light in the darkness is Graphical User Interface. But, though it is easy to use, they are not easy to make. A poor GUI make applications appear naught but hideous.
Perceiving the importance of GUI development, the need for it can’t be refuted. So, we have come up with a series of GUI development which replete with the concepts of GUI development, from scratch to advanced version.


Which Library?

We are going to use the Standard library of python to fulfil our purpose i.e. Tkinter. There are some other libraries available like Wxpython , PyGTK+,PyQT and many more but Tkinter has more notable pros over others. It’s layered approach, simplicity, portability and availability makes it a better alternative for GUI development.
We shall get going with the first program of the series that is concerned with creating a basic window of Tkinter.

The Code


Description


Very first thing we need to do is obvious, importing the module. There are 3 ways of importing the module.
The first one is import Tkinter. All the functionalities of the module can be invoked as Tkinter.function().


The second way of importing is import Tkinter as tk. All the functionalities can be invoked as tk.function().We are going to use this way in our program.


The third way is From Tkinter import *. Importing each and every module directly from Tkinter and consequently all the functionalities are thereby invoked directly with the function name such as function()


The crucial aspect that you need to keep in mind while importing the Tkinter module is that, if you are using Python 3.x version in your IDE then make sure you import tkinter instead of “Tkinter”, else it will throw error, rest functionalities are all the same.

As soon as we are done importing the module, we leap forward towards creating a window. We create the window using tk.Tk() function.


Here Tk() is called in order to create the window, subsequently which is being saved in a variable called main. You may give the variable any name you wish.

There after we must get habituated typing this piece of code immediately variable.mainloop()


Why this? Since we are working with the GUI, it is important that whatever has been created remains on the screen, and visible to users, until and unless any user interruption is encountered. Consequently, the mainloop function makes sure that the loop is invariable, so that the GUI or graphics can be viewed.
The next basic steps for creating a user centric window is to set the size of the window, i.e the geometry in technical terms. Later, we provide a name to the window , by making use of the functionality title.


A thing to ponder upon is that, why every time we are using main, and invoking the functions in this manner main.function() ?
It is so because main is the root window where all the changes has or will be made. In similar manner, creation of many frames is viable. Every other frame or window will have different names on which disparate changes can be made.

Here is a snapshot of the window created.