Skip to content

Commit 0e20c25

Browse files
committed
Refactor calculator to use @ft.control and dataclasses
Refactored calc.py and calc5.py to use @ft.control decorator and dataclass-style field definitions for button and app classes, replacing manual __init__ methods. Updated button instantiation to use 'content' and 'on_click' instead of 'text' and 'button_clicked'. Removed the now-redundant calc5_dataclasses.py file.
1 parent beba210 commit 0e20c25

File tree

3 files changed

+84
-268
lines changed

3 files changed

+84
-268
lines changed

sdk/python/examples/tutorials/calculator/calc.py

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,93 @@
1+
from dataclasses import field
2+
13
import flet as ft
24

35

6+
@ft.control
47
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)
119

1210

11+
@ft.control
1312
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
1815

1916

17+
@ft.control
2018
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
2521

2622

23+
@ft.control
2724
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
3227

3328

29+
@ft.control
3430
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):
3832
self.reset()
39-
40-
self.result = ft.Text(value="0", color=ft.Colors.WHITE, size=20)
4133
self.width = 350
4234
self.bgcolor = ft.Colors.BLACK
43-
self.border_radius = ft.border_radius.all(20)
35+
self.border_radius = ft.BorderRadius.all(20)
4436
self.padding = 20
37+
self.result = ft.Text(value="0", color=ft.Colors.WHITE, size=20)
38+
4539
self.content = ft.Column(
4640
controls=[
47-
ft.Row(controls=[self.result], alignment="end"),
41+
ft.Row(
42+
controls=[self.result],
43+
alignment=ft.MainAxisAlignment.END,
44+
),
4845
ft.Row(
4946
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),
5851
]
5952
),
6053
ft.Row(
6154
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),
6659
]
6760
),
6861
ft.Row(
6962
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),
7467
]
7568
),
7669
ft.Row(
7770
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),
8275
]
8376
),
8477
ft.Row(
8578
controls=[
8679
DigitButton(
87-
text="0", expand=2, button_clicked=self.button_clicked
80+
content="0", expand=2, on_click=self.button_clicked
8881
),
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),
9184
]
9285
),
9386
]
9487
)
9588

9689
def button_clicked(self, e):
97-
data = e.control.data
90+
data = e.control.content
9891
print(f"Button clicked with data = {data}")
9992
if self.result.value == "Error" or data == "AC":
10093
self.result.value = "0"

sdk/python/examples/tutorials/calculator/calc5.py

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,93 @@
1+
from dataclasses import field
2+
13
import flet as ft
24

35

6+
@ft.control
47
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)
119

1210

11+
@ft.control
1312
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
1815

1916

17+
@ft.control
2018
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
2521

2622

23+
@ft.control
2724
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
3227

3328

29+
@ft.control
3430
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):
3832
self.reset()
39-
40-
self.result = ft.Text(value="0", color=ft.Colors.WHITE, size=20)
4133
self.width = 350
4234
self.bgcolor = ft.Colors.BLACK
43-
self.border_radius = ft.border_radius.all(20)
35+
self.border_radius = ft.BorderRadius.all(20)
4436
self.padding = 20
37+
self.result = ft.Text(value="0", color=ft.Colors.WHITE, size=20)
38+
4539
self.content = ft.Column(
4640
controls=[
47-
ft.Row(controls=[self.result], alignment="end"),
41+
ft.Row(
42+
controls=[self.result],
43+
alignment=ft.MainAxisAlignment.END,
44+
),
4845
ft.Row(
4946
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),
5851
]
5952
),
6053
ft.Row(
6154
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),
6659
]
6760
),
6861
ft.Row(
6962
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),
7467
]
7568
),
7669
ft.Row(
7770
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),
8275
]
8376
),
8477
ft.Row(
8578
controls=[
8679
DigitButton(
87-
text="0", expand=2, button_clicked=self.button_clicked
80+
content="0", expand=2, on_click=self.button_clicked
8881
),
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),
9184
]
9285
),
9386
]
9487
)
9588

9689
def button_clicked(self, e):
97-
data = e.control.data
90+
data = e.control.content
9891
print(f"Button clicked with data = {data}")
9992
if self.result.value == "Error" or data == "AC":
10093
self.result.value = "0"

0 commit comments

Comments
 (0)