In this short guide, you’ll see how to create a simple calculator using Python.
First, you’ll observe the complete syntax to create the calculator. And then, you’ll see the steps to build the calculator from scratch.
Complete Syntax to Create a Calculator using Python
Here is the complete syntax to create a simple calculator using Python.
import tkinter as tk root= tk.Tk() canvas1 = tk.Canvas(root, width = 300, height = 300) canvas1.pack() entry1 = tk.Entry (root) canvas1.create_window(210, 100, window=entry1) entry2 = tk.Entry (root) canvas1.create_window(210, 140, window=entry2) entry3 = tk.Entry (root) canvas1.create_window(210, 240, window=entry3) label0 = tk.Label(root, text='Calculator') label0.config(font=('helvetica', 14)) canvas1.create_window(150, 40, window=label0) label1 = tk.Label(root, text='Type Value 1:') label1.config(font=('helvetica', 10)) canvas1.create_window(100, 100, window=label1) label2 = tk.Label(root, text='Type Value 2:') label2.config(font=('helvetica', 10)) canvas1.create_window(100, 140, window=label2) label3 = tk.Label(root, text='Result:') label3.config(font=('helvetica', 10)) canvas1.create_window(100, 240, window=label3) def add(): v1 = entry1.get() v2 = entry2.get() label4 = tk.Label(root, text= float(v1)+float(v2),font=('helvetica', 10, 'bold'),bg='white') canvas1.create_window(210, 240, window=label4) buttonAdd = tk.Button(text='+', command=add, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 5) canvas1.create_window(90, 190, window=buttonAdd) def sub(): v1 = entry1.get() v2 = entry2.get() label5 = tk.Label(root, text= float(v1)-float(v2),font=('helvetica', 10, 'bold'),bg='white') canvas1.create_window(210, 240, window=label5) buttonSub = tk.Button(text='–', command=sub, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 5) canvas1.create_window(140, 190, window=buttonSub) def mul(): v1 = entry1.get() v2 = entry2.get() label6 = tk.Label(root, text= float(v1)*float(v2),font=('helvetica', 10, 'bold'),bg='white') canvas1.create_window(210, 240, window=label6) buttonMul = tk.Button(text='x', command=mul, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 5) canvas1.create_window(190, 190, window=buttonMul) def div(): v1 = entry1.get() v2 = entry2.get() label7 = tk.Label(root, text= float(v1)/float(v2),font=('helvetica', 10, 'bold'),bg='white') canvas1.create_window(210, 240, window=label7) buttonDiv = tk.Button(text='/', command=div, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 5) canvas1.create_window(240, 190, window=buttonDiv) root.mainloop()
Next, you’ll see the full steps to build the calculator in Python.
How to Build the Calculator in Python
Import the tkinter package and create the Canvas
The first thing that you’ll need to do is to import the tkinter package. The tkinter package can be used to create a Graphical User Interface (GUI) in Python.
You’ll also need to add the canvas, which is your GUI display in which you can place items, such as buttons, entry boxes, etc.
import tkinter as tk root= tk.Tk() canvas1 = tk.Canvas(root, width = 300, height = 300) canvas1.pack()
Create the entry boxes
Next, you’ll need to create the entry boxes to collect the data from the user.
The first two entry boxes are used to collect the Value 1 and Value 2. While the third entry box is just for visual purposes, where the result of the calculation would be displayed.
entry1 = tk.Entry (root) canvas1.create_window(210, 100, window=entry1) entry2 = tk.Entry (root) canvas1.create_window(210, 140, window=entry2) entry3 = tk.Entry (root) canvas1.create_window(210, 240, window=entry3)
Add the labels
There are 4 labels that are currently displayed on the GUI:
- Calculator
- Type Value 1:
- Type Value 2:
- Result:
You may edit the text, format and/or location where the labels will be displayed.
label0 = tk.Label(root, text='Calculator') label0.config(font=('helvetica', 14)) canvas1.create_window(150, 40, window=label0) label1 = tk.Label(root, text='Type Value 1:') label1.config(font=('helvetica', 10)) canvas1.create_window(100, 100, window=label1) label2 = tk.Label(root, text='Type Value 2:') label2.config(font=('helvetica', 10)) canvas1.create_window(100, 140, window=label2) label3 = tk.Label(root, text='Result:') label3.config(font=('helvetica', 10)) canvas1.create_window(100, 240, window=label3)
Create the functions and buttons
There are 4 functions in the code:
- add() – to add the values
- sub() – to subtract the values
- mul() – to multiply the values
- div() – to divide the values
For each of those 4 functions, there is an associated button that can be used to trigger the function. For example, the ‘buttonAdd’ would trigger the add function to add the values that the user typed in the entry boxes.
def add(): v1 = entry1.get() v2 = entry2.get() label4 = tk.Label(root, text= float(v1)+float(v2),font=('helvetica', 10, 'bold'),bg='white') canvas1.create_window(210, 240, window=label4) buttonAdd = tk.Button(text='+', command=add, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 5) canvas1.create_window(90, 190, window=buttonAdd) def sub(): v1 = entry1.get() v2 = entry2.get() label5 = tk.Label(root, text= float(v1)-float(v2),font=('helvetica', 10, 'bold'),bg='white') canvas1.create_window(210, 240, window=label5) buttonSub = tk.Button(text='–', command=sub, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 5) canvas1.create_window(140, 190, window=buttonSub) def mul(): v1 = entry1.get() v2 = entry2.get() label6 = tk.Label(root, text= float(v1)*float(v2),font=('helvetica', 10, 'bold'),bg='white') canvas1.create_window(210, 240, window=label6) buttonMul = tk.Button(text='x', command=mul, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 5) canvas1.create_window(190, 190, window=buttonMul) def div(): v1 = entry1.get() v2 = entry2.get() label7 = tk.Label(root, text= float(v1)/float(v2),font=('helvetica', 10, 'bold'),bg='white') canvas1.create_window(210, 240, window=label7) buttonDiv = tk.Button(text='/', command=div, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 5) canvas1.create_window(240, 190, window=buttonDiv) root.mainloop()
Don’t forget to add the root.mainloop() at the end.