Python calculator app 🖩

preview_player
Показать описание
python calculator program project tutorial example explained

#python #calculator #program

# ****************************************************************
# Python Calculator
# ****************************************************************
from tkinter import *

def button_press(num):

global equation_text

equation_text = equation_text + str(num)

def equals():

global equation_text

try:

total = str(eval(equation_text))

equation_text = total

except SyntaxError:

equation_text = ""

except ZeroDivisionError:

equation_text = ""

def clear():

global equation_text

equation_text = ""

window = Tk()

equation_text = ""

equation_label = StringVar()

label = Label(window, textvariable=equation_label, font=('consolas',20), bg="white", width=24, height=2)

frame = Frame(window)

button1 = Button(frame, text=1, height=4, width=9, font=35,
command=lambda: button_press(1))

button2 = Button(frame, text=2, height=4, width=9, font=35,
command=lambda: button_press(2))

button3 = Button(frame, text=3, height=4, width=9, font=35,
command=lambda: button_press(3))

button4 = Button(frame, text=4, height=4, width=9, font=35,
command=lambda: button_press(4))

button5 = Button(frame, text=5, height=4, width=9, font=35,
command=lambda: button_press(5))

button6 = Button(frame, text=6, height=4, width=9, font=35,
command=lambda: button_press(6))

button7 = Button(frame, text=7, height=4, width=9, font=35,
command=lambda: button_press(7))

button8 = Button(frame, text=8, height=4, width=9, font=35,
command=lambda: button_press(8))

button9 = Button(frame, text=9, height=4, width=9, font=35,
command=lambda: button_press(9))

button0 = Button(frame, text=0, height=4, width=9, font=35,
command=lambda: button_press(0))

plus = Button(frame, text='+', height=4, width=9, font=35,
command=lambda: button_press('+'))

minus = Button(frame, text='-', height=4, width=9, font=35,
command=lambda: button_press('-'))

multiply = Button(frame, text='*', height=4, width=9, font=35,
command=lambda: button_press('*'))

divide = Button(frame, text='/', height=4, width=9, font=35,
command=lambda: button_press('/'))

equal = Button(frame, text='=', height=4, width=9, font=35,
command=equals)

decimal = Button(frame, text='.', height=4, width=9, font=35,
command=lambda: button_press('.'))

clear = Button(window, text='clear', height=4, width=12, font=35,
command=clear)

# ****************************************************************

Bro Code merch store 👟 :
===========================================================
===========================================================

music credits 🎼 :
===========================================================
Creative Commons — Attribution-ShareAlike 3.0 Unported— CC BY-SA 3.0
===========================================================
Рекомендации по теме
Комментарии
Автор

#
# Python Calculator
#
from tkinter import *

def button_press(num):

global equation_text

equation_text = equation_text + str(num)



def equals():

global equation_text

try:

total = str(eval(equation_text))

equation_label.set(total)

equation_text = total

except SyntaxError:

equation_label.set("syntax error")

equation_text = ""

except ZeroDivisionError:

error")

equation_text = ""

def clear():

global equation_text

equation_label.set("")

equation_text = ""


window = Tk()
window.title("Calculator program")
window.geometry("500x500")

equation_text = ""

equation_label = StringVar()

label = Label(window, textvariable=equation_label, font=('consolas', 20), bg="white", width=24, height=2)
label.pack()

frame = Frame(window)
frame.pack()

button1 = Button(frame, text=1, height=4, width=9, font=35,
command=lambda: button_press(1))
button1.grid(row=0, column=0)

button2 = Button(frame, text=2, height=4, width=9, font=35,
command=lambda: button_press(2))
button2.grid(row=0, column=1)

button3 = Button(frame, text=3, height=4, width=9, font=35,
command=lambda: button_press(3))
button3.grid(row=0, column=2)

button4 = Button(frame, text=4, height=4, width=9, font=35,
command=lambda: button_press(4))
button4.grid(row=1, column=0)

button5 = Button(frame, text=5, height=4, width=9, font=35,
command=lambda: button_press(5))
button5.grid(row=1, column=1)

