Python Tkinter program for the simple calculator


In this program, you will learn how to create a simple calculator using Tkinter in Python.


b1 = tk.Button(root, text="+", command=findsum).place(x=200, y=300)
b2 = tk.Button(root, text="-", command=findsub).place(x=250, y=300)
b2 = tk.Button(root, text="*", command=findmul).place(x=300, y=300)
b2 = tk.Button(root, text="/", command=finddiv).place(x=350, y=300)

Example: How to create a simple calculator using Tkinter in Python

import tkinter as tk
from functools import partial


def findsum(l3, num1, num2):
    n1 = int(num1.get())
    n2 = int(num2.get())
    n3 = n1 + n2
    l3.config(text="Result = %d" % n3)
    return


def findsub(l3, num1, num2):
    n1 = int(num1.get())
    n2 = int(num2.get())
    n3 = n1 - n2
    l3.config(text="Result = %d" % n3)
    return


def findmul(l3, num1, num2):
    n1 = int(num1.get())
    n2 = int(num2.get())
    n3 = n1 * n2
    l3.config(text="Result = %d" % n3)
    return


def finddiv(l3, num1, num2):
    n1 = int(num1.get())
    n2 = int(num2.get())
    n3 = n1 / n2
    l3.config(text="Result = %f" % n3)
    return


root = tk.Tk()
root.title("Xiith.com")
root.geometry("700x500")

number1 = tk.StringVar()
number2 = tk.StringVar()

l1 = tk.Label(root, text="Enter 1st number").place(x=20, y=60)
l2 = tk.Label(root, text="Enter 2nd number").place(x=20, y=120)
t1 = tk.Entry(root, textvariable=number1).place(x=200, y=60)
t2 = tk.Entry(root, textvariable=number2).place(x=200, y=120)

labelResult = tk.Label(root)
labelResult.place(x=250, y=200)

findsum = partial(findsum, labelResult, number1, number2)
findsub = partial(findsub, labelResult, number1, number2)
findmul = partial(findmul, labelResult, number1, number2)
finddiv = partial(finddiv, labelResult, number1, number2)

b1 = tk.Button(root, text="+", command=findsum).place(x=200, y=300)
b2 = tk.Button(root, text="-", command=findsub).place(x=250, y=300)
b2 = tk.Button(root, text="*", command=findmul).place(x=300, y=300)
b2 = tk.Button(root, text="/", command=finddiv).place(x=350, y=300)

root.mainloop()

Output:

tkinter simple calculator