|
| 1 | +from dataclasses import field |
| 2 | + |
1 | 3 | import flet as ft |
2 | 4 |
|
3 | 5 |
|
| 6 | +@ft.control |
4 | 7 | class CalcButton(ft.Button): |
5 | | - def __init__(self, text, button_clicked, expand=1): |
6 | | - super().__init__() |
7 | | - self.text = text |
8 | | - self.expand = expand |
9 | | - self.on_click = button_clicked |
10 | | - self.data = text |
| 8 | + expand: int = field(default_factory=lambda: 1) |
11 | 9 |
|
12 | 10 |
|
| 11 | +@ft.control |
13 | 12 | class DigitButton(CalcButton): |
14 | | - def __init__(self, text, button_clicked, expand=1): |
15 | | - CalcButton.__init__(self, text, button_clicked, expand) |
16 | | - self.bgcolor = ft.Colors.WHITE24 |
17 | | - self.color = ft.Colors.WHITE |
| 13 | + bgcolor: ft.Colors = ft.Colors.WHITE_24 |
| 14 | + color: ft.Colors = ft.Colors.WHITE |
18 | 15 |
|
19 | 16 |
|
| 17 | +@ft.control |
20 | 18 | class ActionButton(CalcButton): |
21 | | - def __init__(self, text, button_clicked): |
22 | | - CalcButton.__init__(self, text, button_clicked) |
23 | | - self.bgcolor = ft.Colors.ORANGE |
24 | | - self.color = ft.Colors.WHITE |
| 19 | + bgcolor: ft.Colors = ft.Colors.ORANGE |
| 20 | + color: ft.Colors = ft.Colors.WHITE |
25 | 21 |
|
26 | 22 |
|
| 23 | +@ft.control |
27 | 24 | class ExtraActionButton(CalcButton): |
28 | | - def __init__(self, text, button_clicked): |
29 | | - CalcButton.__init__(self, text, button_clicked) |
30 | | - self.bgcolor = ft.Colors.BLUE_GREY_100 |
31 | | - self.color = ft.Colors.BLACK |
| 25 | + bgcolor: ft.Colors = ft.Colors.BLUE_GREY_100 |
| 26 | + color: ft.Colors = ft.Colors.BLACK |
32 | 27 |
|
33 | 28 |
|
| 29 | +@ft.control |
34 | 30 | class CalculatorApp(ft.Container): |
35 | | - # application's root control (i.e. "view") containing all other controls |
36 | | - def __init__(self): |
37 | | - super().__init__() |
| 31 | + def init(self): |
38 | 32 | self.reset() |
39 | | - |
40 | | - self.result = ft.Text(value="0", color=ft.Colors.WHITE, size=20) |
41 | 33 | self.width = 350 |
42 | 34 | self.bgcolor = ft.Colors.BLACK |
43 | | - self.border_radius = ft.border_radius.all(20) |
| 35 | + self.border_radius = ft.BorderRadius.all(20) |
44 | 36 | self.padding = 20 |
| 37 | + self.result = ft.Text(value="0", color=ft.Colors.WHITE, size=20) |
| 38 | + |
45 | 39 | self.content = ft.Column( |
46 | 40 | controls=[ |
47 | | - ft.Row(controls=[self.result], alignment="end"), |
| 41 | + ft.Row( |
| 42 | + controls=[self.result], |
| 43 | + alignment=ft.MainAxisAlignment.END, |
| 44 | + ), |
48 | 45 | ft.Row( |
49 | 46 | controls=[ |
50 | | - ExtraActionButton( |
51 | | - text="AC", button_clicked=self.button_clicked |
52 | | - ), |
53 | | - ExtraActionButton( |
54 | | - text="+/-", button_clicked=self.button_clicked |
55 | | - ), |
56 | | - ExtraActionButton(text="%", button_clicked=self.button_clicked), |
57 | | - ActionButton(text="/", button_clicked=self.button_clicked), |
| 47 | + ExtraActionButton(content="AC", on_click=self.button_clicked), |
| 48 | + ExtraActionButton(content="+/-", on_click=self.button_clicked), |
| 49 | + ExtraActionButton(content="%", on_click=self.button_clicked), |
| 50 | + ActionButton(content="/", on_click=self.button_clicked), |
58 | 51 | ] |
59 | 52 | ), |
60 | 53 | ft.Row( |
61 | 54 | controls=[ |
62 | | - DigitButton(text="7", button_clicked=self.button_clicked), |
63 | | - DigitButton(text="8", button_clicked=self.button_clicked), |
64 | | - DigitButton(text="9", button_clicked=self.button_clicked), |
65 | | - ActionButton(text="*", button_clicked=self.button_clicked), |
| 55 | + DigitButton(content="7", on_click=self.button_clicked), |
| 56 | + DigitButton(content="8", on_click=self.button_clicked), |
| 57 | + DigitButton(content="9", on_click=self.button_clicked), |
| 58 | + ActionButton(content="*", on_click=self.button_clicked), |
66 | 59 | ] |
67 | 60 | ), |
68 | 61 | ft.Row( |
69 | 62 | controls=[ |
70 | | - DigitButton(text="4", button_clicked=self.button_clicked), |
71 | | - DigitButton(text="5", button_clicked=self.button_clicked), |
72 | | - DigitButton(text="6", button_clicked=self.button_clicked), |
73 | | - ActionButton(text="-", button_clicked=self.button_clicked), |
| 63 | + DigitButton(content="4", on_click=self.button_clicked), |
| 64 | + DigitButton(content="5", on_click=self.button_clicked), |
| 65 | + DigitButton(content="6", on_click=self.button_clicked), |
| 66 | + ActionButton(content="-", on_click=self.button_clicked), |
74 | 67 | ] |
75 | 68 | ), |
76 | 69 | ft.Row( |
77 | 70 | controls=[ |
78 | | - DigitButton(text="1", button_clicked=self.button_clicked), |
79 | | - DigitButton(text="2", button_clicked=self.button_clicked), |
80 | | - DigitButton(text="3", button_clicked=self.button_clicked), |
81 | | - ActionButton(text="+", button_clicked=self.button_clicked), |
| 71 | + DigitButton(content="1", on_click=self.button_clicked), |
| 72 | + DigitButton(content="2", on_click=self.button_clicked), |
| 73 | + DigitButton(content="3", on_click=self.button_clicked), |
| 74 | + ActionButton(content="+", on_click=self.button_clicked), |
82 | 75 | ] |
83 | 76 | ), |
84 | 77 | ft.Row( |
85 | 78 | controls=[ |
86 | 79 | DigitButton( |
87 | | - text="0", expand=2, button_clicked=self.button_clicked |
| 80 | + content="0", expand=2, on_click=self.button_clicked |
88 | 81 | ), |
89 | | - DigitButton(text=".", button_clicked=self.button_clicked), |
90 | | - ActionButton(text="=", button_clicked=self.button_clicked), |
| 82 | + DigitButton(content=".", on_click=self.button_clicked), |
| 83 | + ActionButton(content="=", on_click=self.button_clicked), |
91 | 84 | ] |
92 | 85 | ), |
93 | 86 | ] |
94 | 87 | ) |
95 | 88 |
|
96 | 89 | def button_clicked(self, e): |
97 | | - data = e.control.data |
| 90 | + data = e.control.content |
98 | 91 | print(f"Button clicked with data = {data}") |
99 | 92 | if self.result.value == "Error" or data == "AC": |
100 | 93 | self.result.value = "0" |
|
0 commit comments