├── README.md └── source_code └── calculator.py /README.md: -------------------------------------------------------------------------------- 1 | # Python-Calculator 2 | A calculator program made with python language 3 | -------------------------------------------------------------------------------- /source_code/calculator.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | def button_press(num): 4 | 5 | global equation_text 6 | 7 | equation_text = equation_text + str(num) 8 | 9 | equation_label.set(equation_text) 10 | 11 | def equals(): 12 | 13 | global equation_text 14 | 15 | try: 16 | 17 | total = str(eval(equation_text)) 18 | 19 | equation_label.set(total) 20 | 21 | equation_text = total 22 | 23 | except SyntaxError: 24 | 25 | equation_label.set("syntax error") 26 | 27 | equation_text = "" 28 | 29 | except ZeroDivisionError: 30 | 31 | equation_label.set("arithmetic error") 32 | 33 | equation_text = "" 34 | 35 | def clear(): 36 | 37 | global equation_text 38 | 39 | equation_label.set("") 40 | 41 | equation_text = "" 42 | 43 | 44 | window = Tk() 45 | window.title("Calculator program") 46 | window.geometry("500x500") 47 | 48 | equation_text = "" 49 | 50 | equation_label = StringVar() 51 | 52 | label = Label(window, textvariable=equation_label, font=('consolas',20), bg="white", width=24, height=2) 53 | label.pack() 54 | 55 | frame = Frame(window) 56 | frame.pack() 57 | 58 | button1 = Button(frame, text=1, height=4, width=9, font=35, 59 | command=lambda: button_press(1)) 60 | button1.grid(row=0, column=0) 61 | 62 | button2 = Button(frame, text=2, height=4, width=9, font=35, 63 | command=lambda: button_press(2)) 64 | button2.grid(row=0, column=1) 65 | 66 | button3 = Button(frame, text=3, height=4, width=9, font=35, 67 | command=lambda: button_press(3)) 68 | button3.grid(row=0, column=2) 69 | 70 | button4 = Button(frame, text=4, height=4, width=9, font=35, 71 | command=lambda: button_press(4)) 72 | button4.grid(row=1, column=0) 73 | 74 | button5 = Button(frame, text=5, height=4, width=9, font=35, 75 | command=lambda: button_press(5)) 76 | button5.grid(row=1, column=1) 77 | 78 | button6 = Button(frame, text=6, height=4, width=9, font=35, 79 | command=lambda: button_press(6)) 80 | button6.grid(row=1, column=2) 81 | 82 | button7 = Button(frame, text=7, height=4, width=9, font=35, 83 | command=lambda: button_press(7)) 84 | button7.grid(row=2, column=0) 85 | 86 | button8 = Button(frame, text=8, height=4, width=9, font=35, 87 | command=lambda: button_press(8)) 88 | button8.grid(row=2, column=1) 89 | 90 | button9 = Button(frame, text=9, height=4, width=9, font=35, 91 | command=lambda: button_press(9)) 92 | button9.grid(row=2, column=2) 93 | 94 | button0 = Button(frame, text=0, height=4, width=9, font=35, 95 | command=lambda: button_press(0)) 96 | button0.grid(row=3, column=0) 97 | 98 | plus = Button(frame, text='+', height=4, width=9, font=35, 99 | command=lambda: button_press('+')) 100 | plus.grid(row=0, column=3) 101 | 102 | minus = Button(frame, text='-', height=4, width=9, font=35, 103 | command=lambda: button_press('-')) 104 | minus.grid(row=1, column=3) 105 | 106 | multiply = Button(frame, text='*', height=4, width=9, font=35, 107 | command=lambda: button_press('*')) 108 | multiply.grid(row=2, column=3) 109 | 110 | divide = Button(frame, text='/', height=4, width=9, font=35, 111 | command=lambda: button_press('/')) 112 | divide.grid(row=3, column=3) 113 | 114 | equal = Button(frame, text='=', height=4, width=9, font=35, 115 | command=equals) 116 | equal.grid(row=3, column=2) 117 | 118 | decimal = Button(frame, text='.', height=4, width=9, font=35, 119 | command=lambda: button_press('.')) 120 | decimal.grid(row=3, column=1) 121 | 122 | clear = Button(window, text='clear', height=4, width=12, font=35, 123 | command=clear) 124 | clear.pack() 125 | 126 | window.mainloop() 127 | --------------------------------------------------------------------------------