In this tutorial, I’ll show you how to create a simple calculator using Python.
To start, I’ll share the source code that you can use right away. I’ll then perform a quick demonstration, followed by an explanation of each component that was used in the code to build the calculator in Python.
This is how the calculator would look like:
Source Code to Create the Calculator using Python
You may run the following code in Python in order to launch the calculator:
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()
Using the Calculator in Python
You should now see the following display:
Let’s say that you want to sum the values of 4 and 3. You can then type those values in the entry boxes, and then click on the ‘+’ button to add those values:
The result that you’ll get is 7. Which is indeed the sum of 4 and 3:
And if you decide to multiply those values instead, then simply click on the ‘x’ to multiply the values, and you’ll get the result of 12:
In the final section, I’ll explain each component that was used in the code.
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 will 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’ will 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.