making simple calculator with python Tkinter

 Tkinter beginners project

calculator using Tkinter python


from tkinter import *

root = Tk()
root.title("calculater")
e = Entry(root, width=50)
e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)


def button_add(number):
currnt = e.get()
e.delete(0, END)
e.insert(0, str(currnt) + str(number))


def button_clear():
e.delete(0, END)

def button_sum():
number1 = e.get()
global fnumber
global math
math='addition'
fnumber = int(number1)
e.delete(0, END)

def button_equals():
number2 = e.get()
e.delete(0, END)
if math=="addition":
e.insert(0, fnumber + int(number2))
elif math=='subtract':
e.insert(0,fnumber-int(number2))
elif math=='multiply':
e.insert(0,fnumber * int(number2))
elif math=='division':
e.insert(0,fnumber / int(number2))
elif math=='factorial':
fact=1
for i in range(1,fnumber+1):
fact=fact*i
e.insert(0,fact)
def button_subtract():
number1 = e.get()
global fnumber
global math
math='subtract'
fnumber = int(number1)
e.delete(0, END)
def button_multiply():
number1 = e.get()
global fnumber
global math
math='multiply'
fnumber = int(number1)
e.delete(0, END)
def division():
number1 = e.get()
global fnumber
global math
math='division'
fnumber = int(number1)
e.delete(0, END)
def factorial():
number1 = e.get()
global fnumber
global math
math = 'factorial'
fnumber = int(number1)
e.delete(0, END)

#def buttons
button1 = Button(root, text="1", pady=20, padx=40, command=lambda: button_add(1), fg="blue")
button2 = Button(root, text="2", pady=20, padx=40, command=lambda: button_add(2), fg="blue")
button3 = Button(root, text="3", pady=20, padx=40, command=lambda: button_add(3), fg="blue")
button4 = Button(root, text="4", pady=20, padx=40, command=lambda: button_add(4), fg="blue")
button5 = Button(root, text="5", pady=20, padx=40, command=lambda: button_add(5), fg="blue")
button6 = Button(root, text="6", pady=20, padx=40, command=lambda: button_add(6), fg="blue")
button7 = Button(root, text="7", pady=20, padx=40, command=lambda: button_add(7), fg="blue")
button8 = Button(root, text="8", pady=20, padx=40, command=lambda: button_add(8), fg="blue")
button9 = Button(root, text="9", pady=20, padx=40, command=lambda: button_add(9), fg="blue")
button0 = Button(root, text="0", pady=20, padx=40, command=lambda: button_add(0), fg="blue")
buttonx = Button(root, text="+", pady=20, padx=40, command=button_sum, fg="blue")
buttone = Button(root, text="=", pady=20, padx=40, command=button_equals, fg="blue")
button_clear = Button(root, text="clear", pady=20, padx=85, command=button_clear, fg="blue")
button_sub = Button(root, text="-", pady=20, padx=40, command=button_subtract, fg="blue")
button_mul = Button(root, text="x", pady=20, padx=40, command=button_multiply, fg="blue")
button_div = Button(root, text="/", pady=20, padx=40, command=division, fg="blue")
button_fact=Button(root, text='!',pady=20,padx=40,command=factorial,fg='blue')
#setting button on screen
button1.grid(row=1, column=0)
button2.grid(row=2, column=0)
button3.grid(row=3, column=0)
button4.grid(row=1, column=1)
button5.grid(row=2, column=1)
button6.grid(row=3, column=1)
button7.grid(row=1, column=2)
button8.grid(row=2, column=2)
button9.grid(row=3, column=2)
button0.grid(row=4, column=1)
buttonx.grid(row=5, column=1)
buttone.grid(row=5, column=0)
button_clear.grid(row=6,column=1,columnspan=85)
button_sub.grid(row=4, column=0)
button_mul.grid(row=4,column=2)
button_div.grid(row=5,column=2)
button_fact.grid(row=6,column=0)
# opening a widget
root.mainloop()


Comments

Popular Posts