The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems. It helps in making the GUI based window for any application.

In this tutoarial will look at some of the general GUI using tkinter. It comes already installed in Python. For the whole series I used Python3. It also works in Python2 but there is a difference in captitalization. For Python2 it is-:

import TkinterIn [2]:

from tkinter import *

Procedure for creating a GUI using tkinter -:

  1. Import the module tkinter.
  2. Have to create a tk object -> This creates a toplevel widget of Tk which usually is the main window of an application.
  3. Create a window class which is defined by all the functions you want to create in that GUI.
  4. mainloop() -> It will keep your GUI alive untill you perform any event.

Let’s look with the help of examples -:

Simple demo of how to create a GUI based window using Tkinter

In [2]:

## Creating own class
## In that calling the Frame class present in the tkinter module

class Window(Frame):
    
    def __init__(self,master=None):
        ## It is calling the parent method constructor i.e, Frame class here
        super().__init__(master)
        self.master = master

In [5]:

main_obj = Tk()
main_obj.geometry("400x300")

app = Window(main_obj)

app.mainloop()

Output of the above code is shown below. In this it will show the window GUI generated by tkinter module.

There are geometric managers in this module.

  1. place -> It will place your widget at the mentioned co-ordinates.
  2. pack() -> It organizes the widgets in blocks before placing in the parent widget.
  3. grid() -> It organizes the widgets in grid (table-like structure) before placing in the parent widget.

Creating widgets

Button

Adding buttons in the window.In [4]:

class Window(Frame):
    
    def __init__(self,master=None):
        ## It is calling the parent method constructor i.e, Frame class here
        super().__init__(master)
        self.master = master
        ## For geometry purpose
        self.pack()
        ## Calling a user-defined function.
        self.createbuttons()
        
    
    def createbuttons(self):
        
        ## Creating a button
        ## Given a text -> Quit
        ## Calling quit ecent.
        exit_button = Button(self, text='Quit', command = self.quit)
        
        ## Where to place this button
        exit_button.pack({"side": "left"})
        
        ## Creating another message button.
        ## Given a text -> 'Hello'
        ## Command -> It will call another function which is just printing some message.
        message_button = Button(self, text='Hello', command= self.say_hi)

        ## Where to place this button
        message_button.pack({"side": "right"})
        
    
    ## Function will be called by a button.
    def say_hi(self):
        print ("Hello! Everyone")
        

In [ ]:

main_obj = Tk()
main_obj.geometry("400x300")

app = Window(main_obj)

app.mainloop()

The output of above code is shown below and If you click quit button it will quit the window. If you will click the hello button then it will print the message.

As we saw in ide like File menu, Edit menu etc.In [3]:

class Window(Frame):
    
    def __init__(self,master=None):
        ## It is calling the parent method constructor i.e, Frame class here
        super().__init__(master)
        self.master = master
        ## For geometry purpose
        self.pack()
        ## Calling a user-defined function.
        self.createmenu()
        
    
    def createmenu(self):
        
        menu = Menu(self.master)
        self.master.config(menu=menu)
        
        ## Creating a file menu
        file = Menu(menu)
        ## Creating content for the file menu.
        file.add_command(label='Open',command=self.say_hi)
        file.add_command(label='Save',command=self.say_hi)
        ## Adding these buttons/content in the file menu
        menu.add_cascade(label='File',menu=file)
        
        ## Creating a edit menu
        edit = Menu(menu)
        ## Creating content for the edit menu.
        edit.add_command(label='Find',command=self.say_hi)
        edit.add_command(label='Undo',command=self.say_hi)
        ## Adding the buttons/content in the edit menu.
        menu.add_cascade(label='Edit',menu=edit)
        
        
    
    ## Function will be called by a button.
    def say_hi(self):
        print ("Hello! Everyone")

In [4]:

main_obj = Tk()
main_obj.geometry("400x300")

app = Window(main_obj)

app.mainloop()

Hello! Everyone

The output of above program is shown below i.e, it will create a menu bar and if you will click File menu then save and open option will pop up. Right now functionality is just printing a message but you can modify the code in order to make changes in functionality.