bgui - changing frame alpha?

Is there some way to control the alpha of a frame, so when you change the alpha of the frame, the contents of the frame also change? I couldn’t see anything about this in the API docs for it. Any pointers?


The bgui doesn’t have a uniform way of setting a widget’s alpha. Frames store it in an attribute called colors, images an attribute called color, progress bars an attribute called bg_colors and fill_colors. Colors are store in dictionaries, lists, tuples, lists of tuples.

Widgets should have a dedicated alpha attribute though, I’ll open an issue for you on the google code page.

In the meantime, something such as this may suffice:

def set_alpha(widget, alpha):    def replace_alpha(attr, alpha):
        if isinstance(attr, (list, tuple)):
            attr = list(attr)
            if isinstance(attr[0], (list, tuple)):
                for i in range(len(attr)):
                    attr[i] = replace_alpha(attr[i], alpha)
                return attr
            elif isinstance(attr[0], (int, float)):
                attr[3] = alpha
                return attr
        elif isinstance(attr, dict):
            for key in attr:
                new_attr = replace_alpha(attr[key], alpha)
                if new_attr is not None:
                    attr[key] = new_attr
            return attr

    for attr_name in dir(widget):
        if 'color' in attr_name:
            attr = getattr(widget, attr_name)
            attr = replace_alpha(attr, alpha)
            if attr is not None:
                setattr(widget, attr_name, attr)

    for child in widget.children:
        set_alpha(widget.children[child], alpha)

It’s not very pretty, the above function looks for all attributes with ‘color’ in their name and handles each one according to if its a list, list of lists or a dictionary. It then recursively calls itself to handle all the widget’s children.

Thank you for your response andrew, I’ll give that a function a go later on today. Is it meant to placed inside of in bgui, or for use in scripts outside of bgui? Either way, I’m trying it later on. :slight_smile:

And thanks for opening an issue on googlecode. There should definitely be an easier way to change a widgets alpha. BGUI seems to run pretty darn fast, so hopefully changing alpha for widgets doesn’t hinder performance too much.

The snippet of code can be placed anywhere, call it like so: set_alpha(frame, 0.5).

You can also inherit from the base classes, that bgui provides and extend or override the default behavior. The other way is extending the library itself. The implementation looks always something like iterating over the children and setting their alpha values.