Calculator Using Python and TKinter Library

    Hello python programmer, for my first post, I want to share a little about a project that uses python's TKINTER library as a visual for a project. As is well known, python is a widely-used programming language, a language that uses interpretation as a compiler, and uses object-oriented programming concepts, and is a programming language with a high-level language.

    Before looking at the python source code used, here is the display of the calculator that will be created.



    

    The arithmetic functions on the calculator are simple arithmetic such as addition, subtraction, multiplication, and division, then there is the power of two or square, then there is the power of y where y is the next number entered, then there is the square root, and factorial. Here is the source code for a calculator with programming language using the TKinter library


# Python program to create a simple GUI

# calculator using Tkinter

import math

from tkinter import *

 

expression = ""

 

def press(num):

    global expression

    expression = expression + str(num)

    equation.set(expression)

 

def sqrt():

    global expression

    if int(expression)>=0:

        temp = str(eval(expression+'**(1/2)'))

        equation.set(temp)

        expression = temp

       

    else:

        temp = "ERROR"

    equation.set(temp)

 

def factorial(n):

    if n==0 or n==1:

        return 1

    else:

        return n*factorial(n-1)

   

def fact_func():

    global expression

    hasil = str(factorial(int(expression)))

    expression = hasil

    equation.set(hasil)

   

def pangkat2():

    global expression

    if int(expression)>=0:

        temp = str(eval(expression+'**2'))

        equation.set(temp)

        expression = temp

    else:

        temp = "ERROR"

    equation.set(temp)

 

def equalpress():

    try:

        global expression

        total = str(eval(expression))

        equation.set(total)

        expression = total

    except:

        equation.set(" error ")

        expression = ""

 

def clear():

    global expression

    expression = ""

    equation.set("")

 

def backspace():

    global expression

    expression = expression[:-1]

    equation.set(expression)

   

def delete():

    global expression

    expression = expression[1:]

    equation.set(expression)

 

if __name__ == "__main__":

    gui = Tk()

    gui.configure(background="#e2eeda")

    gui.title("Calculator using TKInter and Python")

    gui.geometry("270x210")

    gui.resizable(False,False)

 

    fg='#fff2ed'

    bg='#fe0000'

    relief='groove'

    pady=1

 

    equation = StringVar()

 

    expression_field = Entry(gui, textvariable=equation,justify='center',relief='flat')

    expression_field.grid(rowspan=2,column=0, columnspan=4,sticky=E, ipadx=70,ipady=3,pady=5)

    equation.set("Calculator")

 

#Button

    button1 = Button(gui, text=' 1 ', fg=fg, bg=bg,

                     command=lambda: press(1), height=1, width=7,relief=relief)

    button1.grid(row=2, column=0,pady=pady)

 

    button2 = Button(gui, text=' 2 ', fg=fg, bg=bg,

                     command=lambda: press(2), height=1, width=7,relief=relief)

    button2.grid(row=2, column=1,pady=pady)

 

    button3 = Button(gui, text=' 3 ', fg=fg, bg=bg,

                     command=lambda: press(3), height=1, width=7,relief=relief)

    button3.grid(row=2, column=2,pady=pady)

 

    button4 = Button(gui, text=' 4 ', fg=fg, bg=bg,

                     command=lambda: press(4), height=1, width=7,relief=relief)

    button4.grid(row=2, column=3,pady=pady)

 

    button5 = Button(gui, text=' 5 ', fg=fg, bg=bg,

                     command=lambda: press(5), height=1, width=7,relief=relief)

    button5.grid(row=3, column=0,pady=pady)

 

    button6 = Button(gui, text=' 6 ', fg=fg, bg=bg,

                     command=lambda: press(6), height=1, width=7,relief=relief)

    button6.grid(row=3, column=1,pady=pady)

 

    button7 = Button(gui, text=' 7 ', fg=fg, bg=bg,

                     command=lambda: press(7), height=1, width=7,relief=relief)

    button7.grid(row=3, column=2,pady=pady)

 

    button8 = Button(gui, text=' 8 ', fg=fg, bg=bg,

                     command=lambda: press(8), height=1, width=7,relief=relief)

    button8.grid(row=3, column=3,pady=pady)

 

    button9 = Button(gui, text=' 9 ', fg=fg, bg=bg,

                     command=lambda: press(9), height=1, width=7,relief=relief)

    button9.grid(row=4, column=0,pady=pady)

 

    button0 = Button(gui, text=' 0 ', fg=fg, bg=bg,

                     command=lambda: press(0), height=1, width=7,relief=relief)

    button0.grid(row=4, column=1,pady=pady)

 

#Aritmatika

    plus = Button(gui, text=' + ', fg=fg, bg=bg,

                  command=lambda: press("+"), height=1, width=7,relief=relief)

    plus.grid(row=4, column=2,pady=pady)

 

    minus = Button(gui, text=' - ', fg=fg, bg=bg,

                   command=lambda: press("-"), height=1, width=7,relief=relief)

    minus.grid(row=4, column=3,pady=pady)

 

    clear = Button(gui, text='Clear', fg=fg, bg=bg,

                   command=clear, height=1, width=7,relief=relief)

    clear.grid(row=5, column=0,pady=pady)

 

    kurangi = Button(gui, text='<-', fg=fg, bg=bg,

                     command=backspace, height = 1, width = 7,relief=relief)

    kurangi.grid(row=5, column=1,pady=pady)

 

    multiply = Button(gui, text='x', fg=fg, bg=bg,

                      command=lambda: press("*"), height=1, width=7,relief=relief)

    multiply.grid(row=5, column=2,pady=pady)

 

    pangkat2 = Button(gui, text='^2', fg=fg, bg=bg,

                      command=pangkat2, height=1, width=7,relief=relief)

    pangkat2.grid(row=6, column=0,pady=pady)

 

    fact = Button(gui, text='!', fg=fg, bg=bg,

                      command=fact_func, height=1, width=7,relief=relief)

    fact.grid(row=6, column=3,pady=pady)

 

    pangkaty = Button(gui, text='^y', fg=fg, bg=bg,

                      command=lambda: press("**"), height=1, width=7,relief=relief)

    pangkaty.grid(row=6, column=1,pady=pady)

 

    akar = Button(gui, text='V2', fg=fg, bg=bg,

                      command=sqrt, height=1, width=7,relief=relief)

    akar.grid(row=6, column=2,pady=pady)

 

    divide = Button(gui, text=' / ', fg=fg, bg=bg,

                    command=lambda: press("/"), height=1, width=7,relief=relief)

    divide.grid(row=5, column=3,pady=pady)

 

#Baris Akhir

    hapus = Button(gui, text='Del', fg=fg, bg=bg,

                     command=delete, height = 1, width = 7,relief=relief)

    hapus.grid(row=7, column=1,pady=pady)

   

    Decimal = Button(gui, text='.', fg=fg, bg=bg,

                     command=lambda: press('.'), height=1, width=7,relief=relief)

    Decimal.grid(row=7, column=2,pady=pady)

 

    equal = Button(gui, text=' = ', fg=fg, bg=bg,

                   command=equalpress, height=1, width=7,relief=relief)

    equal.grid(row=7, column=3,pady=pady)

 

 

    gui.mainloop()


Here is an explanation of the code


import math

from tkinter import *


The code is the code to retrieve the required libraries such as the math library and the Tkinter library. 


expression = ""

The code is code to create a variable with the name expression.

def press(num):

    global expression

    expression = expression + str(num)

    equation.set(expression)

 

def sqrt():

    global expression

    if int(expression)>=0:

        temp = str(eval(expression+'**(1/2)'))

        equation.set(temp)

        expression = temp

       

    else:

        temp = "ERROR"

    equation.set(temp)

 

def factorial(n):

    if n==0 or n==1:

        return 1

    else:

        return n*factorial(n-1)

   

def fact_func():

    global expression

    hasil = str(factorial(int(expression)))

    expression = hasil

    equation.set(hasil)

   

def pangkat2():

    global expression

    if int(expression)>=0:

        temp = str(eval(expression+'**2'))

        equation.set(temp)

        expression = temp

    else:

        temp = "ERROR"

    equation.set(temp)

 

def equalpress():

    try:

        global expression

        total = str(eval(expression))

        equation.set(total)

        expression = total

    except:

        equation.set(" error ")

        expression = ""

 

def clear():

    global expression

    expression = ""

    equation.set("")

 

def backspace():

    global expression

    expression = expression[:-1]

    equation.set(expression)

   

def delete():

    global expression

    expression = expression[1:]

    equation.set(expression)

 

if __name__ == "__main__":

    gui = Tk()

    gui.configure(background="#e2eeda")

    gui.title("Calculator using TKInter and Python")

    gui.geometry("270x210")

    gui.resizable(False,False)

 

    fg='#fff2ed'

    bg='#fe0000'

    relief='groove'

    pady=1

 

    equation = StringVar()

 

    expression_field = Entry(gui, textvariable=equation,justify='center',relief='flat')

    expression_field.grid(rowspan=2,column=0, columnspan=4,sticky=E, ipadx=70,ipady=3,pady=5)

    equation.set("Calculator")


   The code is the code to create a function. The function created here is the basic function in existing arithmetic, there is a press function to make numbers appear on the calculator layer, then there is sqrt for the square root, there is a factorial function to create a factorial calculation function, then the fact_func function to make the result of factorial displayed on the layer calculator, the next power function is a function to perform powers of 2, the next equalpress function is a function to display the results on the calculator, the clear function is to delete those on the calculator layer, then there is a backspace function to delete characters from the right on the calculator layer, while delete is to delete a character from left. Next to the function is a function to display the visual of the calculator.

