import flet as ft
class CalcButton(ft.ElevatedButton):
def __init__(self, text, button_clicked, expand=1):
super().__init__()
self.text = text
self.expand = expand
self.on_click = button_clicked
self.data = text
class DigitButton(CalcButton):
def __init__(self, text, button_clicked, expand=1):
CalcButton.__init__(self, text, button_clicked, expand)
self.bgcolor = ft.colors.WHITE24
self.color = ft.colors.WHITE
class ActionButton(CalcButton):
def __init__(self, text, button_clicked):
CalcButton.__init__(self, text, button_clicked)
self.bgcolor = ft.colors.ORANGE
self.color = ft.colors.WHITE
class ExtraActionButton(CalcButton):
def __init__(self, text, button_clicked):
CalcButton.__init__(self, text, button_clicked)
self.bgcolor = ft.colors.BLUE_GREY_100
self.color = ft.colors.BLACK
class CalculatorApp(ft.Container):
# application's root control (i.e. "view") containing all other controls
def __init__(self):
super().__init__()
self.reset()
self.result = ft.Text(value="0", color=ft.colors.WHITE, size=20)
self.width = 350
self.bgcolor = ft.colors.BLACK
self.border_radius = ft.border_radius.all(20)
self.padding = 20
self.content = ft.Column(
controls=[
ft.Row(controls=[self.result], alignment="end"),
ft.Row(
controls=[
ExtraActionButton(
text="AC", button_clicked=self.button_clicked
),
ExtraActionButton(
text="+/-", button_clicked=self.button_clicked
),
ExtraActionButton(text="%", button_clicked=self.button_clicked),
ActionButton(text="/", button_clicked=self.button_clicked),
]
),
ft.Row(
controls=[
DigitButton(text="7", button_clicked=self.button_clicked),
DigitButton(text="8", button_clicked=self.button_clicked),
DigitButton(text="9", button_clicked=self.button_clicked),
ActionButton(text="*", button_clicked=self.button_clicked),
]
),
ft.Row(
controls=[
DigitButton(text="4", button_clicked=self.button_clicked),
DigitButton(text="5", button_clicked=self.button_clicked),
DigitButton(text="6", button_clicked=self.button_clicked),
ActionButton(text="-", button_clicked=self.button_clicked),
]
),
ft.Row(
controls=[
DigitButton(text="1", button_clicked=self.button_clicked),
DigitButton(text="2", button_clicked=self.button_clicked),
DigitButton(text="3", button_clicked=self.button_clicked),
ActionButton(text="+", button_clicked=self.button_clicked),
]
),
ft.Row(
controls=[
DigitButton(
text="0", expand=2, button_clicked=self.button_clicked
),
DigitButton(text=".", button_clicked=self.button_clicked),
ActionButton(text="=", button_clicked=self.button_clicked),
]
),
]
)
def button_clicked(self, e):
data = e.control.data
print(f"Button clicked with data = {data}")
if self.result.value == "Error" or data == "AC":
self.result.value = "0"
self.reset()
elif 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 = 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: ft.Page):
page.title = "Calc App"
# create application instance
calc = CalculatorApp()
# add application's root control to the page
page.add(calc)
ft.app(target=main)
elif operator == "ax^2+bx+c=0":
return selv.solv()
def solv():
a = float(input("Nhập a: "))
b = float(input("Nhập b: "))
c = float(input("Nhập c: "))
if a != 0:
delta = b**2 - 4*a*c
if delta > 0:
x1 = (-b + delta**0.5) / (2*a)
x2 = (-b - delta**0.5) / (2*a)
return f"x1 = {x1}, x2 = {x2}"
elif delta == 0:
x = -b / (2*a)
return f"x = {x}"
else:
return "Equation with no solution"
else:
return "Not a quadratic equation"
Подробнее здесь: https://stackoverflow.com/questions/790 ... or-project
Как добавить третий аргумент в проект учебного калькулятора Flet? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Интеграция двух приложений Flet с основным приложением Flet App Python
Anonymous » » в форуме Python - 0 Ответы
- 52 Просмотры
-
Последнее сообщение Anonymous
-