|
| 1 | +from tkinter import * |
| 2 | + |
| 3 | +root = Tk() |
| 4 | + |
| 5 | +# Creating window title |
| 6 | +root.title("Simple Calculator") |
| 7 | + |
| 8 | +# Creating input fields |
| 9 | +e = Entry(root, width=35, borderwidth=5) |
| 10 | +e.grid(row=0, column=0, columnspan=3, padx=10, pady=10) |
| 11 | +global f_num |
| 12 | +global math |
| 13 | + |
| 14 | + |
| 15 | +# Creating functions for buttons |
| 16 | +def button_click(number): |
| 17 | + current = e.get() |
| 18 | + e.delete(0, END) |
| 19 | + e.insert(0, str(current) + str(number)) |
| 20 | + |
| 21 | + |
| 22 | +def button_clear(): |
| 23 | + e.delete(0, END) |
| 24 | + |
| 25 | + |
| 26 | +def button_add(): |
| 27 | + first_number = e.get() |
| 28 | + global f_num |
| 29 | + global math |
| 30 | + math = "addition" |
| 31 | + f_num = int(first_number) |
| 32 | + e.delete(0, END) |
| 33 | + |
| 34 | + |
| 35 | +def button_sub(): |
| 36 | + first_number = e.get() |
| 37 | + global f_num |
| 38 | + global math |
| 39 | + math = "subtraction" |
| 40 | + f_num = int(first_number) |
| 41 | + e.delete(0, END) |
| 42 | + |
| 43 | + |
| 44 | +def button_mul(): |
| 45 | + first_number = e.get() |
| 46 | + global f_num |
| 47 | + global math |
| 48 | + math = "multiplication" |
| 49 | + f_num = int(first_number) |
| 50 | + e.delete(0, END) |
| 51 | + |
| 52 | + |
| 53 | +def button_div(): |
| 54 | + first_number = e.get() |
| 55 | + global f_num |
| 56 | + global math |
| 57 | + math = "division" |
| 58 | + f_num = int(first_number) |
| 59 | + e.delete(0, END) |
| 60 | + |
| 61 | + |
| 62 | +def button_equal(): |
| 63 | + second_number = e.get() |
| 64 | + e.delete(0, END) |
| 65 | + if math == "addition": |
| 66 | + e.insert(0, f_num + int(second_number)) |
| 67 | + if math == "subtraction": |
| 68 | + e.insert(0, f_num - int(second_number)) |
| 69 | + if math == "multiplication": |
| 70 | + e.insert(0, f_num * int(second_number)) |
| 71 | + if math == "division": |
| 72 | + e.insert(0, f_num / int(second_number)) |
| 73 | + |
| 74 | + |
| 75 | +# Creating buttons |
| 76 | +button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1)) |
| 77 | +button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2)) |
| 78 | +button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3)) |
| 79 | +button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4)) |
| 80 | +button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5)) |
| 81 | +button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6)) |
| 82 | +button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7)) |
| 83 | +button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8)) |
| 84 | +button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9)) |
| 85 | +button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0)) |
| 86 | +button_add = Button(root, text="+", padx=39, pady=20, command=button_add) |
| 87 | +button_sub = Button(root, text="-", padx=41, pady=20, command=button_sub) |
| 88 | +button_mul = Button(root, text="*", padx=40, pady=20, command=button_mul) |
| 89 | +button_div = Button(root, text="/", padx=41, pady=20, command=button_div) |
| 90 | +button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal) |
| 91 | +button_clear = Button(root, text="Clear", padx=79, pady=20, command=button_clear) |
| 92 | + |
| 93 | +# Arranging buttons shape and size |
| 94 | +button_1.grid(row=3, column=0) |
| 95 | +button_2.grid(row=3, column=1) |
| 96 | +button_3.grid(row=3, column=2) |
| 97 | + |
| 98 | +button_4.grid(row=2, column=0) |
| 99 | +button_5.grid(row=2, column=1) |
| 100 | +button_6.grid(row=2, column=2) |
| 101 | + |
| 102 | +button_7.grid(row=1, column=0) |
| 103 | +button_8.grid(row=1, column=1) |
| 104 | +button_9.grid(row=1, column=2) |
| 105 | + |
| 106 | +button_0.grid(row=4, column=0) |
| 107 | +button_clear.grid(row=4, column=1, columnspan=2) |
| 108 | +button_add.grid(row=5, column=0) |
| 109 | +button_equal.grid(row=5, column=1, columnspan=2) |
| 110 | + |
| 111 | +button_sub.grid(row=6, column=0) |
| 112 | +button_mul.grid(row=6, column=1) |
| 113 | +button_div.grid(row=6, column=2) |
| 114 | + |
| 115 | +root.mainloop() |
0 commit comments