Graphical User Interface (GUI) Programs in Python using tkinter Package

Graphical User Interface (GUI) Programs in Python using tkinter Package

Graphical User Interface (GUI)-

 

Graphical User Interface (GUI) allows a user to interact with programs using visual components. Such as, entry box, buttons, canvas, menus etc.. On the other hand, Command Line Interface (CLI) uses commands or text labels to interact with a software program.

Graphical User Interface (GUI) Programming in Python using tkinter- In Python, you can create visual components using tkinter library. Package tkinter has several GUI components and you can use them just calling functions.

In this post you will know about to create visual components using tkinter.

Creating a simple window-

To create any visual component you need to import tkinter package.

import tkinter as t

rootwindow=t.Tk()
t.mainloop()

If you run the above program you will get the small window in which nothing is written.

Graphical User Interface (GUI) Programs in Python using tkinter Package

Tk() function from tkinter creates a windowobject(rootwindow) and mainloop() function is for showing the window until you close it. If you do not use mainloop() function then window will appear instantly and you might not be able to see it.

Let us use title() function for title of the window and minsize() function for setting minimum size of window.

import tkinter as t
rootwindow=t.Tk()
rootwindow.minsize(500,500)
rootwindow.title(“GUI Programming”)
t.mainloop()

You will see the output below.

Graphical User Interface (GUI) Programs in Python using tkinter Package

You have not written anything inside the window let us write something. Using Label function we can put some text in window.

import tkinter as t
rootwindow=t.Tk()
rootwindow.minsize(500,500)
rootwindow.title(“GUI Programming”)
l1=t.Label(rootwindow, text=”This is GUI Programming”)
l1.place(x=250,y=250)
t.mainloop()

In the above program, you can see Label function in which text keyword parameter holds a string. Which you can see in window, place() function
puts the label at the place you specify coordinate (x, y). Here place() is called geometry manager.

You will see the output below:

Graphical User Interface (GUI) Programs in Python using tkinter Package

You have seen some text in window. Now create entry boxes in the created window you can do it by using Entry() function.

import tkinter as t

rootwindow=t.Tk()
rootwindow.minsize(800,300)
rootwindow.title(“GUI Programming”)
l1=t.Label(rootwindow, text=”This is GUI Programming”)
l1.place(x=300,y=20)
l2=t.Label(rootwindow, text=”Enter First Number”)
l2.place(x=300,y=60)
e1=t.Entry()
e1.place(x=300,y=80)
l3=t.Label(rootwindow, text=”Enter Second Number”)
l3.place(x=300,y=100)
e2=t.Entry()
e2.place(x=300,y=120)
t.mainloop()

The output of the below program would be.

Graphical User Interface (GUI) Programs in Python using tkinter Package

In the above image, you can see two entry boxes.

You have created two entry boxes, and you can enter numbers and letters but nothing will happen.

To happen something you have to create a button and if you press the button a function should be executed.

def cal():
x=int(e1.get())
y=int(e2.get())
z= x + y
l4=t.Label(rootwindow, text=”Sum=”+str(z))
l4.place(x=300,y=180)

import tkinter as t
rootwindow=t.Tk()
rootwindow.minsize(600,300)
rootwindow.title(“GUI Programming”)
l1=t.Label(rootwindow, text=”This is GUI Programming”)
l1.place(x=300,y=20)
l2=t.Label(rootwindow, text=”Enter First Number”)
l2.place(x=300,y=60)
e1=t.Entry()
e1.place(x=300,y=80)
l3=t.Label(rootwindow, text=”Enter Second Number”)
l3.place(x=300,y=100)
e2=t.Entry()
e2.place(x=300,y=120)
b1=t.Button(rootwindow, text=”Press Me”,command=cal)
b1.place(x=300,y=140)
t.mainloop()

 

Graphical User Interface (GUI) Programs in Python using tkinter Package

The above program is full GUI program, in which if you enter two numbers in entry boxes and press “Press Me” button then numbers will be added.
You can observe command is assigned by cal function which is user defined function. Function get() will receive objects you enter in entry boxes as string.

eval() function- eval() function is very important to build a calculator eval() function can compute an expression.

For example

expr=eval(‘2+3+5-2’)
print(expr)

Then the output would be 8

Making a Simple Calculator Which Evaluates Expressions-

def cal():
expr=eval(e1.get())
l4=t.Label(rootwindow, text=’Result=’+str(expr))
l4.place(x=300,y=180)

import tkinter as t
rootwindow=t.Tk()
rootwindow.minsize(600,300)
rootwindow.title(“GUI Programming”)
l1=t.Label(rootwindow, text=”This is GUI Programming”)
l1.place(x=300,y=20)
l2=t.Label(rootwindow, text=”Enter First Number”)
l2.place(x=300,y=60)
e1=t.Entry()
e1.place(x=300,y=80)
b1=t.Button(rootwindow, text=”Press Me”,command=cal)
b1.place(x=300,y=140)
t.mainloop()

The above program can evaluate an expression.
See an output.

Graphical User Interface (GUI) Programs in Python using tkinter Package

Canvas in Python-
Canvas you use for painting the same way you can draw several geometrical shapes on Canvas.

import tkinter as t

rwin= t.Tk()
c = t.Canvas(rwin, bg = “red”, height = 500, width = 400)
c.pack()
c.create_line(0,10,300,600,fill = ‘blue’)
c.create_rectangle(20,40,300,300,fill=’green’)
c.create_polygon(10,20, 30,400, 60,70, 400,200,500,500, fill=’white’)
c.pack()
t.mainloop()

The similar way you can create canvas using Canvas function.
And you can create a line using create_line() function.
A rectancle you can create using create_rectangle() function.
A polygon you can create using create_rectangle() function.

The output of the above function would be.

Graphical User Interface (GUI) Programs in Python using tkinter Package

Conclusion-

To sum up, I have demonstrated how to make GUI components using tkinter package. Hope, you will make some interesting program after reading this post. You can make a calculator to start GUI programming.

References(Links)
1-Graphical User Interfaces with Tk, https://docs.python.org/3/library/tk.html

2-https://subscription.packtpub.com/book/application_development/9781787129450/3/ch03lvl1sec30/how-to-create-the-title-of-a-tkinter-window-form

Leave a Comment

Your email address will not be published. Required fields are marked *

©Postnetwork-All rights reserved.