# 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

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

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)

b2 = Button(window, text="Subtract", width=10,command=subtract_numbers)

b3 = Button(window, text="Multiply", width=10,command=multiply_numbers)

b4 = Button(window, text="Divide", width=10, command=divide_numbers)
label_easycodebook = Label(text="www.EasyCodeBook.com for Python GUI Tutorials",
bg='lightgreen',font=('verdana',12))

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.
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: