event

        classDiagram
  CharmyObject <|-- EventTask
  EventTask <|-- DelayTask
    
class charmy.event.EventHandling

基类:object

multithread_tasks: list[tuple[EventTask, Event]] = []
WORKING_THREAD: Thread
__init__()

A class containing event handling abilities.

Example

This is mostly for internal use of Charmy.

class Widget(EventHandling, ...):
    def __init__(self):
        super().__init__(self)
...

This shows subclassing EventHandling to let Widget gain the ability of handling events.

latest_event: Event
tasks: dict[type[Event], list[EventTask]]
parse_event_type_str(event_type_str: str) dict

This function parses event type string.

Type String Format

Either in {Event type} or {Event type}[{Param 1}, {Param 2}]. Example: MousePressed or MousePressed[0]

Return Value & Deprecation

The return value is a dict containing the type and params expressed in string. This kind of return value might be unused in the new events system designed for Charmy. Therefore, this function might be removed in the future.

param event_type_str:

The event type string to be parsed

returns:

JSON, parsed event type

trigger(event_obj: Event) Self

To trigger a type of event

Args:

event_type: The type of event to trigger

Example

class Widget(EventHandling, ...):
    ...

my_widget = Widget()
my_widget.trigger(MousePress(pos=(30, 50), button=0))

This shows triggering a mouse.press event in a Widget, which inherited EventHandling so has the ability to handle events.

bind(event_type: type[Event], target: Callable | Iterable, conditions: dict = {}, multithread: bool = False, _is_internal: bool = False, task_obj_receiver: Optional[var.Var[EventTask]] = None, return_task: Literal[True] = True) EventTask
bind(event_type: type[Event], target: Callable | Iterable, conditions: dict = {}, multithread: bool = False, _is_internal: bool = False, task_obj_receiver: Optional[var.Var[EventTask]] = None, return_task: Literal[False] = False) Self

To bind a task to the object when a specific type of event is triggered.

Example

my_button = Button(...).pack()
press_down_event = my_button.bind(event_types.Click, lambda _: print("Hello world!"))

This shows binding a hello world to the button when it's press.

param event_type:

The type of event to be bound to

param target:

A (list of) callable thing, what to do when this task is executed

param condition:

Conditions required for the task to run when event is triggered

param multithread:

If this task should be executed in another thread (False by default)

param _is_internal:

If the task is added by Charmy and should be kept when clear bind

param task_obj_receiver:

Give task object to Var object, if not None, the task object will be assigned to it

return:

EventTask if return_task is True, otherwise the EventHandling itself.

on(event_type: type[event_types.Event], conditions: Optional[dict[str, Any]] = None, multithread: bool = False, _is_internal: bool = False, task_obj_receiver: Optional[var.Var[EventTask]] = None) Callable[[Callable], Callable]

Generate a decorator that binds the event.

→ See bind() for parameters description.

unbind(target_task: EventTask) Self

To unbind a specific task.

Example

my_button = Button(...)
my_task = my_button.bind(event_types.Click, lambda: print("Ouch!"))
my_button.unbind(my_task)

This show unbinding the task bound to Click event from my_button.

param target_task:

The EventTask to unbind.

return:

If success

clear_bind(target_type: type[Event]) Self

To unbind the tasks bound to a specific type of events.

Example

my_button = Button(...)
my_task = my_button.bind(event_types.Click, lambda: print("Ouch!"))
my_button.clear_bind(event_types.Click)

This show unbinding all tasks bound to Click event from my_button.

param target_type:

The event type to clear bind.

return:

If success

class charmy.event.EventTask(target: Callable | Iterable[Callable], conditions: dict[str, Any], multithread: bool = False, _internal_task: bool = False)

基类:CharmyObject

A class to represent event task when an event is triggered.

Each object is to represent a task bound to the event.

Example

This is mostly for internal use of Charmy.

class EventHandling():
    ...
    def bind(self, ...):
        ...
        task = EventTask(event_id, target, multithread, _keep_at_clear)
        ...
    ...

This shows where this class is used for storing task properties in most cases.

param target:

A callable thing, what to do when this task is executed

param multithread:

If this task should be executed in another thread (False by default)

param _internal_task:

If the task is internally created and used by Charmy

target: Callable | Iterable[Callable]
conditions: dict[str, Any]
multithread: bool = False
task_threads: ClassVar[list[Thread]] = []
__init__(target: Callable | Iterable[Callable], conditions: dict[str, Any], multithread: bool = False, _internal_task: bool = False) None
execute(event: Event | None = None) None

Execute the task.

Return value:

Return value of the target if not multitask, otherwise None

class charmy.event.DelayTask(target: Callable | Iterable[Callable], conditions: dict[str, Any], multithread: bool = False, _internal_task: bool = False)

基类:EventTask