Skip to content

Commit a5806ad

Browse files
committed
Wide layout
Added a wide layout for the calculator that is enabled when the calculator is wider than it is tall.
1 parent 76c0d6d commit a5806ad

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

calculator/gui/button.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,41 @@ class ButtonLayout:
1111
buttons: list[str]
1212
size_overrides: dict = field(default_factory=dict)
1313

14-
LAYOUT_STANDARD = ButtonLayout(4,
14+
LAYOUT_STANDARD_TALL = ButtonLayout(4,
1515
["AC", "CE", "%", "/",
1616
"7", "8", "9", "x",
1717
"4", "5", "6", "-",
1818
"1", "2", "3", "+",
1919
"+/-", "0", ".", "="])
2020

21-
LAYOUT_NO_NEGATE = ButtonLayout(4,
22-
["AC", "CE", "%", "/",
23-
"7", "8", "9", "x",
24-
"4", "5", "6", "-",
25-
"1", "2", "3", "+",
26-
"0", ".", "="],
27-
{(2, 4): (2,1)})
28-
29-
LAYOUT_NO_NUMBERS = ButtonLayout(5,
30-
["AC", "CE", "%", ".", "=",
31-
"/", "x", "-", "+",],
32-
{(4,0): (1,2)})
21+
LAYOUT_STANDARD_WIDE = ButtonLayout(5,
22+
["AC", "7", "8", "9", "/",
23+
"CE", "4", "5", "6", "x",
24+
"%", "1", "2", "3", "-",
25+
"+/-", "0", ".", "=", "+"])
3326

3427
def calc_button(root, calculator: api.Calculator, display_text):
3528
button = tk.Button(root, text=display_text, command=lambda: calculator.input_button(display_text))
3629
return button
3730

3831
class CalculatorButtons(AbstractBaseWidget):
32+
def __init__(self, calculator):
33+
super().__init__(calculator)
34+
self.prev_column_count = 0
35+
3936
def _create_layout(self, window_resolution):
4037
aspect_ratio = window_resolution[0] / window_resolution[1]
41-
self.create_buttons(LAYOUT_STANDARD)
38+
if aspect_ratio > 1:
39+
self.create_buttons(LAYOUT_STANDARD_WIDE)
40+
self.prev_column_count = LAYOUT_STANDARD_WIDE.width
41+
else:
42+
self.create_buttons(LAYOUT_STANDARD_TALL)
43+
self.prev_column_count = LAYOUT_STANDARD_TALL.width
44+
45+
def _destroy_layout(self):
46+
super()._destroy_layout()
47+
for i in range(self.prev_column_count):
48+
self.frame.columnconfigure(i, weight=0)
4249

4350
def _update(self):
4451
pass
@@ -58,7 +65,7 @@ def create_buttons(self, layout: ButtonLayout):
5865
button.grid(column=x, row=y, sticky=tk.NSEW, columnspan=size_x, rowspan=size_y)
5966

6067
for i in range(layout.width):
61-
self.frame.grid_columnconfigure(i, uniform="calc_buttons", weight=1)
68+
self.frame.grid_columnconfigure(i, weight=1)
6269

6370
for i in range(int(len(layout.buttons)/layout.width)):
6471
self.frame.grid_rowconfigure(i, weight=1)

0 commit comments

Comments
 (0)