Skip to content

widgets

change_widget(root_widget, address, sub_widget)

Substitutes a sub_widget in a root_widget. Address is a sequence of childhood ranks as a string, root_widget first child address is '0' The root_widget is altered but the object remains the same

Source code in src/antakia/gui/widgets.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def change_widget(root_widget: Widget, address: str, sub_widget: Widget | str):
    """
    Substitutes a sub_widget in a root_widget.
    Address is a sequence of childhood ranks as a string, root_widget first child address is  '0'
    The root_widget is altered but the object remains the same
    """
    try:
        int(address)
    except ValueError:
        raise ValueError(address, "must be a string composed of digits")

    parent_widget = _get_parent(root_widget, address)
    new_children = []
    for i in range(len(parent_widget.children)):
        if i == int(address[-1]):
            if sub_widget is not None:
                new_children.append(sub_widget)
        else:
            new_children.append(parent_widget.children[i])
    parent_widget.children = new_children

check_address(root_widget, address)

For debug purposes : check if the address is reachable and returns the widget class

Source code in src/antakia/gui/widgets.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def check_address(root_widget: Widget, address: str) -> str:
    """
    For debug purposes : check if the address is reachable and returns the widget class
    """

    widget = root_widget.children[int(address[0])]
    txt = f"[{address[0]}] : {widget.__class__.__name__}"
    if len(address) == 1:
        return txt
    elif widget is not None and len(widget.children) > 0:
        # let's continue further :
        return txt + ", " + check_address(widget, address[1:])
    else:
        # address targets a non existent widget :
        return txt + f", nothing @[{address[0]}]"

get_widget(root_widget, address)

Returns a sub widget of root_widget. Address is a sequence of childhood ranks as a string Return sub_widget may be modified, it's still the same sub_widget of the root_widget get_widget(root_widget, '0') returns root_widgetn first child TODO : allow childhood rank > 9

Source code in src/antakia/gui/widgets.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def get_widget(root_widget: Widget, address: str) -> Widget:
    """
    Returns a sub widget of root_widget. Address is a sequence of childhood ranks as a string
    Return sub_widget may be modified, it's still the same sub_widget of the root_widget
    get_widget(root_widget, '0') returns root_widgetn first child
    TODO : allow childhood rank > 9
    """
    try:
        int(address)
    except ValueError:
        raise ValueError(address, "must be a string composed of digits")

    if len(address) > 1:
        try:
            return get_widget(root_widget.children[int(address[0])], address[1:])
        except IndexError:
            raise IndexError(f"Nothing found @{address} in this {root_widget.__class__.__name__}")
    else:
        if isinstance(root_widget, v.Tooltip):
            return root_widget.v_slots[0]["children"]
        else:
            return root_widget.children[int(address[0])]