How to switch between several layouts in Makie?

Hello,
I am creating a GUI in Makie, and I would like to be able to change the layout with a menu widget. So far, I can add widgets (GridLaouts) at runtime, but I didn’t find how to hide or replace them. See the mwe below. It should switch between layouts, but it puts one over another instead.

using GLMakie

items =["buttons", "slider"]
fig = Figure()
fig[1,1] = menu = Menu(fig, options=items)

on(menu.selection) do s
    if s == "buttons"
        fig[2,1] = gl1 = GridLayout()
        gl1[1,1] = Button(fig, label ="button 1")
        gl1[1,2] = Button(fig, label ="button 2")
    else
        fig[2,1] = gl2 = GridLayout()
        gl2[1,1] = Slider(fig, range= 0:1)
    end
end

fig

Maybe compare with this recent post, the deletion functionality of Makie is not yet that well figured out:

A GridLayout only describes where to put certain elements, and multiple objects can be in the same location, that’s why your approach doesn’t work. It’s not like a matrix where you overwrite things.

Thank you for the answer. I’ll try it later today.
Isn’t there any possibility of hiding the gridlayout instead of deleting it?

The GridLayout is not visible so you can’t hide it, only the objects placed using it. But there’s currently no great way to hide Block objects. What I’ve done once a long time ago was to place an independent GridLayout offscreen and put things in that to “hide” them. In your case you could move gl1 or gl2 there whenever you need the other one. To place a grid outside, use the bbox argument with a negative boundingbox like bbox = BBox(-200, -100, 0, 100)

Thank you, it works this way.

For anyone interested, the working code looks like this:

using GLMakie

items =["buttons", "slider"]
fig = Figure()
fig[1,1] = menu = Menu(fig, options=items)
hiddenlayout = GridLayout(bbox = BBox(-200, -100, 0, 100))

# firts layout
gl1 = GridLayout()
gl1[1,1] = Button(fig, label ="button 1")
gl1[1,2] = Button(fig, label ="button 2")

# second layout
gl2 = GridLayout()
gl2[1,1] = Slider(fig, range= 0:1)

# initaly show the first one
fig[2,1] = gl1
hiddenlayout[1,1] = gl2

on(menu.selection) do s 
    if s == "buttons"
        fig[2,1] = gl1
        hiddenlayout[1,1] = gl2
    else
        fig[2,1] = gl2
        hiddenlayout[1,1] = gl1
    end
end

fig

Glad that you got something to work, I hope this will get more convenient in the future :slight_smile:

This is the code which emerged from that :slight_smile: