Python Tkinter GUI Script to Make a Simple Math Calculator Full Project For Beginners

Python Tkinter GUI Script to Make a Simple Math Calculator Full Project For Beginners

 

Welcome folks today in this post we will be showing you how to make a simple calculator in tkinter. This will be made using a library called as tkinter which is a gui library of python. All the source code of application will be given below.

 

 

Get Started

 

 

 

You need to be having python installed on your system

 

And then you need to install tkinter inside your project

 

pip install tkinter

 

Now make a app.py file and copy paste the following code

 

 

app.py

 

 

import tkinter as tk

calc = tk.Tk()
calc.title("CrappyCalc")

buttons = [
'7',  '8',  '9',  '*',  'C',
'4',  '5',  '6',  '/',  'Neg',
'1',  '2',  '3',  '-',  '$',
'0',  '.',  '=',  '+',  '@' ]

# set up GUI
row = 1
col = 0
for i in buttons:
    button_style = 'raised'
    action = lambda x = i: click_event(x)
    tk.Button(calc, text = i, width = 7, height = 7, relief = button_style, command = action) \
        .grid(row = row, column = col, sticky = 'nesw', )
    col += 1
    if col > 4:
        col = 0
        row += 1

display = tk.Entry(calc, width = 40, bg = "white")
display.grid(row = 0, column = 0, columnspan = 5)

def click_event(key):

    # = -> calculate results
    if key == '=':
        # safeguard against integer division
        if '/' in display.get() and '.' not in display.get():
            display.insert(tk.END, ".0")
            
        # attempt to evaluate results
        try:
            result = eval(display.get())
            display.insert(tk.END, " = " + str(result))
        except:
            display.insert(tk.END, "   Error, use only valid chars")
            
    # C -> clear display     
    elif key == 'C':
        display.delete(0, tk.END)
        
        
    # $ -> clear display     
    elif key == '$':
        display.delete(0, tk.END)
        display.insert(tk.END, "$$$$C.$R.$E.$A.$M.$$$$")
        

    # @ -> clear display     
    elif key == '@':
        display.delete(0, tk.END)
        display.insert(tk.END, "wwwwwwwwwwwwwwwwebsite")        

        
    # neg -> negate term
    elif key == 'neg':
        if '=' in display.get():
            display.delete(0, tk.END)
        try:
            if display.get()[0] == '-':
                display.delete(0)
            else:
                display.insert(0, '-')
        except IndexError:
            pass

    # clear display and start new input     
    else:
        if '=' in display.get():
            display.delete(0, tk.END)
        display.insert(tk.END, key)

# RUNTIME
calc.mainloop()

 

See also  Python 3 Script to Export Source Code to PDF Document Using code2pdf Module Full Project For Beginners

 

 

Screenshot

 

 

 

 

Leave a Reply