/  Technology   /  Building Desktop Application(GUI) Using Python and Tkinter
Building Desktop Application(GUI) Using Python and Tkinter

Building Desktop Application(GUI) Using Python and Tkinter

In this article we will walk through the process of creating a GUI app in python. you will learn about the elements needed to develop the GUI app in python so let’s begin by checking out what GUI actually is.

 

What Is Graphical User Interface?

 

The graphical user interface is a desktop application that helps you to interact with computers. It is a type of user interface that allows users to interact with electronic devices through graphical icons and visual indicators. They are used to perform different tasks in a desktop laptop or any other electronic device. GUI apps like text editors can be used to create, read,  update, or delete different types of files. GUI app like chess, solitaire are games which you can play and app like chrome, firefox is used to surf the internet. so there are different types of GUI app which we daily use on our laptop or desktops.

 

Python Libraries For GUI

 

Python provides a variety of libraries to build a GUI app tool as python qt, Tkinter, pygame,  kivy. In this article, we will be using Tkinter

to build a GUI app.

 

Tkinter

 

Tkinter is a built-in library python which is used to create a simple GUI app. It is a standard  GUI library for python. It is preferred by a lot of learns and developers because of its simplicity. IT gives us an object-oriented interface to the TK GUI toolkit.

 

Now let’s build a very simple GUI and understand various components of tkinter.

 

1]First you will have to import a tkinter module

the next step is to create a window that will contain widgets. The window is the foundational element of a tkinter GUI. It contains all

GUI elements such as text boxes, labels buttons known as widgets.

import tkinter as tk
window =tk.TK ()

with you will run the code a new window will pop up on your screen which will look like this

 

Output :

 

Building Desktop Application(GUI) Using Python and Tkinter

Next step is optional if you want to rename the title of the window you can use

window.title(‘getting started with tkinter’)

 

Output :

 

Building Desktop Application(GUI) Using Python and Tkinter

 

 Adding Widget

 

Now we will add a widget called label which helps to insert into the window.

label = tk.Label(text="welcome to i2 tutorials")
label. pack()

 

Output:

 

Building Desktop Application(GUI) Using Python and Tkinter

we created a label widget and then we use .pack() method to add it to window screen.

we can also control label text and background colors using the foreground and background parameters

label =tk.Label(text="hello, Tkinter",foreground='white', background='black')

 

Output :

 

Building Desktop Application(GUI) Using Python and Tkinter

Entry Widget

 

when you want to get input from the user like name, the email address we can use Entry widget. you can use the entry widget as

ent=tk.Entry (window,width=30)
ent.pack()

Output :

 

Building Desktop Application(GUI) Using Python and Tkinter

 

Button Widget

 

The button widget is used to display click on the button. It can be created as

button=tk.Button(text='click here')
button.pack()

Output:

 

Building Desktop Application(GUI) Using Python and Tkinter

you can also modify or control the text, background color and size of a button as we did it with label widget by using the

parameters such as width, height, bg, fg.

button=tk.Button(text='click here', width=20,height=5, bg='yellow', fg='red')

Let us understand different widgets that Tkinter provides

  • Canvas : The canvas widget is used to draw shapes such as lines, ovals, rectangles, polygon.
  • CheckButton : CheckButton widget is used to display number of options as checkboxes and you can select  multiple options from it.
  • Frame : Frame widget is used as container for grouping and organizing the widgets.
  • Radiobutton : The Radiobutton widget is used to display various options and user can select only one options out of it.
  • Menu : Menu widget is used to create menus in GUI.

 

Binding Functions

 

Binding functions are the functions which are called whenever a event occurs. It can be implemented as follows

import tkinter
window = tkinter.Tk()
window.title("GUI")
def i2Tutorial():
tkinter.Label(window, text = "GUI with Tkinter!").pack()
tkinter.Button(window, text = "Click Me!", command = i2tutorialTutorial).pack()
window.mainloop()

Output :

 

Before Clicking

Building Desktop Application(GUI) Using Python and Tkinter

After Clicking

Building Desktop Application(GUI) Using Python and Tkinter

Here we have defined a button which on clicking calls the function i2Tutorial and displays message.

In the above code we have used mainloop() method which is nothing but event loop which tells the code  to keep displaying the window until we manually close it.

 

Image and Icon

 

You can also add images or icons in your app by using PhotoImage method. It can be implemented as follows

import tkinter
window = tkinter.Tk()
window.title("i2tutorials")
img = tkinter.PhotoImage(file = "Pictures//steve.jpg")
photo = tkinter.Label(window, image = img)
photo.pack()
window.mainloop()

 

Grid

 

Grid is used to organize the widgets. It uses matrix row-column concept. It takes two parameters one for row and other one for column. Let’s try to learn by taking example

import tkinter
window = tkinter.Tk()
window.title("i2tutorial")
tkinter.Label(window, text = "Username").grid(row = 0)
tkinter.Entry(window).grid(row = 0, column = 1)
tkinter.Label(window, text = "Password").grid(row = 1)
tkinter.Entry(window).grid(row = 1, column = 1)
tkinter.Label(window, text = "Have a Nice Day").grid(columnspan = 2)
window.mainloop()

Output :

 

Building Desktop Application(GUI) Using Python and Tkinter

In this article we have covered basics of Tkinter required to build an GUI app. Hope it will help with getting started with your own app GUI creation.

Leave a comment