Skip to content

Commit

Permalink
feature: add example code
Browse files Browse the repository at this point in the history
  • Loading branch information
zimolab committed Oct 16, 2024
1 parent 3322d68 commit 4f8de06
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
63 changes: 63 additions & 0 deletions examples/equation_solver_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from typing import Optional

from pyguiadapter.adapter import GUIAdapter
from pyguiadapter.adapter.uoutput import uprint
from pyguiadapter.exceptions import ParameterError
from pyguiadapter.widgets import FloatSpinBoxConfig
from pyguiadapter.windows.fnexec import FnExecuteWindowConfig


def equation_solver_4(
a: float = 1.0, b: float = 0.0, c: float = 0.0
) -> Optional[tuple]:
"""A simple equation solver for equations like:
**ax^2 + bx + c = 0** (a, b, c ∈ **R** and a ≠ 0)
@param a: a ∈ R and a ≠ 0
@param b: b ∈ R
@param c: c ∈ R
@return:
"""
if a == 0:
raise ParameterError(parameter_name="a", message="a cannot be zero!")
uprint(f"Equation:")
uprint(f" {a}x² + {b}x + {c} = 0")
delta = b**2 - 4 * a * c
if delta < 0:
return None
x1 = (-b + delta**0.5) / (2 * a)
if delta == 0:
return x1, x1
x2 = (-b - delta**0.5) / (2 * a)
return x1, x2


if __name__ == "__main__":
window_config = FnExecuteWindowConfig(
title="Equation Solver",
icon="mdi6.function-variant",
execute_button_text="Solve",
size=(350, 550),
document_dock_visible=False,
output_dock_initial_size=(None, 100),
show_function_result=True,
function_result_message="roots: {}",
default_parameter_group_name="Equation Parameters",
)

adapter = GUIAdapter()
adapter.add(
equation_solver_4,
window_config=window_config,
widget_configs={
"a": FloatSpinBoxConfig(
default_value=1.0,
decimals=5,
step=0.00005,
),
"b": FloatSpinBoxConfig(decimals=5, step=0.00005),
"c": FloatSpinBoxConfig(decimals=5, step=0.00005),
},
)
adapter.run()
82 changes: 82 additions & 0 deletions examples/equation_solver_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from typing import Optional

from pyguiadapter.action import Action
from pyguiadapter.adapter import GUIAdapter
from pyguiadapter.adapter.uoutput import uprint
from pyguiadapter.exceptions import ParameterError
from pyguiadapter.menu import Menu
from pyguiadapter.utils import messagebox
from pyguiadapter.widgets import FloatSpinBoxConfig
from pyguiadapter.windows.fnexec import FnExecuteWindowConfig, FnExecuteWindow


def equation_solver_5(
a: float = 1.0, b: float = 0.0, c: float = 0.0
) -> Optional[tuple]:
"""A simple equation solver for equations like:
**ax^2 + bx + c = 0** (a, b, c ∈ **R** and a ≠ 0)
@param a: a ∈ R and a ≠ 0
@param b: b ∈ R
@param c: c ∈ R
@return:
"""
if a == 0:
raise ParameterError(parameter_name="a", message="a cannot be zero!")
uprint(f"Equation:")
uprint(f" {a}x² + {b}x + {c} = 0")
delta = b**2 - 4 * a * c
if delta < 0:
return None
x1 = (-b + delta**0.5) / (2 * a)
if delta == 0:
return x1, x1
x2 = (-b - delta**0.5) / (2 * a)
return x1, x2


if __name__ == "__main__":
window_config = FnExecuteWindowConfig(
title="Equation Solver",
icon="mdi6.function-variant",
execute_button_text="Solve",
size=(350, 450),
document_dock_visible=False,
show_function_result=True,
function_result_message="roots: {}",
default_parameter_group_name="Equation Parameters",
# 隐藏`OutputDock`窗口
output_dock_visible=False,
# 因为隐藏了`OutputDock`窗口,所以无需将函数调用结果及函数异常信息打印到输出浏览器中
print_function_error=False,
print_function_result=False,
)

def on_action_about(wind: FnExecuteWindow, action: Action):
messagebox.show_text_file(
wind,
text_file="./about.html",
text_format="html",
title="About",
)

action_about = Action(text="About", on_triggered=on_action_about)
menu_help = Menu(title="Help", actions=[action_about])

adapter = GUIAdapter()
adapter.add(
equation_solver_5,
window_menus=[menu_help],
window_config=window_config,
widget_configs={
"a": FloatSpinBoxConfig(
default_value=1.0,
decimals=5,
step=0.00005,
),
"b": FloatSpinBoxConfig(decimals=5, step=0.00005),
"c": FloatSpinBoxConfig(decimals=5, step=0.00005),
},
)
adapter.run()

0 comments on commit 4f8de06

Please sign in to comment.