button6 = Button(frame, text=6, height=4, width=9, font=35,
command=lambda: button_press(6))
button6.grid(row=1, column=2)

button7 = Button(frame, text=7, height=4, width=9, font=35,
command=lambda: button_press(7))
button7.grid(row=2, column=0)

button8 = Button(frame, text=8, height=4, width=9, font=35,
command=lambda: button_press(8))
button8.grid(row=2, column=1)

button9 = Button(frame, text=9, height=4, width=9, font=35,
command=lambda: button_press(9))
button9.grid(row=2, column=2)

button0 = Button(frame, text=0, height=4, width=9, font=35,
command=lambda: button_press(0))
button0.grid(row=3, column=0)

plus = Button(frame, text='+', height=4, width=9, font=35,
command=lambda: button_press('+'))
plus.grid(row=0, column=3)

minus = Button(frame, text='-', height=4, width=9, font=35,
command=lambda: button_press('-'))
minus.grid(row=1, column=3)

multiply = Button(frame, text='*', height=4, width=9, font=35,
command=lambda: button_press('*'))
multiply.grid(row=2, column=3)

divide = Button(frame, text='/', height=4, width=9, font=35,
command=lambda: button_press('/'))
divide.grid(row=3, column=3)

equal = Button(frame, text='=', height=4, width=9, font=35,
command=equals)
equal.grid(row=3, column=2)

decimal = Button(frame, text='.', height=4, width=9, font=35,
command=lambda: button_press('.'))
decimal.grid(row=3, column=1)

clear = Button(window, text='clear', height=4, width=12, font=35,
command=clear)
clear.pack()

window.mainloop()

BroCodez
Автор

At first I was like "Could you go a little more in-depth with explaining the code?" But. That's when I realized I need to go on my own for deeper explanations. It makes it so much easier and satisfying to get yourself unstuck. Great Lesson Bro!

blazer
Автор

From deep of my heart. Thanks very much!!!

juankorsia
Автор

i really love the way that you tech bro i am grateful to be taught by u thank you

baahubaliankit
Автор

thanks for the short and understandable tutorial

sjmjqwo
Автор

Made a simple calculator :)

# Calculator program
# In python

def addition(x, y):
output1 = (x + y)
return output1

def subtraction(x, y):
output2 = (x - y)
return output2

def multiplication(x, y):
output3 = (x * y)
return output3

def division(x, y):
output = (x / y)
return output

def square(z):
output4 = (z * z)
return output4

def cube(z):
output5 = (z * z * z)
return output5

def factorial_pos(z):
output6 = 1
for i in range(z, 1, -1):
output6 = (output6 * i)
return output6

def factorial_neg(z):
output7 = -1
for i in range(z, -1):
output7 = (output7 * i)
return output7

def exp_pos(b, p):
output8 = 1
for i in range(p):
output8 = (output8 * b)
return output8

def exp_neg(b, p):
output9 = -1
for i in range(p):
output9 = (output9 * b)
return output9

def mtp_tables_ps(x, y):
print("\nResult:\n")
for i in range(1, y+1):
print(f"{x} x {i} = {x*i}")
else:
print(f'''\nThese are the multiplication tables
of {x} with-in the range of {y}''')

def mtp_tables_ng(x, y):
print("\nResult:\n")
for i in range(-1, (y-1), -1):
print(f"{x} x {i} = {x*i}")
else:
print(f'''\nThese are the multiplication tables
of {x} with-in the range of {y}''')

