Untitled
unknown
plain_text
4 years ago
4.2 kB
14
Indexable
from tkinter import *
def handleButtonClick(number):
global operator
operator += str(number)
text_Input.set(operator)
def handleButtonClear():
global operator
operator=""
text_Input.set("")
def handleResult():
global operator
try:
result=str(eval(operator))
text_Input.set(result)
operator=""
except:
text_Input.set("ERROR")
operator=""
root=Tk()
root.title('Calculator')
operator=""
text_Input = StringVar()
txtDisplay = Entry(root, width=30, font=('arial', 20,'bold'), textvariable=text_Input, bd=5, insertwidth=4, bg='white',justify='right').grid(columnspan=4)
button7 = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text=7, command=lambda:handleButtonClick('7') ,bg='white').grid(row=1, column=0)
button8 = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text=8, command=lambda:handleButtonClick('8'), bg='white').grid(row=1, column=1)
button9 = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text=9, command=lambda:handleButtonClick('9'), bg='white').grid(row=1, column=2)
buttonDe = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text='/', command=lambda:handleButtonClick('/'), bg='white').grid(row=1, column=3)
button4 = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text=4, command=lambda:handleButtonClick('4'), bg='white').grid(row=2, column=0)
button5 = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text=5, command=lambda:handleButtonClick('5'), bg='white').grid(row=2, column=1)
button6 = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text=6, command=lambda:handleButtonClick('6'), bg='white').grid(row=2, column=2)
buttonMu = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text='*', command=lambda:handleButtonClick('*'), bg='white').grid(row=2, column=3)
button1 = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text=1, command=lambda:handleButtonClick('1'), bg='white').grid(row=3, column=0)
button2 = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text=2, command=lambda:handleButtonClick('2'), bg='white').grid(row=3, column=1)
button3 = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text=3, command=lambda:handleButtonClick('3'), bg='white').grid(row=3, column=2)
buttonSub = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text='-', command=lambda:handleButtonClick('-'), bg='white').grid(row=3, column=3)
buttonClear = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text='c', command=lambda:handleButtonClear(), bg='white').grid(row=4, column=0)
buttonDot = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text='.', command=lambda:handleButtonClick('.'), bg='white').grid(row=4, column=1)
button0 = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text=0, command=lambda:handleButtonClick('0'), bg='white').grid(row=4, column=2)
buttonAdd = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text='+', command=lambda:handleButtonClick('+'), bg='white').grid(row=4, column=3)
buttonOpen = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text='(', command=lambda:handleButtonClick('('), bg='white').grid(row=5, column=0)
buttonClose = Button(root, padx=20, width=2 , height=1 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text=')', command=lambda:handleButtonClick(')'), bg='white').grid(row=5, column=1)
buttonRs = Button(root, padx=78 ,pady=10, bd=4, fg='black',font=('arial', 20,'bold'), text='=', command=lambda:handleResult(), bg='white').grid(row=5, column=2, columnspan=2)
root.mainloop()Editor is loading...