Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Services

Kevin edited this page May 27, 2024 · 6 revisions

A service is a class that inherits from GObject.Object. It is a singleton instance that emits signals when an internal value changes or when it receives new values. By default, one can connect to the signals with:

Service.connect("signal-name", callback)

An example to test could be the BatteryService:

from PotatoWidgets.Services import BatteryService

BatteryService.connect("percentage", lambda _, value: do_something_with_this_value(value))

Now, for example, to use it with widgets:

from PotatoWidgets import Widget
from PotatoWidgets.Services import BatteryService

MyDynamicLabel = Widget.Label("")

BatteryService.connect("percentage", lambda _, value: MyDynamicLabel.set_text(value))

# or
OtherDynamicLabel = Widget.Label(
    attributes=lambda self: BatteryService.connect(
        "percentage", lambda _, x: self.set_text(x)
    )
)

In the previous case, the instance BatteryService is called, and the connect method is used. However, there is also another method for services, which is better to use when services return useful information that should not be modified directly. For example, in a progress bar:

from PotatoWidgets import Widget
from PotatoWidgets.Services import BatteryService

Percentage = BatteryService.bind("percentage")

BatteryProgress = Widget.ProgressBar(value=Percentage)

In this example, the bind method will return a variable that will be updated when the service emits the specified signal.

For more examples check at the Examples page

Clone this wiki locally