Groups allow you to manage the elements that are rendered on the display. You can think of them like layers that can be removed or displayed. You can even have a Group inside of another Group and that is exactly what we are going to do but first we need to set a few things up.

Define a Group

To define a display group we want to use the displayio.Group() function. This function requires you to set a max_size that puts a limit to how many objects that your group can hold. So if I know that I will only be using two buttons and a text box for one group, I would use displayio.Group(max_size=3). If I then added a fourth object to that group, I would get an error message telling me that the Group is already full. So you may want to add a few more spots that you need for your group

Here is an example of some groups that we would want to setup for our UI.

Download: file
# ------------- Display Groups ------------- #
splash = displayio.Group(max_size=10) # The Main Display Group
view1 = displayio.Group(max_size=15, x=0, y=40) # Group for View 1 objects
view2 = displayio.Group(max_size=15, x=0, y=40) # Group for View 2 objects
view3 = displayio.Group(max_size=15, x=0, y=40) # Group for View 3 objects

This sets up one Main display group called splash and three other groups that will be used to display selectable tab views. Notice how we have set the view groups to be moved down 40 pixels. This is so that these groups will start their display coordinates from x=0, y=0 just underneath where our Tab buttons will be rendered. It will just make the layout of those pages a little easier to manage.

Adding to a Group

Now that we have a few groups, we will want to assign them to our splash group so that they can be part of the display image. This is done by using .append() to add you object to the list of objects in the group.

Here is how it looks to add our three view groups to our main group:

Download: file
splash.append(view1)
splash.append(view2)
splash.append(view3)

Later, we will be adding objects like buttons, text, and images to these Groups.

Other helpful functions

here are a few other helpful functions that we will be using to manage our groups.

  • pop() - This is basically used to delete a Group. We will use this later for switching Icon images.
    • group.pop() - This deletes the first element in group.
    • group.pop(2) - This deletes the third element in group.
    • view3.pop(-2) - This deletes the second to last element in the view3 Group.
  • remove(i) - This is used to remove a Group or Object from a Group. It will not delete the Object or Group, but it can be used to temporarily remove that element for the display.
    • group.remove(object) - This will remove the element object from group
    • splash.remove(view2) - This will remove the view2 Group from the splash Group

For CircuitPython 5 and greater, there is a new function called hidden and it can be used to hide a Group or Object in a Group.

  • hidden = bool - Set this to True to hide all elements of a group.
    • splash[4].hidden = True - This will hide the 5th Group or Object in the splash Group.
    • splash[4].hidden = False - This will show the 5th Group or Object in the splash Group.

While this function will hide something from view, it will not always bring it back unless the display is refreshed. For this reason, we will be using another method for showing and hiding Groups.

Hide and Show a Group

The biggest reason we want to use Groups is that we can use them like Layers to show and hide making it possible to have multiple screen views. For this example we are going to use buttons that will let us switch between view1, view2, and view3. This will be done by showing one of those pages while hiding the other two. To make this easy, and because we will be doing it a lot, we will make a function for hiding a group and another for showing a group.

Download: file
# ------------- Layer Functions ------------- #
# Hide a given Group by removing it from the main display Group.
def hideLayer(i):
    try:
        splash.remove(i)
    except ValueError:
        pass
# Show a given Group by adding it to the main display Group.
def showLayer(i):
    try:
        splash.append(i)
    except ValueError:
        pass

We will go over how this function gets used in the Usage section.

For more information on Groups, check out the link below for the CircuitPython documentation.

This guide was first published on Feb 10, 2020. It was last updated on Feb 10, 2020. This page (Groups) was last updated on Feb 20, 2020.