From 906be7b2eaa24f510e52e272c703aca0909fd9be Mon Sep 17 00:00:00 2001 From: Mosawer Mahboob Date: Fri, 13 Oct 2023 01:26:00 +0430 Subject: [PATCH 1/2] Added class inheritance to better decouple the program. --- calc.py | 124 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 94 insertions(+), 30 deletions(-) diff --git a/calc.py b/calc.py index 758bc87..dcb6187 100644 --- a/calc.py +++ b/calc.py @@ -1,18 +1,25 @@ import tkinter as tk +from dataclasses import dataclass -LARGE_FONT_STYLE = ("Arial", 40, "bold") -SMALL_FONT_STYLE = ("Arial", 16) -DIGITS_FONT_STYLE = ("Arial", 24, "bold") -DEFAULT_FONT_STYLE = ("Arial", 20) -OFF_WHITE = "#F8FAFF" -WHITE = "#FFFFFF" -LIGHT_BLUE = "#CCEDFF" -LIGHT_GRAY = "#F5F5F5" -LABEL_COLOR = "#25265E" +@dataclass +class Fonts: + LARGE_FONT_STYLE = ("Arial", 40, "bold") + SMALL_FONT_STYLE = ("Arial", 16) + DIGITS_FONT_STYLE = ("Arial", 24, "bold") + DEFAULT_FONT_STYLE = ("Arial", 20) -class Calculator: +@dataclass +class Colors: + OFF_WHITE = "#F8FAFF" + WHITE = "#FFFFFF" + LIGHT_BLUE = "#CCEDFF" + LIGHT_GRAY = "#F5F5F5" + LABEL_COLOR = "#25265E" + + +class Calculator(Colors, Fonts): def __init__(self): self.window = tk.Tk() self.window.geometry("375x667") @@ -46,10 +53,16 @@ def __init__(self): def bind_keys(self): self.window.bind("", lambda event: self.evaluate()) for key in self.digits: - self.window.bind(str(key), lambda event, digit=key: self.add_to_expression(digit)) + self.window.bind( + str(key), + lambda event, + digit=key: self.add_to_expression(digit)) for key in self.operations: - self.window.bind(key, lambda event, operator=key: self.append_operator(operator)) + self.window.bind( + key, + lambda event, + operator=key: self.append_operator(operator)) def create_special_buttons(self): self.create_clear_button() @@ -58,18 +71,30 @@ def create_special_buttons(self): self.create_sqrt_button() def create_display_labels(self): - total_label = tk.Label(self.display_frame, text=self.total_expression, anchor=tk.E, bg=LIGHT_GRAY, - fg=LABEL_COLOR, padx=24, font=SMALL_FONT_STYLE) + total_label = tk.Label( + self.display_frame, + text=self.total_expression, + anchor=tk.E, + bg=self.LIGHT_GRAY, + fg=self.LABEL_COLOR, + padx=24, + font=self.SMALL_FONT_STYLE) total_label.pack(expand=True, fill='both') - label = tk.Label(self.display_frame, text=self.current_expression, anchor=tk.E, bg=LIGHT_GRAY, - fg=LABEL_COLOR, padx=24, font=LARGE_FONT_STYLE) + label = tk.Label( + self.display_frame, + text=self.current_expression, + anchor=tk.E, + bg=self.LIGHT_GRAY, + fg=self.LABEL_COLOR, + padx=24, + font=self.LARGE_FONT_STYLE) label.pack(expand=True, fill='both') return total_label, label def create_display_frame(self): - frame = tk.Frame(self.window, height=221, bg=LIGHT_GRAY) + frame = tk.Frame(self.window, height=221, bg=self.LIGHT_GRAY) frame.pack(expand=True, fill="both") return frame @@ -79,9 +104,18 @@ def add_to_expression(self, value): def create_digit_buttons(self): for digit, grid_value in self.digits.items(): - button = tk.Button(self.buttons_frame, text=str(digit), bg=WHITE, fg=LABEL_COLOR, font=DIGITS_FONT_STYLE, - borderwidth=0, command=lambda x=digit: self.add_to_expression(x)) - button.grid(row=grid_value[0], column=grid_value[1], sticky=tk.NSEW) + button = tk.Button( + self.buttons_frame, + text=str(digit), + bg=self.WHITE, + fg=self.LABEL_COLOR, + font=self.DIGITS_FONT_STYLE, + borderwidth=0, + command=lambda x=digit: self.add_to_expression(x)) + button.grid( + row=grid_value[0], + column=grid_value[1], + sticky=tk.NSEW) def append_operator(self, operator): self.current_expression += operator @@ -93,8 +127,14 @@ def append_operator(self, operator): def create_operator_buttons(self): i = 0 for operator, symbol in self.operations.items(): - button = tk.Button(self.buttons_frame, text=symbol, bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE, - borderwidth=0, command=lambda x=operator: self.append_operator(x)) + button = tk.Button( + self.buttons_frame, + text=symbol, + bg=self.OFF_WHITE, + fg=self.LABEL_COLOR, + font=self.DEFAULT_FONT_STYLE, + borderwidth=0, + command=lambda x=operator: self.append_operator(x)) button.grid(row=i, column=4, sticky=tk.NSEW) i += 1 @@ -105,8 +145,14 @@ def clear(self): self.update_total_label() def create_clear_button(self): - button = tk.Button(self.buttons_frame, text="C", bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE, - borderwidth=0, command=self.clear) + button = tk.Button( + self.buttons_frame, + text="C", + bg=self.OFF_WHITE, + fg=self.LABEL_COLOR, + font=self.DEFAULT_FONT_STYLE, + borderwidth=0, + command=self.clear) button.grid(row=0, column=1, sticky=tk.NSEW) def square(self): @@ -114,8 +160,14 @@ def square(self): self.update_label() def create_square_button(self): - button = tk.Button(self.buttons_frame, text="x\u00b2", bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE, - borderwidth=0, command=self.square) + button = tk.Button( + self.buttons_frame, + text="x\u00b2", + bg=self.OFF_WHITE, + fg=self.LABEL_COLOR, + font=self.DEFAULT_FONT_STYLE, + borderwidth=0, + command=self.square) button.grid(row=0, column=2, sticky=tk.NSEW) def sqrt(self): @@ -123,8 +175,14 @@ def sqrt(self): self.update_label() def create_sqrt_button(self): - button = tk.Button(self.buttons_frame, text="\u221ax", bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE, - borderwidth=0, command=self.sqrt) + button = tk.Button( + self.buttons_frame, + text="\u221ax", + bg=self.OFF_WHITE, + fg=self.LABEL_COLOR, + font=self.DEFAULT_FONT_STYLE, + borderwidth=0, + command=self.sqrt) button.grid(row=0, column=3, sticky=tk.NSEW) def evaluate(self): @@ -140,8 +198,14 @@ def evaluate(self): self.update_label() def create_equals_button(self): - button = tk.Button(self.buttons_frame, text="=", bg=LIGHT_BLUE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE, - borderwidth=0, command=self.evaluate) + button = tk.Button( + self.buttons_frame, + text="=", + bg=self.LIGHT_BLUE, + fg=self.LABEL_COLOR, + font=self.DEFAULT_FONT_STYLE, + borderwidth=0, + command=self.evaluate) button.grid(row=4, column=3, columnspan=2, sticky=tk.NSEW) def create_buttons_frame(self): From 0f482d22141e709942790aa7469d5355e2cd6191 Mon Sep 17 00:00:00 2001 From: Mosawer Mahboob <142144474+masomahboob@users.noreply.github.com> Date: Wed, 27 Dec 2023 21:39:17 +0000 Subject: [PATCH 2/2] removed inheritance. refactored the code to use dataclasses for static values --- calc.py | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/calc.py b/calc.py index dcb6187..236f70b 100644 --- a/calc.py +++ b/calc.py @@ -19,7 +19,7 @@ class Colors: LABEL_COLOR = "#25265E" -class Calculator(Colors, Fonts): +class Calculator: def __init__(self): self.window = tk.Tk() self.window.geometry("375x667") @@ -75,26 +75,26 @@ def create_display_labels(self): self.display_frame, text=self.total_expression, anchor=tk.E, - bg=self.LIGHT_GRAY, - fg=self.LABEL_COLOR, + bg=Colors.LIGHT_GRAY, + fg=Colors.LABEL_COLOR, padx=24, - font=self.SMALL_FONT_STYLE) + font=Fonts.SMALL_FONT_STYLE) total_label.pack(expand=True, fill='both') label = tk.Label( self.display_frame, text=self.current_expression, anchor=tk.E, - bg=self.LIGHT_GRAY, - fg=self.LABEL_COLOR, + bg=Colors.LIGHT_GRAY, + fg=Colors.LABEL_COLOR, padx=24, - font=self.LARGE_FONT_STYLE) + font=Fonts.LARGE_FONT_STYLE) label.pack(expand=True, fill='both') return total_label, label def create_display_frame(self): - frame = tk.Frame(self.window, height=221, bg=self.LIGHT_GRAY) + frame = tk.Frame(self.window, height=221, bg=Colors.LIGHT_GRAY) frame.pack(expand=True, fill="both") return frame @@ -107,9 +107,9 @@ def create_digit_buttons(self): button = tk.Button( self.buttons_frame, text=str(digit), - bg=self.WHITE, - fg=self.LABEL_COLOR, - font=self.DIGITS_FONT_STYLE, + bg=Colors.WHITE, + fg=Colors.LABEL_COLOR, + font=Fonts.DIGITS_FONT_STYLE, borderwidth=0, command=lambda x=digit: self.add_to_expression(x)) button.grid( @@ -130,9 +130,9 @@ def create_operator_buttons(self): button = tk.Button( self.buttons_frame, text=symbol, - bg=self.OFF_WHITE, - fg=self.LABEL_COLOR, - font=self.DEFAULT_FONT_STYLE, + bg=Colors.OFF_WHITE, + fg=Colors.LABEL_COLOR, + font=Fonts.DEFAULT_FONT_STYLE, borderwidth=0, command=lambda x=operator: self.append_operator(x)) button.grid(row=i, column=4, sticky=tk.NSEW) @@ -148,9 +148,9 @@ def create_clear_button(self): button = tk.Button( self.buttons_frame, text="C", - bg=self.OFF_WHITE, - fg=self.LABEL_COLOR, - font=self.DEFAULT_FONT_STYLE, + bg=Colors.OFF_WHITE, + fg=Colors.LABEL_COLOR, + font=Fonts.DEFAULT_FONT_STYLE, borderwidth=0, command=self.clear) button.grid(row=0, column=1, sticky=tk.NSEW) @@ -163,9 +163,9 @@ def create_square_button(self): button = tk.Button( self.buttons_frame, text="x\u00b2", - bg=self.OFF_WHITE, - fg=self.LABEL_COLOR, - font=self.DEFAULT_FONT_STYLE, + bg=Colors.OFF_WHITE, + fg=Colors.LABEL_COLOR, + font=Fonts.DEFAULT_FONT_STYLE, borderwidth=0, command=self.square) button.grid(row=0, column=2, sticky=tk.NSEW) @@ -178,9 +178,9 @@ def create_sqrt_button(self): button = tk.Button( self.buttons_frame, text="\u221ax", - bg=self.OFF_WHITE, - fg=self.LABEL_COLOR, - font=self.DEFAULT_FONT_STYLE, + bg=Colors.OFF_WHITE, + fg=Colors.LABEL_COLOR, + font=Fonts.DEFAULT_FONT_STYLE, borderwidth=0, command=self.sqrt) button.grid(row=0, column=3, sticky=tk.NSEW) @@ -201,9 +201,9 @@ def create_equals_button(self): button = tk.Button( self.buttons_frame, text="=", - bg=self.LIGHT_BLUE, - fg=self.LABEL_COLOR, - font=self.DEFAULT_FONT_STYLE, + bg=Colors.LIGHT_BLUE, + fg=Colors.LABEL_COLOR, + font=Fonts.DEFAULT_FONT_STYLE, borderwidth=0, command=self.evaluate) button.grid(row=4, column=3, columnspan=2, sticky=tk.NSEW)