Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.
Merged
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
3 changes: 2 additions & 1 deletion projects/Create_calculator_app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Python 3

### Screenshot/GIF showing the sample use of the script
<!--Remove the below lines and add yours -->
![ ](https://github.com/Python-World/python-mini-projects/blob/master/projects/Create_calculator_app/output.png)
![Output](https://user-images.githubusercontent.com/53505850/95007683-0d85c700-0630-11eb-9aa4-7125f6be1ea0.PNG)


## *Author Name*
<!--Remove the below lines and add yours -->
Expand Down
45 changes: 35 additions & 10 deletions projects/Create_calculator_app/calculator.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from tkinter import Tk, END, Entry, N, E, S, W, Button
from tkinter import font
from tkinter import Label
from functools import partial



def get_input(entry, argu):
entry.insert(END, argu)

Expand All @@ -20,30 +20,49 @@ def clear(entry):

def calc(entry):
input_info = entry.get()
output = str(eval(input_info.strip()))
try:
output = str(eval(input_info.strip()))
except ZeroDivisionError:
popupmsg()
output = ""
clear(entry)
entry.insert(END, output)


def popupmsg():
popup = Tk()
popup.resizable(0, 0)
popup.geometry("120x100")
popup.title("Alert")
label = Label(popup, text="Cannot divide by 0 ! \n Enter valid values")
label.pack(side="top", fill="x", pady=10)
B1 = Button(popup, text="Okay", bg="#DDDDDD", command=popup.destroy)
B1.pack()


def cal():
root = Tk()
root.title("Calc")
root.resizable(0, 0)

entry_font = font.Font(size=15)
entry = Entry(root, justify="right", font=entry_font)
entry.grid(row=0, column=0, columnspan=4, sticky=N + W + S + E, padx=5, pady=5)
entry.grid(row=0, column=0, columnspan=4,
sticky=N + W + S + E, padx=5, pady=5)

cal_button_bg = '#FF6600'
num_button_bg = '#4B4B4B'
other_button_bg = '#DDDDDD'
text_fg = '#FFFFFF'
button_active_bg = '#C0C0C0'

num_button = partial(Button, root, fg=text_fg, bg=num_button_bg, padx=10, pady=3, activebackground=button_active_bg)
cal_button = partial(Button, root, fg=text_fg, bg=cal_button_bg, padx=10, pady=3, activebackground=button_active_bg)
num_button = partial(Button, root, fg=text_fg, bg=num_button_bg,
padx=10, pady=3, activebackground=button_active_bg)
cal_button = partial(Button, root, fg=text_fg, bg=cal_button_bg,
padx=10, pady=3, activebackground=button_active_bg)

button7 = num_button(text='7', bg=num_button_bg, command=lambda: get_input(entry, '7'))
button7 = num_button(text='7', bg=num_button_bg,
command=lambda: get_input(entry, '7'))
button7.grid(row=2, column=0, pady=5)

button8 = num_button(text='8', command=lambda: get_input(entry, '8'))
Expand Down Expand Up @@ -80,18 +99,20 @@ def cal():
button12.grid(row=2, column=3, pady=5)

button0 = num_button(text='0', command=lambda: get_input(entry, '0'))
button0.grid(row=5, column=0, columnspan=2, padx=3, pady=5, sticky=N + S + E + W)
#button0.grid(row=5, column=0, columnspan=2, padx=3, pady=5, sticky=N + S + E + W)
button0.grid(row=5, column=0, pady=5)

button13 = num_button(text='.', command=lambda: get_input(entry, '.'))
button13.grid(row=5, column=2, pady=5)
button13.grid(row=5, column=1, pady=5)

button14 = Button(root, text='/', fg=text_fg, bg=cal_button_bg, padx=10, pady=3,
command=lambda: get_input(entry, '/'))
button14.grid(row=1, column=3, pady=5)

button15 = Button(root, text='<-', bg=other_button_bg, padx=10, pady=3,
command=lambda: backspace(entry), activebackground=button_active_bg)
button15.grid(row=1, column=0, columnspan=2, padx=3, pady=5, sticky=N + S + E + W)
button15.grid(row=1, column=0, columnspan=2,
padx=3, pady=5, sticky=N + S + E + W)

button16 = Button(root, text='C', bg=other_button_bg, padx=10, pady=3,
command=lambda: clear(entry), activebackground=button_active_bg)
Expand All @@ -101,6 +122,10 @@ def cal():
command=lambda: calc(entry), activebackground=button_active_bg)
button17.grid(row=5, column=3, pady=5)

button18 = Button(root, text='^', fg=text_fg, bg=cal_button_bg, padx=10, pady=3,
command=lambda: get_input(entry, '**'))
button18.grid(row=5, column=2, pady=5)

root.mainloop()


Expand Down
Binary file modified projects/Create_calculator_app/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.