How to develop a simple calculator using Tkinter in python?

How to develop a simple calculator using Tkinter in python?

Hello folks, This is Ruban Gino Singh To develop a successful simple calculator using Tkinter, the developer must know about the basics of python and some basics about the Tkinter Library/Module. Let me explain some basics about the Tkinter module.

What is Tkinter in python?

Tkinter Programming:

Tkinter is the standard GUI (Graphical User Interface) in Python. Python when combined with Tkinter provides a fast and easy way to create simple minimal Graphical User Interface applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.

Creating a GUI application using Tkinter is an easy task. All you need to do is perform the following steps -

  • Import the Tkinter module.
  • Create the GUI application main window.
  • Add one or more of the above-mentioned widgets to the GUI application.
  • Enter the main event loop to take action against the event triggered by the user.

Example Syntax:

#!/usr/bin/python
import Tkinter 

window = Tk()
window.title("Your Title")
window.geometry("400x400") # size of the window (widthxheight)

# your code goes here...

window.mainloop()

So, now you guys know about What is Tkinter?. Let's see how to develop a simple GUI calculator with python.

from tkinter import *

# Syntax to create a Window
window = Tk()
window.title('URK20CS2001-Calculator') # To give the title of the window.
window.geometry("400x450") # To initialize the size of the window. 
window.resizable(False, False) # If you don't want to make your window resizable initialize this line. 

num1 = IntVar() # initialize the first variable
num2 = IntVar() # initialize the second variable

mainTitle = Label(window, text="Calculator", font=("Arial", 25))
mainTitle.place(x=134, y=45) # Use the place layout to place further labels

# Text
type1 = Label(window, text="Type Value 1:", font=("Arial", 15))
type1.place(x=61, y=125)

type2 = Label(window, text="Type Value 2:", font=("Arial", 15))
type2.place(x=61, y=197)

resultLabel = Label(window, text="Result:", font=("Arial", 15))
resultLabel.place(x=118, y=336)

 # Use functions to give action to the button. 
def Add():
    sum = num1.get() + num2.get()
    resultEntry.delete(0, END)
    resultEntry.insert(0, str(sum))

def Diff():
    diff = num1.get() - num2.get()
    resultEntry.delete(0, END)
    resultEntry.insert(0, str(diff))

def Mul():
    mul = num1.get() * num2.get()
    resultEntry.delete(0, END)
    resultEntry.insert(0, str(mul))

def Div():
    div = num1.get() / num2.get()
    resultEntry.delete(0, END)
    resultEntry.insert(0, str(div))

# Entries Syntax
entry1 = Entry(window, textvariable=num1)
entry1.place(x=200, y=130, height=25, width=143)

entry2 = Entry(window, textvariable=num2)
entry2.place(x=200, y=202, height=25, width=143)

resultEntry = Entry(window)
resultEntry.place(x=200, y=340, height=25, width=143)

# Buttons syntax
buttonAdd = Button(window, text="+", fg="white", bg="green", font=("Arial", 14), command=Add) # Use command to call the functions.
buttonAdd.place(x=74, y=262, height=32, width=56)

buttonSub = Button(window, text="-", fg="white", bg="green", font=("Arial", 14), command=Diff)
buttonSub.place(x=142, y=262, height=32, width=56)

buttonMul = Button(window, text="x", fg="white", bg="green", font=("Arial", 14), command=Mul)
buttonMul.place(x=210, y=262, height=32, width=56)

buttonDiv = Button(window, text="/", fg="white", bg="green", font=("Arial", 14), command=Div) 
buttonDiv.place(x=278, y=262, height=32, width=56)

window.mainloop()

Output:

image.png

I hope you guys will get the code to develop the simple calculator program. If you want to learn more about Tkinter in python visit Tutorials point to know about the python Tkinter GUI.

Well, stay tuned to grab the tips and tricks about the programming languages here...

Stay tuned to create a registration form using Tkinter.

written by Ruban Gino Singh, rubangino.in.

Get in touch with me!!
Ruban Gino Singh
Website: www.rubangino.in
Mail: info@rubangino.in

Follow me on:
Instagram: bit.ly/rubangino
Facebook: bit.ly/rubangino22
Linkedin: bit.ly/ruban-gino-singh
Twitter: bit.ly/Rubangino22