def operator(c):
if(c == ('+')):
try:
x = float(input("Enter 1st number\n: "))
y = float(input("Enter 2nd number\n: "))
except ValueError:
print("You can only enter integers.Try to run again!")
else:
print("Result:")
print(addition(x, y))
elif(c == ('-')):
try:
x = float(input("Enter 1st number\n: "))
y = float(input("Enter 2nd number\n: "))
except ValueError:
print("You can only enter integers.Try to run again!")
else:
print("Result:")
print(subtraction(x, y))
elif(c == ('*')):
try:
x = float(input("Enter 1st number\n: "))
y = float(input("Enter 2nd number\n: "))
except ValueError:
print("You can only enter integers.Try to run again!")
else:
print("Result:")
print(multiplication(x, y))
elif(c == ('/')):
try:
x = float(input("Enter 1st number\n: "))
y = float(input("Enter 2nd number\n: "))
division(x, y)
except ValueError:
print("You can only enter integers.Try to run again!")
except ZeroDivisionError:
print("You cant divide by 0.Try to run again!")
else:
print("Result:")
print(division(x, y))
elif(c == ("**")):
try:
z = float(input("Enter the number to sqaure it\n: "))
except ValueError:
print("You can only enter integers.Try to run again!")
else:
print("Result:")
print(square(z))
elif(c == ("***")):
try:
z = float(input("Enter the number to cube it\n: "))
except ValueError:
print("You can only enter integers.Try to run again!")
else:
print("Result:")
print(cube(z))
elif(c == ('!')):
try:
z = int(input("Enter the number to calculate its factorial\n: "))
if(z > 0):
print("Result:")
print(factorial_pos(z))
elif(z < 0):
print("Result:")
print(factorial_neg(z))
else:
if(z == 0):
print("Result:")
print(z*0)
else:
pass
except ValueError:
print("You can only enter Integers.Try to run again!")
elif(c == ('exp')):
try:
b = int(input("Enter the base number\n: "))
p = int(input("Enter the power number\n: "))
if(p < 0):
print("You cant enter negative integers as Power.Try to run again!")
elif(b > 0):
print("Result:")
print(exp_pos(b, p))
elif(b < 0):
print("Result:")
print(exp_neg(b, p))
elif(b == 0):
print("Result:")
print(b*0)
else:
pass
except ValueError:
print("You can only enter Integers.Try to run again!")
elif(c == "mtp"):
try:
x = int(input("Enter the number for its multiplication tables\n: "))
y = int(input("Enter the range for the tables\n: "))
if(y > 0):
mtp_tables_ps(x, y)
elif(y < 0):
mtp_tables_ng(x, y)
else:
print("Result:")
print(x*y)
except ValueError:
print("You can only enter Integers.Try to run again!")
else:
print("Invalid operator!")

def new_program(c):
c = input('''Enter an operator sign:
('+', '-', '*', '/', '**', '***', '!', 'exp', 'mtp')\n: ''')
operator(c)

c = input('''Enter an operator sign:
('+', '-', '*', '/', '**', '***', '!', 'exp', 'mtp')\n: ''')
operator(c)
print()
ctn = input('''Do you want to continue your calculation?
Enter 'y' for Yes / 'n' for No.\n: ''')

ctn_cmd = ['y', 'Y', 'n', 'N']

while(ctn == 'y' or ctn == 'Y'):
print()
new_program(c)
print()
ctn = input('''Do you want to continue your calculation?
Enter 'y' for Yes / 'n' for No.\n: ''')
if(ctn == 'n' or ctn == 'N'):
print()
print("Thank you for using the calculator.\n")
else:
if(ctn not in ctn_cmd):
print("Invalid command!Try to run again.")
else:
pass

# Code finished successfully

adityanaik
Автор

thank you so much.. i had so much fun creating this project.
liked and subscribed

damdom
Автор

Thank you so much...it really helps me❤️

theaddictor
Автор

This is an awesome tutorial bro! Keep it up.
As a python learner I want to know how can I bind keyboard keys in this calculator.

jamil_faruk
Автор

thanks this tutorial was really helpfull

harshbhadrawale
Автор

Thankk you bro codee this is a good tkinter study

akazagiyu
Автор

I tried to code it myself before this, it went well but I made each function for each number, i probably need to learn more lol

IMCYT
Автор

How would you fix the decimal + decimal issue where it gives you a run on number?

adamritchey
Автор

realy thanks bro yu are aprofessional and this lesson is very useful thanks again ♥♥

vwrldyr
Автор

I am not gonna lie it is criminal to be this good at code

milky_edge