Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added py-calculator #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions Problem Statement 3/Py-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
##Python - Calculator
##GUI Calculator made in python using tkinter


Py-Calculator is a GUI calculator application with all the basic functionalities of a calculator written completely in Python3 using the tkinter framework.

It can run on any system running Python 3 or above and having tkinter library.

- Light weight
- No hidden attributes
- Cross platform support
- Easy to understand source code

## Requirements
For the program to run, it needs the following **dependencies** to be installed in python:
- math
- tkinter
- tkinter.constants
- Typing

*If not present, then install the libraries using pip or any other way of your choice.

## How to run
As mentioned above, python version 3 is required to run this program.
- First navigate to the 'Py-Calculator' folder.
- Then run he following command:
- python3 calculator.py

185 changes: 185 additions & 0 deletions Problem Statement 3/Py-Calculator/calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import math
import tkinter as tk
from tkinter.constants import COMMAND, CURRENT, END, INSERT
from typing import Text


class Button(tk.Button):
def __init__(self, master, **kw):
tk.Button.__init__(self,master=master,**kw)
self.defaultBackground = self["background"]
self.bind("<Enter>", self.on_enter)
self.bind("<Leave>", self.on_leave)

def on_enter(self, e):
self['background'] = self['activebackground']

def on_leave(self, e):
self['background'] = self.defaultBackground


ui = tk.Tk()
ui.title("Calculator")

ui.geometry("287x350")
ui.attributes('-topmost', True)

photo = tk.PhotoImage(file = "Images/icon.png")
ui.iconphoto(False, photo)

def lab(val):
tk.Label(ui, text=str(val), width=5, borderwidth=3).grid(row=0, column=4, pady=8, columnspan=2)

lab(": )")

e = tk.Entry(ui, font=("default, 11"), insertontime=0, bd=5, width=21, borderwidth=10, foreground="#ff0000", highlightthickness=5, highlightcolor="#00CFFF", highlightbackground="#00CFFF")
e.grid(row=0, rowspan=2, column=0, columnspan=4, padx=5, pady=5)
e.bind("<Key>", lambda e: "break")

def view(val):
tk.Label(ui, borderwidth=3, relief="sunken", text=str(val), width=34, bg="#4EFF61", fg="#000000").grid(row=2, column=0, columnspan=5, pady=5)

view("Calculations here")

global flag
flag = 0

def insrt(number):
global flag
if flag == 1:
e.delete(0, END)
view("Calculations here")
lab(": )")
flag = 0
current=e.get()
e.delete(0, END)
e.insert(0, str(current)+str(number))

def addition(s):
lab(s)
global stat
stat = str(s)
global num
num = e.get()
e.delete(0, END)

def substraction(s):
lab(s)
global stat
stat = str(s)
global num
num = e.get()
e.delete(0, END)

def multiplication(s):
lab(s)
global stat
stat = str(s)
global num
num = e.get()
e.delete(0, END)

def division(s):
lab(s)
global stat
stat = str(s)
global num
num = e.get()
e.delete(0, END)

def root(s):
lab(s)
global num
num = e.get()
e.delete(0, END)
e.insert(0, str("Root of: "))
global flag
flag = 0
global stat
stat = str(s)
res(s)

def power(s):
lab(s)
global num
num = e.get()
e.delete(0, END)
e.insert(0, str("Power of: "))
global flag
flag = 0
global stat
stat = str(s)
res(s)

def res(s):
lab(s)
global stat
num2 = e.get()
if stat == "+":
ans1 = float(num) + float(num2)
ans = ans1
elif stat == "-":
ans1 = float(num) - float(num2)
ans = ans1
elif stat == "x":
ans1 = float(num) * float(num2)
ans = ans1
elif stat == "/":
ans1 = float(num) / float(num2)
ans = "{:.3f}".format(ans1)
elif stat == "root":
num2 = str(num2[9:])
ans1 = float(math.pow(float(num), float(1/int(num2))))
ans = "{:.2f}".format(ans1)
elif stat == "pow":
num2 = str(num2[10:])
ans1 = float(math.pow(float(num), int(num2)))
ans = "{:.2f}".format(ans1)
e.delete(0, END)
e.insert(0, ans)
global flag
flag = 1
if stat == "root":
view(str(num2) + " root of " + str(num) + " = " + str(ans1))
elif stat == "pow":
view(str(num) + " power " + str(num2) + " = " + str(ans1))
else:
view(str(num) + " " + str(stat) + " " + str(num2) + " = " + str(ans1))