#Button

    button1 = Button(gui, text=' 1 ', fg=fg, bg=bg,

                     command=lambda: press(1), height=1, width=7,relief=relief)

    button1.grid(row=2, column=0,pady=pady)

 

    button2 = Button(gui, text=' 2 ', fg=fg, bg=bg,

                     command=lambda: press(2), height=1, width=7,relief=relief)

    button2.grid(row=2, column=1,pady=pady)

 

    button3 = Button(gui, text=' 3 ', fg=fg, bg=bg,

                     command=lambda: press(3), height=1, width=7,relief=relief)

    button3.grid(row=2, column=2,pady=pady)

 

    button4 = Button(gui, text=' 4 ', fg=fg, bg=bg,

                     command=lambda: press(4), height=1, width=7,relief=relief)

    button4.grid(row=2, column=3,pady=pady)

 

    button5 = Button(gui, text=' 5 ', fg=fg, bg=bg,

                     command=lambda: press(5), height=1, width=7,relief=relief)

    button5.grid(row=3, column=0,pady=pady)

 

    button6 = Button(gui, text=' 6 ', fg=fg, bg=bg,

                     command=lambda: press(6), height=1, width=7,relief=relief)

    button6.grid(row=3, column=1,pady=pady)

 

    button7 = Button(gui, text=' 7 ', fg=fg, bg=bg,

                     command=lambda: press(7), height=1, width=7,relief=relief)

    button7.grid(row=3, column=2,pady=pady)

 

    button8 = Button(gui, text=' 8 ', fg=fg, bg=bg,

                     command=lambda: press(8), height=1, width=7,relief=relief)

    button8.grid(row=3, column=3,pady=pady)

 

    button9 = Button(gui, text=' 9 ', fg=fg, bg=bg,

                     command=lambda: press(9), height=1, width=7,relief=relief)

    button9.grid(row=4, column=0,pady=pady)

 

    button0 = Button(gui, text=' 0 ', fg=fg, bg=bg,

                     command=lambda: press(0), height=1, width=7,relief=relief)

    button0.grid(row=4, column=1,pady=pady)

 

#Aritmatika

    plus = Button(gui, text=' + ', fg=fg, bg=bg,

                  command=lambda: press("+"), height=1, width=7,relief=relief)

    plus.grid(row=4, column=2,pady=pady)

 

    minus = Button(gui, text=' - ', fg=fg, bg=bg,

                   command=lambda: press("-"), height=1, width=7,relief=relief)

    minus.grid(row=4, column=3,pady=pady)

 

    clear = Button(gui, text='Clear', fg=fg, bg=bg,

                   command=clear, height=1, width=7,relief=relief)

    clear.grid(row=5, column=0,pady=pady)

 

    kurangi = Button(gui, text='<-', fg=fg, bg=bg,

                     command=backspace, height = 1, width = 7,relief=relief)

    kurangi.grid(row=5, column=1,pady=pady)

 

    multiply = Button(gui, text='x', fg=fg, bg=bg,

                      command=lambda: press("*"), height=1, width=7,relief=relief)

    multiply.grid(row=5, column=2,pady=pady)

 

    pangkat2 = Button(gui, text='^2', fg=fg, bg=bg,

                      command=pangkat2, height=1, width=7,relief=relief)

    pangkat2.grid(row=6, column=0,pady=pady)

 

    fact = Button(gui, text='!', fg=fg, bg=bg,

                      command=fact_func, height=1, width=7,relief=relief)

    fact.grid(row=6, column=3,pady=pady)

 

    pangkaty = Button(gui, text='^y', fg=fg, bg=bg,

                      command=lambda: press("**"), height=1, width=7,relief=relief)

    pangkaty.grid(row=6, column=1,pady=pady)

 

    akar = Button(gui, text='V2', fg=fg, bg=bg,

                      command=sqrt, height=1, width=7,relief=relief)

    akar.grid(row=6, column=2,pady=pady)

 

    divide = Button(gui, text=' / ', fg=fg, bg=bg,

                    command=lambda: press("/"), height=1, width=7,relief=relief)

    divide.grid(row=5, column=3,pady=pady)

 

#Baris Akhir

    hapus = Button(gui, text='Del', fg=fg, bg=bg,

                     command=delete, height = 1, width = 7,relief=relief)

    hapus.grid(row=7, column=1,pady=pady)

   

    Decimal = Button(gui, text='.', fg=fg, bg=bg,

                     command=lambda: press('.'), height=1, width=7,relief=relief)

    Decimal.grid(row=7, column=2,pady=pady)

 

    equal = Button(gui, text=' = ', fg=fg, bg=bg,

                   command=equalpress, height=1, width=7,relief=relief)

    equal.grid(row=7, column=3,pady=pady)


The code is the code to create a button to be pressed and provide input to the calculator. The keypad consists of a number pad from 1 to 9, plus the aforementioned arithmetic, and several other functions.



gui.mainloop()


The code is a code to make the application experience repetition so that it is not closed every time it completes a function


So much knowledge that I can share with you may be useful, Thank You

Komentar

Posting Komentar