Python 3 Four Function Calculator Program tkinter GUI

By | March 25, 2020

Topic: Python 3 Four Function Calculator Program tkinter GUI

Widgets used in Simple 4 Function Calculator

We have used here:

  1. Label widgets to display labels for entry widgets for example. We use a label widget for showing result of addition, subtraction, multiplication or division. Last label simply displays ‘www.EasycodeBook.com for Python GUI Tutorials’.
  2. Two Entry widgets will receive user input. The user will enter two numbers in them.
  3. Four button widgets are used to perform 4 arithmetic functions of this calculator program

Python 3 Four Function Calculator Program tkinter GUI

Python 3 Four Function Calculator Program tkinter GUI

 

# Simple 4 Function Calculator Program
# using tkinter GUI module
from tkinter import *
 
def add_numbers():
    res=float(e1.get())+ float(e2.get())
    res = round(res,4)
    label_text.set(res)
def subtract_numbers():
    res=float(e1.get())- float(e2.get())
    res = round(res,4)
    label_text.set(res)

def multiply_numbers():
    res=float(e1.get())* float(e2.get())
    res = round(res,4)
    label_text.set(res)

def divide_numbers():
    res=float(e1.get()) / float(e2.get())
    res = round(res,4)
    label_text.set(res)
 
window = Tk()
#window.geometry("400x300")
label_text=StringVar();
Label(window, text="Enter First Number:").grid(row=0, column=0,sticky=W)
Label(window, text="Enter Second Number:").grid(row=1,column=0, sticky=W)
Label(window, text="Result:").grid(row=3,column=0, sticky=W)
result=Label(window, text="", textvariable=label_text).grid(row=3,column=1, sticky=W)
 
e1 = Entry(window)
e2 = Entry(window)
 
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
 
b1 = Button(window, text="Add", width=10,command=add_numbers)
b1.grid(row=0, column=2, padx=5, pady=5,sticky=W)
 
b2 = Button(window, text="Subtract", width=10,command=subtract_numbers)
b2.grid(row=0, column=3, padx=5, pady=5,sticky=W)

b3 = Button(window, text="Multiply", width=10,command=multiply_numbers)
b3.grid(row=1, column=2, padx=5, pady=5,sticky=W)

b4 = Button(window, text="Divide", width=10, command=divide_numbers)
b4.grid(row=1, column=3, padx=5, pady=5,sticky=W)
label_easycodebook = Label(text="www.EasyCodeBook.com for Python GUI Tutorials",
                           bg='lightgreen',font=('verdana',12))

label_easycodebook.grid(row=5,column=0,padx=5,pady=5,columnspan=4) 
 
window.mainloop()
 

How Python 4 Function Calculator Program Works

# Simple 4 Function Calculator Program
# using tkinter GUI module
from tkinter import *

[
write down 4 functions for handling click events of buttons
get() is used to get text from entry widget
float() for converting numeric string into float number
round(num,4) for rounding up to 4 decimal places
]
def add_numbers():
    res=float(e1.get())+ float(e2.get())
    res = round(res,4)
    label_text.set(res)
def subtract_numbers():
    res=float(e1.get())- float(e2.get())
    res = round(res,4)
    label_text.set(res)

def multiply_numbers():
    res=float(e1.get())* float(e2.get())
    res = round(res,4)
    label_text.set(res)

def divide_numbers():
    res=float(e1.get()) / float(e2.get())
    res = round(res,4)
    label_text.set(res)
 
window = Tk()
[
create 4 label widgets and using grid() 
place them in window

]
label_text=StringVar();
Label(window, text="Enter First Number:").grid(row=0, column=0,sticky=W)
Label(window, text="Enter Second Number:").grid(row=1,column=0, sticky=W)
Label(window, text="Result:").grid(row=3,column=0, sticky=W)
result=Label(window, text="", textvariable=label_text).grid(row=3,column=1, sticky=W)

[
create 2 entry widgets for input and using grid() 
place them in window

]  
e1 = Entry(window)
e2 = Entry(window)
 
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

[
create 4 button widgets and using grid() 
place them in window

Use command=event_handler_function for click event
example:
b1 = Button(window, text="Add", width=10,command=add_numbers)
] 
 
b1 = Button(window, text="Add", width=10,command=add_numbers)
b1.grid(row=0, column=2, padx=5, pady=5,sticky=W)
 
b2 = Button(window, text="Subtract", width=10,command=subtract_numbers)
b2.grid(row=0, column=3, padx=5, pady=5,sticky=W)

b3 = Button(window, text="Multiply", width=10,command=multiply_numbers)
b3.grid(row=1, column=2, padx=5, pady=5,sticky=W)

b4 = Button(window, text="Divide", width=10, command=divide_numbers)
b4.grid(row=1, column=3, padx=5, pady=5,sticky=W)

label_easycodebook = Label(text="www.EasyCodeBook.com for Python GUI Tutorials",
                           bg='lightgreen',font=('verdana',12))

label_easycodebook.grid(row=5,column=0,padx=5,pady=5,columnspan=4) 
window.mainloop()
 

2 thoughts on “Python 3 Four Function Calculator Program tkinter GUI

  1. Pingback: Multiplication Table Python GUI Program | EasyCodeBook.com

  2. Pingback: Find Factorial by Recursive Function Python GUI Program | EasyCodeBook.com

Leave a Reply

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