def clear():
lab(": |")
view("Calculations here")
global flag
flag = 1
e.delete(0, END)


sub = Button(ui,activebackground='#F2FF02', bg="#FF0006", text="-", padx=18, pady=15, command=lambda: substraction("-")).grid(row=5, column=3)
mul = Button(ui,activebackground='#F2FF02', bg="#FF0006", text="x", padx=17, pady=15, command=lambda: multiplication("x")).grid(row=4, column=3)
div = Button(ui,activebackground='#F2FF02', bg="#FF0006", text="/", padx=19, pady=8, command=lambda: division("/")).grid(row=3, column=3)
cls = Button(ui,activebackground='#EE0D11',bg="#14FF02", text="cls", padx=15, pady=8, command= clear).grid(row=3, column=0)

b7 = Button(ui,activebackground='#14FF02', bg="#77FDFF", text="7", padx=20, pady=15, command=lambda: insrt("7")).grid(row=4, column=0)
b8 = Button(ui,activebackground='#14FF02', bg="#77FDFF", text="8", padx=20, pady=15, command=lambda: insrt("8")).grid(row=4, column=1)
b9 = Button(ui,activebackground='#14FF02', bg="#77FDFF", text="9", padx=20, pady=15, command=lambda: insrt("9")).grid(row=4, column=2)
add = Button(ui,activebackground='#F2FF02', bg="#FF0006", text="+", padx=15, pady=42, command=lambda: addition("+")).grid(row=6, column=3, rowspan=2)

b4 = Button(ui,activebackground='#14FF02', bg="#77FDFF", text="4", padx=20, pady=15, command=lambda: insrt("4")).grid(row=5, column=0)
b5 = Button(ui,activebackground='#14FF02', bg="#77FDFF", text="5", padx=20, pady=15, command=lambda: insrt("5")).grid(row=5, column=1)
b6 = Button(ui,activebackground='#14FF02', bg="#77FDFF", text="6", padx=20, pady=15, command=lambda: insrt("6")).grid(row=5, column=2)

b1 = Button(ui,activebackground='#14FF02', bg="#77FDFF", text="1", padx=20, pady=15, command=lambda: insrt("1")).grid(row=6, column=0)
b2 = Button(ui,activebackground='#14FF02', bg="#77FDFF", text="2", padx=20, pady=15, command=lambda: insrt("2")).grid(row=6, column=1)
b3 = Button(ui,activebackground='#14FF02', bg="#77FDFF", text="3", padx=20, pady=15, command=lambda: insrt("3")).grid(row=6, column=2)
equall = Button(ui,activebackground='#89FFFD', bg="#FFF200", text="=", padx=18, pady=115, command=lambda: res("=")).grid(row=3, column=4, rowspan=5)

bdot = Button(ui,activebackground='#14FF02', bg="#77FDFF", text=".", padx=22, pady=15, command=lambda: insrt(".")).grid(row=7, column=2)
b0 = Button(ui,activebackground='#14FF02', bg="#77FDFF", text="0", padx=50, pady=15, command=lambda: insrt("0")).grid(row=7, column=0, columnspan=2)

rt = Button(ui,activebackground='#F2FF02', bg="#FFCC76", text="\u221A", padx=19, pady=8, command=lambda: root("root")).grid(row=3, column=1)
pow = Button(ui,activebackground='#F2FF02', bg="#FFCC76", text="pow", padx=10, pady=8, command=lambda: power("pow")).grid(row=3, column=2)


ui.resizable(False, False)
ui.mainloop()
Binary file not shown.