GUI Calculator using Tkinter in Python




GUI CALCULATOR USING TKINTER IN PYTHON

INTRODUCTION:
Calculator is used in everyday life. From the small calculation to the big calculation, calculator is used. Designing a calculator can be done in many ways, one way of designing is by using Python. I have explained all the procedures of designing a calculator using Python.

BUTTONS:
First we have to create 16 buttons, for creating buttons we are using the command which is given below,
“button1 = Button(abc, text=' 1 ', fg='black', bg='white', command=lambda: press(1), height=5, width=10) button1.grid(row=2, column=0)”

Using the word button a new button is created, ‘fg’ is for front colour and ‘bg’ is for background colour. ‘Height’ specifies the height of a button and ‘width’ specifies the width of the button. In grid we given the row and column where the button should be displayed.
In the same way do it for 16 buttons

FUNCTIONS:
We have to create some functions to run the program.
“command=lambda: press(1)”
This command is given inside of the button. This command is used to call a function when several buttons access the same function, the word ‘lambda’ is used.
Here we are creating three functions for first 14 buttons we use the same functions and then for next two buttons we use different functions separately because one button is ’clear’ and the other button is ‘equal’

We define the function in the name ‘def’
ie.“def press(num): “This is how the function is declared and all the basic functions are declared within it.

In the function equalpress(), we are using two functions they are ‘try’ and ‘except’. The fun ‘try’ will check the conditions within it, if everything is true then it will be displayed or else it will go to ‘except’ and the output is displayed as “there is error”. After we rectify the error or after giving a correct input the output will be displayed.  

SOURCE CODE:
from tkinter import *  
op = ""
def press(num): 
    global op
    op = op + str(num)
    equation.set(op)

def equalpress():
    try:
        global op
        total = str(eval(op))
        equation.set(total)
        op = ""
    except:
        equation.set(" error ")
        op = ""


def clear():
    global op
    op = ""
    equation.set("")

abc = Tk()
abc.configure(background="light grey")
abc.title("Calculator")
abc.geometry("350x400")
equation = StringVar()
op_field = Entry(abc,font=("arial",30,"bold"),textvariable=equation)
op_field.grid(columnspan=70,ipadx=70)
equation.set('enter the operand')

button1 = Button(abc, text=' 1 ', fg='black', bg='white', command=lambda: press(1), height=5, width=10)
button1.grid(row=2, column=0)

button2 = Button(abc, text=' 2 ', fg='black', bg='white', command=lambda: press(2), height=5, width=10)
button2.grid(row=2, column=1)

button3 = Button(abc, text=' 3 ', fg='black', bg='white', command=lambda: press(3), height=5, width=10)
button3.grid(row=2, column=2)

button4 = Button(abc, text=' 4 ', fg='black', bg='white', command=lambda: press(4), height=5, width=10)
button4.grid(row=3, column=0)

button5 = Button(abc, text=' 5 ', fg='black', bg='white', command=lambda: press(5), height=5, width=10)
button5.grid(row=3, column=1)

button6 = Button(abc, text=' 6 ', fg='black', bg='white', command=lambda: press(6), height=5, width=10)
button6.grid(row=3, column=2)

button7 = Button(abc, text=' 7 ', fg='black', bg='white', command=lambda: press(7), height=5, width=10)
button7.grid(row=4, column=0)

button8 = Button(abc, text=' 8 ', fg='black', bg='white', command=lambda: press(8), height=5, width=10)
button8.grid(row=4, column=1)

button9 = Button(abc, text=' 9 ', fg='black', bg='white', command=lambda: press(9), height=5, width=10)
button9.grid(row=4, column=2)

button0 = Button(abc, text=' 0 ', fg='black', bg='white', command=lambda: press(0), height=5, width=10)
button0.grid(row=5, column=0)

plus = Button(abc, text=' + ', fg='black', bg='white', command=lambda: press("+"), height=5, width=10)
plus.grid(row=2, column=3)

minus = Button(abc, text=' - ', fg='black', bg='white', command=lambda: press("-"), height=5, width=10)
minus.grid(row=3, column=3)

 multiply = Button(abc, text=' * ', fg='black', bg='white', command=lambda: press("*"), height=5, width=10)
multiply.grid(row=4, column=3)

divide = Button(abc, text=' / ', fg='black', bg='white', command=lambda: press("/"), height=5, width=10)
divide.grid(row=5, column=3)

equal = Button(abc, text=' = ', fg='black', bg='white', command=equalpress, height=5, width=10)
equal.grid(row=5, column=2)

clear = Button(abc, text='Clear', fg='black', bg='white', command=clear, height=5, width=10)
clear.grid(row=5, column='1')
  
abc.mainloop()

CONCLUSION:
Hence we could create an application ,ie. Calculator using GUI in python, in the same way we can create many applications, all these basic commands and simple logic will be used in every places.
If you have any doubt regarding this blog and if you couldn’t understand the program, please watch my video for clear understanding.








Comments

Popular posts from this blog

TOKEN SEPARATION USING PYTHON