技術関連メディア

Fletで作った100桁計算用電卓アプリのPythonコード

Fletで作った100桁計算用電卓アプリのPythonコードです。

コードまとめ

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)

        return Container(
            width=600,
            bgcolor=colors.BLUE_100,
            border_radius=border_radius.all(20),
            padding=20,
            content=Column(
                controls=[
                    Row(controls=[self.result], alignment="end"),
                    Row(
                        controls=[
                            ElevatedButton(
                                text="AC",
                                bgcolor=colors.PINK_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="AC",
                            ),
                            ElevatedButton(
                                text="+/-",
                                bgcolor=colors.PINK_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="+/-",
                            ),
                            ElevatedButton(
                                text="%",
                                bgcolor=colors.PINK_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="%",
                            ),
                            ElevatedButton(
                                text="/",
                                bgcolor=colors.PINK_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="/",
                            ),
                        ],
                    ),
                    Row(
                        controls=[
                            ElevatedButton(
                                text="700",
                                bgcolor=colors.GREEN_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="700",
                            ),
                            ElevatedButton(
                                text="800",
                                bgcolor=colors.GREEN_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="800",
                            ),
                            ElevatedButton(
                                text="900",
                                bgcolor=colors.GREEN_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="900",
                            ),
                            ElevatedButton(
                                text="*",
                                bgcolor=colors.PINK_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="*",
                            ),
                        ]
                    ),
                    Row(
                        controls=[
                            ElevatedButton(
                                text="400",
                                bgcolor=colors.GREEN_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="400",
                            ),
                            ElevatedButton(
                                text="500",
                                bgcolor=colors.GREEN_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="500",
                            ),
                            ElevatedButton(
                                text="600",
                                bgcolor=colors.GREEN_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="600",
                            ),
                            ElevatedButton(
                                text="-",
                                bgcolor=colors.PINK_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="-",
                            ),
                        ]
                    ),
                    Row(
                        controls=[
                            ElevatedButton(
                                text="100",
                                bgcolor=colors.GREEN_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="100",
                            ),
                            ElevatedButton(
                                text="200",
                                bgcolor=colors.GREEN_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="200",
                            ),
                            ElevatedButton(
                                text="300",
                                bgcolor=colors.GREEN_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="300",
                            ),
                            ElevatedButton(
                                text="+",
                                bgcolor=colors.PINK_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="+",
                            ),
                        ]
                    ),
                    Row(
                        controls=[
                            ElevatedButton(
                                text="0",
                                bgcolor=colors.GREEN_100,
                                color=colors.WHITE,
                                expand=2,
                                on_click=self.button_clicked,
                                data="0",
                            ),
                            ElevatedButton(
                                text=".",
                                bgcolor=colors.GREEN_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data=".",
                            ),
                            ElevatedButton(
                                text="=",
                                bgcolor=colors.PINK_100,
                                color=colors.WHITE,
                                expand=1,
                                on_click=self.button_clicked,
                                data="=",
                            ),
                        ]
                    ),
                ],
            ),
        )

    def button_clicked(self, e):
        data = e.control.data
        if self.result.value == "Error" or data == "AC":
            self.result.value = "0"
            self.reset()

        elif data in ("100", "200", "300", "400", "500", "600", "700", "800", "900", "0", "."):
            if self.result.value == "0" or self.new_operand == True:
                self.result.value = data
                self.new_operand = False
            else:
                self.result.value = self.result.value + data

        elif data in ("+", "-", "*", "/"):
            self.result.value = self.calculate(
                self.operand1, float(self.result.value), self.operator
            )
            self.operator = data
            if self.result.value == "Error":
                self.operand1 = "0"
            else:
                self.operand1 = float(self.result.value)
            self.new_operand = True

        elif data in ("="):
            self.result.value = self.calculate(
                self.operand1, float(self.result.value), self.operator
            )
            self.reset()

        elif data in ("%"):
            self.result.value = float(self.result.value) / 100
            self.reset()

        elif 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 = "Numb App"

    # create application instance
    calc = CalculatorApp()

    # add application's root control to the page
    page.add(calc)


flet.app(target=main)

参考資料

Create Calculator app in Python with Flet

Fletの環境構築方法

FletでPythonを用いてFlutterアプリを作成する方法

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA