Simple GUI calculator using Tkinter in Python
Here we will learn to build a simple GUI (Graphical User Interface) calculator using tkinter module in Python. You don't need to install tkinter externally, it comes with Python.
Python Tkinter
The tkinter package is the standard Python interface to the Tk GUI toolkit.
With this module we can build Python based GUI applications.
Simple GUI calculator using Tkinter
Program
#Simple Calculator
from tkinter import *
#Creating main window
root = Tk()
# Giving title to the window
root.title("Simple Calculator")
# Some useful functions
# Function used to display values on to the screen Entry box
def click(numChar):
if display.get() == "Invalid input":
display.delete(0, END)
display.insert(END, numChar)
# Function executes when '=' button is pressed
# Evaluates the value and gives out the result
def EVAL():
try:
value = eval(display.get())
except:
value = "Invalid input"
display.delete(0, END)
display.insert(0, value)
#Creating Buttons
display = Entry(root, width=45, borderwidth=6)
c0 = Button(root, text="0", padx=30, pady=30, command= lambda: click(0))
c1 = Button(root, text="1", padx=30, pady=30, command= lambda: click(1))
c2 = Button(root, text="2", padx=30, pady=30, command= lambda: click(2))
c3 = Button(root, text="3", padx=30, pady=30, command= lambda: click(3))
c4 = Button(root, text="4", padx=30, pady=30, command= lambda: click(4))
c5 = Button(root, text="5", padx=30, pady=30, command= lambda: click(5))
c6 = Button(root, text="6", padx=30, pady=30, command= lambda: click(6))
c7 = Button(root, text="7", padx=30, pady=30, command= lambda: click(7))
c8 = Button(root, text="8", padx=30, pady=30, command= lambda: click(8))
c9 = Button(root, text="9", padx=30, pady=30, command= lambda: click(9))
add = Button(root, text="+", padx=30, pady=30, command= lambda: click('+'))
sub = Button(root, text="-", padx=30, pady=30, command= lambda: click('-'))
mul = Button(root, text="*", padx=30, pady=30, command= lambda: click('*'))
div = Button(root, text="/", padx=30, pady=30, command= lambda: click('/'))
# '=' button which will call EVAL function
equal = Button(root, text="=", padx=66, pady=30, command=EVAL)
# 'clear' button, which will clear all the values in the entry box
clear = Button(root, text="Clear", padx=95, pady=30, command=lambda: display.delete(0, END))
# Button to delete last value
cut = Button(root, text="<=", padx=30, pady=30, command= lambda: display.delete(len(display.get())-1, END))
#Providing position to the buttons
display.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
c0.grid(row=4, column=0)
c1.grid(row=3, column=0)
c2.grid(row=3, column=1)
c3.grid(row=3, column=2)
c4.grid(row=2, column=0)
c5.grid(row=2, column=1)
c6.grid(row=2, column=2)
c7.grid(row=1, column=0)
c8.grid(row=1, column=1)
c9.grid(row=1, column=2)
add.grid(row=4, column=3)
sub.grid(row=3, column=3)
mul.grid(row=2, column=3)
div.grid(row=1, column=3)
equal.grid(row=4, column=1, columnspan=2)
clear.grid(row=5, column=0, columnspan=3)
cut.grid(row=5, column=3)
root.mainloop()
Output
Module and methods used in the program
- tkinter
- Tk()
- title()
- Entry()
- Button()
Post a Comment
If you have any query, feel free to ask.