|
| 1 | +# ==================== Libraries ==================== |
| 2 | +import tkinter as tk |
| 3 | +from tkinter import ttk |
| 4 | +from tkinter import messagebox |
| 5 | +# =================================================== |
| 6 | +# ==================== Classes ====================== |
| 7 | + |
| 8 | + |
| 9 | +class Inside: |
| 10 | + def __init__(self, parent): |
| 11 | + self.parent = parent |
| 12 | + # ----- Main Frame ----- |
| 13 | + self.cal_frame = ttk.Frame(self.parent) |
| 14 | + self.cal_frame.grid(row=0, column=0) |
| 15 | + # ---------------------- |
| 16 | + # ----- Variable For Main Output ----- |
| 17 | + self.out_var = tk.StringVar() |
| 18 | + # ----- Operator Chooser ----- |
| 19 | + self.opr = tk.StringVar() |
| 20 | + # ----- Values Holder ----- |
| 21 | + self.value1 = tk.StringVar() |
| 22 | + self.value2 = tk.StringVar() |
| 23 | + # ------------------------------------ |
| 24 | + self.output_box() # <---------- Output Box Shower |
| 25 | + self.cal_buttons() # <---------- Buttons On Calculator |
| 26 | + |
| 27 | + def output_box(self): |
| 28 | + show = ttk.Entry(self.cal_frame, textvariable=self.out_var, width=20, font=('calibri', 16), state='readonly') |
| 29 | + show.grid(row=0, column=0, sticky=tk.W, ipady=6, ipadx=1, columnspan=4) |
| 30 | + show.focus() |
| 31 | + |
| 32 | + # ========== * Button Events * ========== < --- Sequence 789456123 |
| 33 | + def press_7(self): |
| 34 | + current = self.out_var.get() |
| 35 | + if current == '': |
| 36 | + self.out_var.set(7) |
| 37 | + else: |
| 38 | + current += str(7) |
| 39 | + self.out_var.set(current) |
| 40 | + |
| 41 | + def press_8(self): |
| 42 | + current = self.out_var.get() |
| 43 | + if current == '': |
| 44 | + self.out_var.set(8) |
| 45 | + else: |
| 46 | + current += str(8) |
| 47 | + self.out_var.set(current) |
| 48 | + |
| 49 | + def press_9(self): |
| 50 | + current = self.out_var.get() |
| 51 | + if current == '': |
| 52 | + self.out_var.set(9) |
| 53 | + else: |
| 54 | + current += str(9) |
| 55 | + self.out_var.set(current) |
| 56 | + |
| 57 | + def press_4(self): |
| 58 | + current = self.out_var.get() |
| 59 | + if current == '': |
| 60 | + self.out_var.set(4) |
| 61 | + else: |
| 62 | + current += str(4) |
| 63 | + self.out_var.set(current) |
| 64 | + |
| 65 | + def press_5(self): |
| 66 | + current = self.out_var.get() |
| 67 | + if current == '': |
| 68 | + self.out_var.set(5) |
| 69 | + else: |
| 70 | + current += str(5) |
| 71 | + self.out_var.set(current) |
| 72 | + |
| 73 | + def press_6(self): |
| 74 | + current = self.out_var.get() |
| 75 | + if current == '': |
| 76 | + self.out_var.set(6) |
| 77 | + else: |
| 78 | + current += str(6) |
| 79 | + self.out_var.set(current) |
| 80 | + |
| 81 | + def press_1(self): |
| 82 | + current = self.out_var.get() |
| 83 | + if current == '': |
| 84 | + self.out_var.set(1) |
| 85 | + else: |
| 86 | + current += str(1) |
| 87 | + self.out_var.set(current) |
| 88 | + |
| 89 | + def press_2(self): |
| 90 | + current = self.out_var.get() |
| 91 | + if current == '': |
| 92 | + self.out_var.set(2) |
| 93 | + else: |
| 94 | + current += str(2) |
| 95 | + self.out_var.set(current) |
| 96 | + |
| 97 | + def press_3(self): |
| 98 | + current = self.out_var.get() |
| 99 | + if current == '': |
| 100 | + self.out_var.set(3) |
| 101 | + else: |
| 102 | + current += str(3) |
| 103 | + self.out_var.set(current) |
| 104 | + |
| 105 | + def press_0(self): |
| 106 | + current = self.out_var.get() |
| 107 | + if current == '': |
| 108 | + self.out_var.set(0) |
| 109 | + else: |
| 110 | + current += str(0) |
| 111 | + self.out_var.set(current) |
| 112 | + |
| 113 | + # ========== Operators Button Handling Function ========== |
| 114 | + def press_clear(self): |
| 115 | + self.out_var.set('') |
| 116 | + |
| 117 | + def press_reset(self): |
| 118 | + self.out_var.set('') |
| 119 | + |
| 120 | + def press_plus(self): |
| 121 | + self.value1 = self.out_var.get() |
| 122 | + if self.value1 == '': |
| 123 | + messagebox.showwarning('Operator Before Number', 'Please Enter Number Before Operator') |
| 124 | + else: |
| 125 | + self.out_var.set('') |
| 126 | + self.opr = '+' |
| 127 | + |
| 128 | + def press_min(self): |
| 129 | + self.value1 = self.out_var.get() |
| 130 | + if self.value1 == '': |
| 131 | + messagebox.showwarning('Operator Before Number', 'Please Enter Number Before Operator') |
| 132 | + else: |
| 133 | + self.out_var.set('') |
| 134 | + self.opr = '-' |
| 135 | + |
| 136 | + def press_mul(self): |
| 137 | + self.value1 = self.out_var.get() |
| 138 | + if self.value1 == '': |
| 139 | + messagebox.showwarning('Operator Before Number', 'Please Enter Number Before Operator') |
| 140 | + else: |
| 141 | + self.out_var.set('') |
| 142 | + self.opr = '*' |
| 143 | + |
| 144 | + def press_div(self): |
| 145 | + self.value1 = self.out_var.get() |
| 146 | + if self.value1 == '': |
| 147 | + messagebox.showwarning('Operator Before Number', 'Please Enter Number Before Operator') |
| 148 | + else: |
| 149 | + self.out_var.set('') |
| 150 | + self.opr = '/' |
| 151 | + |
| 152 | + # ============================================== |
| 153 | + # ========== ***** Equal Button Function ***** ========== |
| 154 | + def press_equal(self): |
| 155 | + self.value2 = self.out_var.get() |
| 156 | + if self.value2 == '': |
| 157 | + messagebox.showerror('Second Number', 'Please Enter Second Number To Perform Calculation') |
| 158 | + else: |
| 159 | + |
| 160 | + try: |
| 161 | + if self.opr == '+': |
| 162 | + self.value1 = int(self.value1) |
| 163 | + self.value2 = int(self.value2) |
| 164 | + result = self.value1 + self.value2 |
| 165 | + self.out_var.set(result) |
| 166 | + if self.opr == '-': |
| 167 | + self.value1 = int(self.value1) |
| 168 | + self.value2 = int(self.value2) |
| 169 | + result = self.value1 - self.value2 |
| 170 | + self.out_var.set(result) |
| 171 | + if self.opr == '*': |
| 172 | + self.value1 = int(self.value1) |
| 173 | + self.value2 = int(self.value2) |
| 174 | + result = self.value1 * self.value2 |
| 175 | + self.out_var.set(result) |
| 176 | + if self.opr == '/': |
| 177 | + self.value1 = int(self.value1) |
| 178 | + self.value2 = int(self.value2) |
| 179 | + result = self.value1 / self.value2 |
| 180 | + self.out_var.set(result) |
| 181 | + |
| 182 | + except ValueError: |
| 183 | + messagebox.showinfo('Restart', 'Please Close And Restart Application...Sorry') |
| 184 | + |
| 185 | + def cal_buttons(self): |
| 186 | + # ===== Row 1 ===== |
| 187 | + btn_c = tk.Button(self.cal_frame, text='Clear', width=6, height=2, bd=2, bg='#58a8e0', command=self.press_clear) |
| 188 | + btn_c.grid(row=1, column=0, sticky=tk.W, pady=5) |
| 189 | + btn_div = tk.Button(self.cal_frame, text='/', width=6, height=2, bd=2, bg='#58a8e0', command=self.press_div) |
| 190 | + btn_div.grid(row=1, column=1, sticky=tk.W) |
| 191 | + btn_mul = tk.Button(self.cal_frame, text='*', width=6, height=2, bd=2, bg='#58a8e0', command=self.press_mul) |
| 192 | + btn_mul.grid(row=1, column=2, sticky=tk.E) |
| 193 | + btn_min = tk.Button(self.cal_frame, text='-', width=6, height=2, bd=2, bg='#58a8e0', command=self.press_min) |
| 194 | + btn_min.grid(row=1, column=3, sticky=tk.E) |
| 195 | + # ===== Row 2 ===== |
| 196 | + btn_7 = tk.Button(self.cal_frame, text='7', width=6, height=2, bd=2, bg='#90a9b8', command=self.press_7) |
| 197 | + btn_7.grid(row=2, column=0, sticky=tk.W, pady=2) |
| 198 | + btn_8 = tk.Button(self.cal_frame, text='8', width=6, height=2, bd=2, bg='#90a9b8', command=self.press_8) |
| 199 | + btn_8.grid(row=2, column=1, sticky=tk.W) |
| 200 | + btn_9 = tk.Button(self.cal_frame, text='9', width=6, height=2, bd=2, bg='#90a9b8', command=self.press_9) |
| 201 | + btn_9.grid(row=2, column=2, sticky=tk.E) |
| 202 | + btn_plus = tk.Button(self.cal_frame, text='+', width=6, height=5, bd=2, bg='#58a8e0', command=self.press_plus) |
| 203 | + btn_plus.grid(row=2, column=3, sticky=tk.E, rowspan=2) |
| 204 | + # ===== Row 3 ===== |
| 205 | + btn_4 = tk.Button(self.cal_frame, text='4', width=6, height=2, bd=2, bg='#90a9b8', command=self.press_4) |
| 206 | + btn_4.grid(row=3, column=0, sticky=tk.W, pady=2) |
| 207 | + btn_5 = tk.Button(self.cal_frame, text='5', width=6, height=2, bd=2, bg='#90a9b8', command=self.press_5) |
| 208 | + btn_5.grid(row=3, column=1, sticky=tk.W) |
| 209 | + btn_6 = tk.Button(self.cal_frame, text='6', width=6, height=2, bd=2, bg='#90a9b8', command=self.press_6) |
| 210 | + btn_6.grid(row=3, column=2, sticky=tk.E) |
| 211 | + # ===== Row 4 ===== |
| 212 | + btn_1 = tk.Button(self.cal_frame, text='1', width=6, height=2, bd=2, bg='#90a9b8', command=self.press_1) |
| 213 | + btn_1.grid(row=4, column=0, sticky=tk.W, pady=2) |
| 214 | + btn_2 = tk.Button(self.cal_frame, text='2', width=6, height=2, bd=2, bg='#90a9b8', command=self.press_2) |
| 215 | + btn_2.grid(row=4, column=1, sticky=tk.W) |
| 216 | + btn_3 = tk.Button(self.cal_frame, text='3', width=6, height=2, bd=2, bg='#90a9b8', command=self.press_3) |
| 217 | + btn_3.grid(row=4, column=2, sticky=tk.E) |
| 218 | + btn_equal = tk.Button(self.cal_frame, text='=', width=6, height=5, bd=2, bg='orange', command=self.press_equal) |
| 219 | + btn_equal.grid(row=4, column=3, sticky=tk.E, rowspan=2) |
| 220 | + # ===== Row 5 ===== |
| 221 | + btn_0 = tk.Button(self.cal_frame, text='0', width=14, height=2, bd=2, bg='#90a9b8', command=self.press_0) |
| 222 | + btn_0.grid(row=5, column=0, sticky=tk.W, pady=2, columnspan=2) |
| 223 | + btn_reset = tk.Button(self.cal_frame, text='Reset', width=6, height=2, bd=2, bg='#90a9b8', command=self.press_reset) |
| 224 | + btn_reset.grid(row=5, column=2, sticky=tk.E) |
| 225 | + |
| 226 | + |
| 227 | +class Main(tk.Tk): |
| 228 | + def __init__(self, *args, **kwargs): |
| 229 | + super().__init__(*args, **kwargs) |
| 230 | + # ----- Title ----- |
| 231 | + self.title('Calculator') |
| 232 | + # ----------------- |
| 233 | + # ----- Geometry Settings ----- |
| 234 | + self.geometry_settings() |
| 235 | + # ----------------------------- |
| 236 | + |
| 237 | + def geometry_settings(self): |
| 238 | + _com_width = self.winfo_screenwidth() |
| 239 | + _com_height = self.winfo_screenheight() |
| 240 | + _my_width = 450 |
| 241 | + _my_height = 450 |
| 242 | + _x = int(_com_width/2 - _my_width/2) |
| 243 | + _y = int(_com_height/2 - _my_height/2) |
| 244 | + geo_string = str(_my_width)+"x"+str(_my_height)+"+"+str(_x)+"+"+str(_y) |
| 245 | + # ----- Setting Now ----- |
| 246 | + self.geometry(geo_string) |
| 247 | + self.resizable(width=False, height=False) |
| 248 | + # ----------------------- |
| 249 | + |
| 250 | + |
| 251 | +# =================== Running The Application ======= |
| 252 | +if __name__ == "__main__": |
| 253 | + calculator = Main() |
| 254 | + Inside(calculator) |
| 255 | + calculator.mainloop() |
0 commit comments