|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import * |
| 3 | +root = tk.Tk() |
| 4 | +root.geometry("170x230") |
| 5 | +root.title("Calculator") |
| 6 | +root.maxsize(170,230) |
| 7 | +root.minsize(170,230) |
| 8 | + |
| 9 | +#Entry Widgets to show calculations |
| 10 | +inp = Entry(root,width=16,borderwidth=3,relief=RIDGE) |
| 11 | +inp.grid(pady=10,row=0,sticky="w",padx=15) |
| 12 | + |
| 13 | + |
| 14 | +# <==================== Button Operation code starts here.. ==============> |
| 15 | +def nine(): |
| 16 | + inp.insert("end","9") |
| 17 | + |
| 18 | +def eight(): |
| 19 | + inp.insert("end","8") |
| 20 | + |
| 21 | +def seven(): |
| 22 | + inp.insert("end","7") |
| 23 | + |
| 24 | +def six(): |
| 25 | + inp.insert("end","6") |
| 26 | + |
| 27 | +def five(): |
| 28 | + inp.insert("end","5") |
| 29 | + |
| 30 | +def four(): |
| 31 | + inp.insert("end","4") |
| 32 | + |
| 33 | +def three(): |
| 34 | + inp.insert("end","3") |
| 35 | + |
| 36 | +def two(): |
| 37 | + inp.insert("end","2") |
| 38 | + |
| 39 | +def one(): |
| 40 | + inp.insert("end","1") |
| 41 | + |
| 42 | +def zero(): |
| 43 | + inp.insert("end","0") |
| 44 | + |
| 45 | +def double_zero(): |
| 46 | + inp.insert("end","00") |
| 47 | + |
| 48 | +def dot(): |
| 49 | + inp.insert("end",".") |
| 50 | + |
| 51 | +def plus(): |
| 52 | + inp.insert("end","+") |
| 53 | + |
| 54 | +def minus(): |
| 55 | + inp.insert("end","-") |
| 56 | + |
| 57 | +def mul(): |
| 58 | + inp.insert("end","*") |
| 59 | + |
| 60 | +def divide(): |
| 61 | + inp.insert("end","/") |
| 62 | + |
| 63 | +def modulus(): |
| 64 | + inp.insert("end","%") |
| 65 | + |
| 66 | +def result(): |
| 67 | + |
| 68 | + |
| 69 | + if inp.get() == "": |
| 70 | + inp.insert("end","error") |
| 71 | + elif inp.get()[0] == "0": |
| 72 | + inp.delete(0,"end") |
| 73 | + inp.insert("end","error") |
| 74 | + else: |
| 75 | + res = inp.get() |
| 76 | + res = eval(res) |
| 77 | + inp.insert("end"," = ") |
| 78 | + inp.insert("end",res) |
| 79 | + |
| 80 | +def clear(): |
| 81 | + inp.delete(0,"end") |
| 82 | + |
| 83 | + |
| 84 | +# <============ end code ================> |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +# <============= Button Design Code starts here.. ==================> |
| 89 | + |
| 90 | +clear = Button(root,text="C",width=2,command=clear,bg="red",fg="white",relief=RIDGE) |
| 91 | +clear.grid(row=0,sticky="w",padx=125) |
| 92 | + |
| 93 | + |
| 94 | +nine = Button(text="9",width=2,command=nine,borderwidth=3,relief=RIDGE) |
| 95 | +nine.grid(row=1,sticky="w",padx=15) |
| 96 | + |
| 97 | +eight = Button(text="8",width=2,command=eight,borderwidth=3,relief=RIDGE) |
| 98 | +eight.grid(row=1,sticky="w",padx=45) |
| 99 | + |
| 100 | +seven = Button(root,text="7",width=2,command=seven,borderwidth=3,relief=RIDGE) |
| 101 | +seven.grid(row=1,sticky="w",padx=75) |
| 102 | + |
| 103 | +plus = Button(root,text="+",width=2,command=plus,borderwidth=3,relief=RIDGE) |
| 104 | +plus.grid(row=1,sticky="e",padx=125) |
| 105 | + |
| 106 | + |
| 107 | +six = Button(text="6",width=2,command=six,borderwidth=3,relief=RIDGE) |
| 108 | +six.grid(row=2,sticky="w",padx=15,pady=5) |
| 109 | + |
| 110 | +five = Button(text="5",width=2,command=five,borderwidth=3,relief=RIDGE) |
| 111 | +five.grid(row=2,sticky="w",padx=45,pady=5) |
| 112 | + |
| 113 | +four = Button(root,text="4",width=2,command=four,borderwidth=3,relief=RIDGE) |
| 114 | +four.grid(row=2,sticky="w",padx=75,pady=5) |
| 115 | + |
| 116 | +minus = Button(root,text="-",width=2,command=minus,borderwidth=3,relief=RIDGE) |
| 117 | +minus.grid(row=2,sticky="e",padx=125,pady=5) |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +three = Button(text="3",width=2,command=three,borderwidth=3,relief=RIDGE) |
| 122 | +three.grid(row=3,sticky="w",padx=15,pady=5) |
| 123 | + |
| 124 | +two = Button(text="2",width=2,command=two,borderwidth=3,relief=RIDGE) |
| 125 | +two.grid(row=3,sticky="w",padx=45,pady=5) |
| 126 | + |
| 127 | +one = Button(root,text="1",width=2,command=one,borderwidth=3,relief=RIDGE) |
| 128 | +one.grid(row=3,sticky="w",padx=75,pady=5) |
| 129 | + |
| 130 | +multiply = Button(root,text="*",width=2,command=mul,borderwidth=3,relief=RIDGE) |
| 131 | +multiply.grid(row=3,sticky="e",padx=125,pady=5) |
| 132 | + |
| 133 | + |
| 134 | +zero = Button(text="0",width=2,command=zero,borderwidth=3,relief=RIDGE) |
| 135 | +zero.grid(row=4,sticky="w",padx=15,pady=5) |
| 136 | + |
| 137 | +double_zero = Button(text="00",width=2,command=double_zero,borderwidth=3,relief=RIDGE) |
| 138 | +double_zero.grid(row=4,sticky="w",padx=45,pady=5) |
| 139 | + |
| 140 | +dot = Button(root,text=".",width=2,command=dot,borderwidth=3,relief=RIDGE) |
| 141 | +dot.grid(row=4,sticky="w",padx=75,pady=5) |
| 142 | + |
| 143 | +divide = Button(root,text="/",width=2,command=divide,borderwidth=3,relief=RIDGE) |
| 144 | +divide.grid(row=4,sticky="e",padx=125,pady=5) |
| 145 | + |
| 146 | +result = Button(root,text="=",width=10,command=result,bg="red",fg="white",borderwidth=3,relief=RIDGE) |
| 147 | +result.grid(row=5,sticky="w",padx=15,pady=5) |
| 148 | + |
| 149 | +modulus = Button(root,text="%",width=2,command=modulus,borderwidth=3,relief=RIDGE) |
| 150 | +modulus.grid(row=5,sticky="e",padx=125,pady=5) |
| 151 | + |
| 152 | +root.mainloop() |
| 153 | + |
| 154 | +# <============ end code ==============> |
0 commit comments