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

Variables

Kevin edited this page May 26, 2024 · 12 revisions

All variables inherit their attributes and methods from Variable. See below for examples.

Variable

inherits from GObject.Object

Unlike a regular variable, a variable that inherits from GObject.Object is an object that emits a signal when its internal value changes. This allows for the use of dynamic content within widgets.

Constructor

Parameter Value Expected Default Value Description
initial_value any The initial value of the variable can be of any type.

Methods

Method Arguments Type Description
set_value any Sets the internal value of the variable; when it changes, it emits a signal.
get_value Returns the internal value of the variable.
bind function The function argument must be callable. The bind method is used to connect to the variable. When the internal value of the variable changes, the callback is executed. By default, the new value is passed as an argument to the callback.

Example

my_first_variable = Variable(0)

bogos_binted = Widget.Button(
    children=Widget.Label(my_first_variable),
    onclick=lambda: my_first_variable.set_value(my_first_variable.value + 1)),
)


Poll

inherits from Variable

Poll is a specialized variable designed for polling data at intervals. It allows you to specify a callback function that is executed between iterations.

Constructor

Parameter Value Expected Default Value Description
interval str or int Interval between iterations; it can be "1s", "2m", "3h", or 1000 for milliseconds.
callback callable The callback to execute between iterations.
initial_value any None The initial value of the variable; can be of any type.

Example

from datetime import datetime

# Example using int for milliseconds or str to convert to milliseconds
# clock = Poll("1s", lambda: datetime.now())
clock = Poll(1000, datetime.now)

label_clock = Widget.Label(clock)

## Both approaches work
# label_clock = Widget.Label(Poll(1000, lambda: datetime.now()))


Important

Currently, I have implemented a solution to avoid the use of the Listener Variable, please see examples of recently implemented methods, however, you are free to use it if you like.

Listener

inherits from Variable

This type of variable is special; instead of using a return to return data, a yield should be used. Additionally, for each function assigned to a variable listener, it will be executed in a separate thread using multithreading. This type of variable will only update its internal value when you decide, providing an opportunity to use wrappers or monitor elements.

Constructor

Parameter Value Expected Default Value Description
callback any The callback to execute between iterations.
initial_value any None The initial value of the variable can be of any type.

Example

from PotatoWidgets import Bash, Listener, Widget
DeviceFolder = Bash.get_output("ls -1 /sys/class/backlight", list)[0]

def get_brightness():
    def get_value():
        return Bash.get_output(
                """brightnessctl | grep Current | awk '{print $4}' | sed 's/[(%)]//g' """,
                int
            )

    with subprocess.Popen(
        [
            "inotifywait",
            "-m",
            "-e",
            "modify",
            f"/sys/class/backlight/{DeviceFolder}/brightness",
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.DEVNULL,
        text=True,
    ) as proc:
        for _ in proc.stdout:
            yield get_value()


brightness = Listener(get_brightness)

Brightness_Label = Widget.Label(brightness)
Clone this wiki locally