|
| 1 | +from tkinter import * |
| 2 | + |
| 3 | +a="" |
| 4 | + |
| 5 | +def press(n): |
| 6 | + global a |
| 7 | + a+=str(n) |
| 8 | + equation.set(a) |
| 9 | + |
| 10 | +def equalpress(): |
| 11 | + try: |
| 12 | + global a |
| 13 | + t=str(eval(a)) |
| 14 | + equation.set(t) |
| 15 | + a="" |
| 16 | + except: |
| 17 | + equation.set("Error") |
| 18 | + a="" |
| 19 | + |
| 20 | +def clear(): |
| 21 | + global a |
| 22 | + a="" |
| 23 | + equation.set("") |
| 24 | + |
| 25 | +if __name__=="__main__": |
| 26 | + gui=Tk() |
| 27 | + gui.configure(background="black") |
| 28 | + gui.title("Calculator") |
| 29 | + gui.geometry("260x150") |
| 30 | + equation=StringVar() |
| 31 | + a_field=Entry(gui,textvariable=equation) |
| 32 | + a_field.grid(columnspan=4,ipadx=70) |
| 33 | + equation.set |
| 34 | + |
| 35 | +button1=Button(gui,text="1",fg="black",bg="white",command=lambda:press(1),height=1,width=7) |
| 36 | +button1.grid(row=2,column=0) |
| 37 | + |
| 38 | +button2=Button(gui,text="2",fg="black",bg="white",command=lambda:press(2),height=1,width=7) |
| 39 | +button2.grid(row=2,column=1) |
| 40 | + |
| 41 | +button3=Button(gui,text="3",fg="black",bg="white",command=lambda:press(3),height=1,width=7) |
| 42 | +button3.grid(row=2,column=2) |
| 43 | + |
| 44 | +button4=Button(gui,text="4",fg="black",bg="white",command=lambda:press(4),height=1,width=7) |
| 45 | +button4.grid(row=3,column=0) |
| 46 | + |
| 47 | +button5=Button(gui,text="5",fg="black",bg="white",command=lambda:press(5),height=1,width=7) |
| 48 | +button5.grid(row=3,column=1) |
| 49 | + |
| 50 | +button6=Button(gui,text="6",fg="black",bg="white",command=lambda:press(6),height=1,width=7) |
| 51 | +button6.grid(row=3,column=2) |
| 52 | + |
| 53 | +button7=Button(gui,text="7",fg="black",bg="white",command=lambda:press(7),height=1,width=7) |
| 54 | +button7.grid(row=4,column=0) |
| 55 | + |
| 56 | +button8=Button(gui,text="8",fg="black",bg="white",command=lambda:press(8),height=1,width=7) |
| 57 | +button8.grid(row=4,column=1) |
| 58 | + |
| 59 | +button9=Button(gui,text="9",fg="black",bg="white",command=lambda:press(9),height=1,width=7) |
| 60 | +button9.grid(row=4,column=2) |
| 61 | + |
| 62 | +button0=Button(gui,text="0",fg="black",bg="white",command=lambda:press(0),height=1,width=7) |
| 63 | +button0.grid(row=5,column=0) |
| 64 | + |
| 65 | +plus=Button(gui,text="+",fg="black",bg="white",command=lambda:press("+"),height=1,width=7) |
| 66 | +plus.grid(row=2,column=3) |
| 67 | + |
| 68 | +minus=Button(gui,text="-",fg="black",bg="white",command=lambda:press("-"),height=1,width=7) |
| 69 | +minus.grid(row=3,column=3) |
| 70 | + |
| 71 | +multiply=Button(gui,text="*",fg="black",bg="white",command=lambda:press("*"),height=1,width=7) |
| 72 | +multiply.grid(row=4,column=3) |
| 73 | + |
| 74 | +divide=Button(gui,text="/",fg="black",bg="white",command=lambda:press("/"),height=1,width=7) |
| 75 | +divide.grid(row=5,column=3) |
| 76 | + |
| 77 | +equal=Button(gui,text="=",fg="black",bg="white",command=equalpress,height=1,width=7) |
| 78 | +equal.grid(row=5,column=2) |
| 79 | + |
| 80 | +clear=Button(gui,text="C",fg="black",bg="white",command=clear,height=1,width=7) |
| 81 | +clear.grid(row=5,column=1) |
| 82 | + |
| 83 | +decimal=Button(gui,text=".",fg="black",bg="white",command=lambda:press("."),height=1,width=7) |
| 84 | +decimal.grid(row=6,column=0) |
| 85 | + |
| 86 | +gui.mainloop() |
0 commit comments