Skip to content

Commit 76c0d6d

Browse files
committed
AbstractBaseWidget class
Creates a new AbstractBaseWidget class that is inherited by the button and display of the calculator. It shares common functionality including rebuilding the gui on resize.
1 parent b2f8fe1 commit 76c0d6d

File tree

4 files changed

+57
-23
lines changed

4 files changed

+57
-23
lines changed

calculator/gui/base_widget.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import api
2+
import tkinter as tk
3+
from abc import ABC, abstractmethod
4+
5+
class AbstractBaseWidget(ABC):
6+
def __init__(self, calculator: api.Calculator):
7+
self.parent = None
8+
self.frame = None
9+
self.calculator = calculator
10+
self.prev_window_resolution = (0,0)
11+
12+
def get_window_resolution(self):
13+
return (self.parent.winfo_width(), self.parent.winfo_height())
14+
15+
def init_gui(self, parent):
16+
self.parent = parent
17+
self.frame = tk.Frame(parent)
18+
self.prev_window_resolution = self.get_window_resolution()
19+
self._create_layout(self.prev_window_resolution)
20+
21+
@abstractmethod
22+
def _create_layout(self, window_resolution: tuple):
23+
pass
24+
25+
def _destroy_layout(self):
26+
for child in self.frame.winfo_children():
27+
child.destroy()
28+
29+
@abstractmethod
30+
def _update(self):
31+
pass
32+
33+
def update(self):
34+
window_resolution = self.get_window_resolution()
35+
if self.prev_window_resolution != window_resolution:
36+
self._destroy_layout()
37+
self._create_layout(window_resolution)
38+
self._update()
39+
self.prev_window_resolution = window_resolution

calculator/gui/button.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import api
22
import tkinter as tk
33
from dataclasses import dataclass, field
4+
from gui.base_widget import AbstractBaseWidget
45

56
NO_BUTTON = "NO_BUTTON"
67

@@ -34,15 +35,15 @@ def calc_button(root, calculator: api.Calculator, display_text):
3435
button = tk.Button(root, text=display_text, command=lambda: calculator.input_button(display_text))
3536
return button
3637

37-
class CalculatorButtons:
38-
def __init__(self, calculator: api.Calculator):
39-
self.calculator = calculator
40-
self.frame = None
41-
self.buttons = []
42-
43-
def create_gui(self, root, layout: ButtonLayout=LAYOUT_STANDARD):
44-
self.frame = tk.Frame(root)
38+
class CalculatorButtons(AbstractBaseWidget):
39+
def _create_layout(self, window_resolution):
40+
aspect_ratio = window_resolution[0] / window_resolution[1]
41+
self.create_buttons(LAYOUT_STANDARD)
42+
43+
def _update(self):
44+
pass
4545

46+
def create_buttons(self, layout: ButtonLayout):
4647
for i, label in enumerate(layout.buttons):
4748
if label == NO_BUTTON:
4849
continue
@@ -55,14 +56,9 @@ def create_gui(self, root, layout: ButtonLayout=LAYOUT_STANDARD):
5556

5657
button = calc_button(self.frame, self.calculator, label)
5758
button.grid(column=x, row=y, sticky=tk.NSEW, columnspan=size_x, rowspan=size_y)
58-
59-
self.buttons.append(button)
6059

6160
for i in range(layout.width):
6261
self.frame.grid_columnconfigure(i, uniform="calc_buttons", weight=1)
6362

6463
for i in range(int(len(layout.buttons)/layout.width)):
65-
self.frame.grid_rowconfigure(i, weight=1)
66-
67-
def update(self):
68-
pass
64+
self.frame.grid_rowconfigure(i, weight=1)

calculator/gui/display.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import api
22
import tkinter as tk
3+
from gui.base_widget import AbstractBaseWidget
34

4-
class CalculatorDisplay:
5+
class CalculatorDisplay(AbstractBaseWidget):
56
def __init__(self, calculator: api.Calculator):
6-
self.calculator = calculator
7-
self.frame = None
7+
super().__init__(calculator)
88
self.upper_entry = None
99
self.lower_entry = None
1010

11-
def create_gui(self, root):
12-
self.frame = tk.Frame(root)
11+
def _create_layout(self, window_resolution):
1312
self.upper_entry = tk.Entry(self.frame, state=tk.DISABLED, justify=tk.RIGHT)
1413
self.lower_entry = tk.Entry(self.frame, state=tk.DISABLED, justify=tk.RIGHT)
1514
self.upper_entry.pack(fill=tk.BOTH,expand=True,side=tk.TOP)
1615
self.lower_entry.pack(fill=tk.BOTH,expand=True,side=tk.BOTTOM)
17-
18-
def update(self):
16+
17+
def _update(self):
1918
self.upper_entry.configure(state=tk.NORMAL)
2019
self.lower_entry.configure(state=tk.NORMAL)
2120

calculator/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def create_gui(self):
1515
self.display = gui.CalculatorDisplay(self.calculator)
1616
self.buttons = gui.CalculatorButtons(self.calculator)
1717

18-
self.display.create_gui(self.window)
19-
self.buttons.create_gui(self.window)
18+
self.display.init_gui(self.window)
19+
self.buttons.init_gui(self.window)
2020

2121
self.display.frame.pack(fill=tk.BOTH,expand=True,side=tk.TOP,padx=10,pady=(10,0))
2222
self.buttons.frame.pack(fill=tk.BOTH,expand=True,side=tk.BOTTOM,padx=10,pady=(0,10))

0 commit comments

Comments
 (0)