How to connect an event handler to a button in Makie.jl?

How can I execute a function when I press a button in Makie?

This is missing in the documentation.

Example:

using GeometryBasics, GLMakie

function reset()
    # set camera and zoom to default values
end

function main()
    scene, layout = layoutscene(resolution = (800, 900), backgroundcolor = RGBf0(0.7, 0.8, 1))
    scene3D = LScene(scene, scenekw = (show_axis=false, limits = Rect(-7,-10.0,0, 11,10,11), resolution = (800, 800), camera = cam3d_cad!, raw = false))

    layout[1, 1] = scene3D
    layout[2, 1] = buttongrid = GridLayout(tellwidth = false)

    buttongrid[1, 1:3] = [Button(scene, label = "RESET"), Button(scene, label = "Zoom +"), Button(scene, label = "Zoom -")]
    return scene
end

I want that the function reset() is called when I click the button “RESET”.

Full code available at: https://github.com/ufechner7/KiteViewer

Huh you’re right. The button has a clicks attribute that counts clicks which you can lift / on

on(button.clicks) do c
    println("pressed $c times")
end
2 Likes

This is indeed working. So simple!

Perhaps it can be added to the documentation.

I will add an example soon I have tagged new docs with an updated example

4 Likes