diff --git a/python/tutorials/calc/calc.py b/python/tutorials/calc/calc.py new file mode 100644 index 00000000..5517c1fe --- /dev/null +++ b/python/tutorials/calc/calc.py @@ -0,0 +1,288 @@ +import flet +from flet import ( + Column, + Container, + ElevatedButton, + Page, + Row, + Text, + UserControl, + border_radius, + colors, +) + + +class CalculatorApp(UserControl): + def build(self): + self.reset() + self.result = Text(value="0", color=colors.WHITE, size=20) + + # application's root control (i.e. "view") containing all other controls + return Container( + width=300, + bgcolor=colors.BLACK, + border_radius=border_radius.all(20), + padding=20, + content=Column( + controls=[ + Row(controls=[self.result], alignment="end"), + Row( + controls=[ + ElevatedButton( + text="AC", + bgcolor=colors.BLUE_GREY_100, + color=colors.BLACK, + expand=1, + on_click=self.button_clicked, + data="AC", + ), + ElevatedButton( + text="+/-", + bgcolor=colors.BLUE_GREY_100, + color=colors.BLACK, + expand=1, + on_click=self.button_clicked, + data="+/-", + ), + ElevatedButton( + text="%", + bgcolor=colors.BLUE_GREY_100, + color=colors.BLACK, + expand=1, + on_click=self.button_clicked, + data="%", + ), + ElevatedButton( + text="/", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="/", + ), + ], + ), + Row( + controls=[ + ElevatedButton( + text="7", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="7", + ), + ElevatedButton( + text="8", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="8", + ), + ElevatedButton( + text="9", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="9", + ), + ElevatedButton( + text="*", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="*", + ), + ] + ), + Row( + controls=[ + ElevatedButton( + text="4", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="4", + ), + ElevatedButton( + text="5", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="5", + ), + ElevatedButton( + text="6", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="6", + ), + ElevatedButton( + text="-", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="-", + ), + ] + ), + Row( + controls=[ + ElevatedButton( + text="1", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="1", + ), + ElevatedButton( + text="2", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="2", + ), + ElevatedButton( + text="3", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="3", + ), + ElevatedButton( + text="+", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="+", + ), + ] + ), + Row( + controls=[ + ElevatedButton( + text="0", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=2, + on_click=self.button_clicked, + data="0", + ), + ElevatedButton( + text=".", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data=".", + ), + ElevatedButton( + text="=", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + on_click=self.button_clicked, + data="=", + ), + ] + ), + ], + ), + ) + + def button_clicked(self, e): + if self.result.value == "Error" or e.data == "AC": + self.result.value = "0" + self.reset() + + elif e.data in ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."): + if self.result.value == "0" or self.new_operand == True: + self.result.value = e.data + self.new_operand = False + else: + self.result.value = self.result.value + e.data + + elif e.data in ("+", "-", "*", "/"): + self.result.value = self.calculate( + self.operand1, float(self.result.value), self.operator + ) + self.operator = e.data + if self.result.value == "Error": + self.operand1 = "0" + else: + self.operand1 = float(self.result.value) + self.new_operand = True + + elif e.data in ("="): + self.result.value = self.calculate( + self.operand1, float(self.result.value), self.operator + ) + self.reset() + + elif e.data in ("%"): + self.result.value = float(self.result.value) / 100 + self.reset() + + elif e.data in ("+/-"): + if float(self.result.value) > 0: + self.result.value = "-" + str(self.result.value) + + elif float(self.result.value) < 0: + self.result.value = str( + self.format_number(abs(float(self.result.value))) + ) + + self.update() + + def format_number(self, num): + if num % 1 == 0: + return int(num) + else: + return num + + def calculate(self, operand1, operand2, operator): + + if operator == "+": + return self.format_number(operand1 + operand2) + + elif operator == "-": + return self.format_number(operand1 - operand2) + + elif operator == "*": + return self.format_number(operand1 * operand2) + + elif operator == "/": + if operand2 == 0: + return "Error" + else: + return self.format_number(operand1 / operand2) + + def reset(self): + self.operator = "+" + self.operand1 = 0 + self.new_operand = True + + +def main(page: Page): + page.title = "Calc App" + + # create application instance + calc = CalculatorApp() + + # add application's root control to the page + page.add(calc) + + +flet.app(target=main) diff --git a/python/tutorials/calc/calc1.py b/python/tutorials/calc/calc1.py new file mode 100644 index 00000000..81059692 --- /dev/null +++ b/python/tutorials/calc/calc1.py @@ -0,0 +1,33 @@ +import flet +from flet import ElevatedButton, Page, Text + + +def main(page: Page): + page.title = "Calc App" + result = Text(value="0") + + page.add( + result, + ElevatedButton(text="AC"), + ElevatedButton(text="+/-"), + ElevatedButton(text="%"), + ElevatedButton(text="/"), + ElevatedButton(text="7"), + ElevatedButton(text="8"), + ElevatedButton(text="9"), + ElevatedButton(text="*"), + ElevatedButton(text="4"), + ElevatedButton(text="5"), + ElevatedButton(text="6"), + ElevatedButton(text="-"), + ElevatedButton(text="1"), + ElevatedButton(text="2"), + ElevatedButton(text="3"), + ElevatedButton(text="+"), + ElevatedButton(text="0"), + ElevatedButton(text="."), + ElevatedButton(text="="), + ) + + +flet.app(target=main) diff --git a/python/tutorials/calc/calc2.py b/python/tutorials/calc/calc2.py new file mode 100644 index 00000000..e20cf61b --- /dev/null +++ b/python/tutorials/calc/calc2.py @@ -0,0 +1,53 @@ +import flet +from flet import ElevatedButton, Page, Row, Text + + +def main(page: Page): + page.title = "Calc App" + result = Text(value="0") + + page.add( + Row(controls=[result]), + Row( + controls=[ + ElevatedButton(text="AC"), + ElevatedButton(text="+/-"), + ElevatedButton(text="%"), + ElevatedButton(text="/"), + ] + ), + Row( + controls=[ + ElevatedButton(text="7"), + ElevatedButton(text="8"), + ElevatedButton(text="9"), + ElevatedButton(text="*"), + ] + ), + Row( + controls=[ + ElevatedButton(text="4"), + ElevatedButton(text="5"), + ElevatedButton(text="6"), + ElevatedButton(text="-"), + ] + ), + Row( + controls=[ + ElevatedButton(text="1"), + ElevatedButton(text="2"), + ElevatedButton(text="3"), + ElevatedButton(text="+"), + ] + ), + Row( + controls=[ + ElevatedButton(text="0"), + ElevatedButton(text="."), + ElevatedButton(text="="), + ] + ), + ) + + +flet.app(target=main) diff --git a/python/tutorials/calc/calc3.py b/python/tutorials/calc/calc3.py new file mode 100644 index 00000000..56f8ef70 --- /dev/null +++ b/python/tutorials/calc/calc3.py @@ -0,0 +1,167 @@ +import flet +from flet import ( + Column, + Container, + ElevatedButton, + Page, + Row, + Text, + border_radius, + colors, +) + + +def main(page: Page): + page.title = "Calc App" + result = Text(value="0", color=colors.WHITE, size=20) + + page.add( + Container( + width=300, + bgcolor=colors.BLACK, + border_radius=border_radius.all(20), + padding=20, + content=Column( + controls=[ + Row(controls=[result], alignment="end"), + Row( + controls=[ + ElevatedButton( + text="AC", + bgcolor=colors.BLUE_GREY_100, + color=colors.BLACK, + expand=1, + ), + ElevatedButton( + text="+/-", + bgcolor=colors.BLUE_GREY_100, + color=colors.BLACK, + expand=1, + ), + ElevatedButton( + text="%", + bgcolor=colors.BLUE_GREY_100, + color=colors.BLACK, + expand=1, + ), + ElevatedButton( + text="/", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + ), + ] + ), + Row( + controls=[ + ElevatedButton( + text="7", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="8", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="9", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="*", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + ), + ] + ), + Row( + controls=[ + ElevatedButton( + text="4", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="5", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="6", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="-", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + ), + ] + ), + Row( + controls=[ + ElevatedButton( + text="1", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="2", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="3", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="+", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + ), + ] + ), + Row( + controls=[ + ElevatedButton( + text="0", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=2, + ), + ElevatedButton( + text=".", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="=", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + ), + ] + ), + ] + ), + ) + ) + + +flet.app(target=main) diff --git a/python/tutorials/calc/calc4.py b/python/tutorials/calc/calc4.py new file mode 100644 index 00000000..3b24fb73 --- /dev/null +++ b/python/tutorials/calc/calc4.py @@ -0,0 +1,176 @@ +import flet +from flet import ( + Column, + Container, + ElevatedButton, + Page, + Row, + Text, + UserControl, + border_radius, + colors, +) + + +class CalculatorApp(UserControl): + def build(self): + result = Text(value="0", color=colors.WHITE, size=20) + + # application's root control (i.e. "view") containing all other controls + return Container( + width=300, + bgcolor=colors.BLACK, + border_radius=border_radius.all(20), + padding=20, + content=Column( + controls=[ + Row(controls=[result], alignment="end"), + Row( + controls=[ + ElevatedButton( + text="AC", + bgcolor=colors.BLUE_GREY_100, + color=colors.BLACK, + expand=1, + ), + ElevatedButton( + text="+/-", + bgcolor=colors.BLUE_GREY_100, + color=colors.BLACK, + expand=1, + ), + ElevatedButton( + text="%", + bgcolor=colors.BLUE_GREY_100, + color=colors.BLACK, + expand=1, + ), + ElevatedButton( + text="/", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + ), + ] + ), + Row( + controls=[ + ElevatedButton( + text="7", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="8", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="9", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="*", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + ), + ] + ), + Row( + controls=[ + ElevatedButton( + text="4", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="5", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="6", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="-", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + ), + ] + ), + Row( + controls=[ + ElevatedButton( + text="1", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="2", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="3", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="+", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + ), + ] + ), + Row( + controls=[ + ElevatedButton( + text="0", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=2, + ), + ElevatedButton( + text=".", + bgcolor=colors.WHITE24, + color=colors.WHITE, + expand=1, + ), + ElevatedButton( + text="=", + bgcolor=colors.ORANGE, + color=colors.WHITE, + expand=1, + ), + ] + ), + ] + ), + ) + + +def main(page: Page): + page.title = "Calc App" + # create application instance + calc = CalculatorApp() + + # add application's root control to the page + page.add(calc) + + +flet.app(target=main)