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

Here we have defined the four user defined functions to perform addition,
subtraction, multiplication and division of the two numbers in the
text boxes.
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)

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

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

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)
]

 

 

Loading

4 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

  3. Pingback: Python Quotes Changer Program tkinter GUI | EasyCodeBook.com

  4. Pingback: Python Spinbox Change Fontsize GUI Program | EasyCodeBook.com

Leave a Reply

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