# How to do context menus in Gtk4?

**URL:** <https://discourse.julialang.org/t/how-to-do-context-menus-in-gtk4/102569>\
**Category:** Visualization\
**Tags:** gtk\
**Created:** [August 7, 2023, 3:50pm UTC](https://discourse.julialang.org/t/how-to-do-context-menus-in-gtk4/102569 "2023-08-07T15:50:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwlidar](https://avatars.discourse-cdn.com/v4/letter/m/6de8d8/32.png) [@mwlidar](https://discourse.julialang.org/u/mwlidar)\
**Post date:** [August 7, 2023, 3:50pm UTC](https://discourse.julialang.org/t/how-to-do-context-menus-in-gtk4/102569/1 "2023-08-07T15:50:52Z")

</div>

Hi!  
After some trying I learned how to make a popup-menu over a widget (context-menu) in Gtk3. Now I’m trying to do the same in Gtk4, where all seems to have changed. The code below defines a GtkPopoverMenu from a GMenu and a GActionGroup for the main window. If I run the code und right klick on the label, the context menu appears as expected. But the actions from the ActionGroup are not associated with the buttons in the popup (label text is grey) and the handlers are not called. So there is something missing to really connected the actions to the buttons in the popup. My hope is that somebody knows what is missing…

```julia
function first_handler(a, v)
    println("Hello")
end

function second_handler(a, v)
    println("Hello Hello")
end

function menutest()
    win = GtkWindow("menutest")
  
    menu = GMenu()
    firstitem = GMenuItem("First", "win.first")
    push!(menu, firstitem)
    seconditem = GMenuItem("Second", "win.second")
    push!(menu, seconditem)
    popper = GtkPopoverMenu(menu, has_arrow = false, position = Gtk4.PositionType_RIGHT)

    ag = GLib.G_.SimpleActionGroup_new()
    add_action(GActionMap(ag), "win.first", first_handler)
    add_action(GActionMap(ag), "win.second", second_handler)
    Gtk4.G_.insert_action_group(win, "win", GActionGroup(ag))

    lab = GtkLabel("The label")
    push!(win, lab)
    Gtk4.G_.set_parent(popper, lab)

    g = GtkGestureClick(lab)
    g.button = 3 # accept right button
    signal_connect(g, "pressed") do controller, n_press, x, y
        popup(popper)
        return Cint(true) # event taken
    end

    show(win)
end

menutest()

```

---

<div class="post-metadata">

**Author:** ![jwahlstrand](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@jwahlstrand](https://discourse.julialang.org/u/jwahlstrand)\
**Post date:** [August 7, 2023, 9:28pm UTC](https://discourse.julialang.org/t/how-to-do-context-menus-in-gtk4/102569/2 "2023-08-07T21:28:12Z")

</div>

It should work if you drop the “win.” from the action names in the `add_action` calls:

```julia
add_action(GActionMap(ag), "first", first_handler)
add_action(GActionMap(ag), "second", second_handler)

```

When you insert the action group with the name “win”, the actions within that group have the “win.” prefix added.

---

<div class="post-metadata">

**Author:** ![mwlidar](https://avatars.discourse-cdn.com/v4/letter/m/6de8d8/32.png) [@mwlidar](https://discourse.julialang.org/u/mwlidar)\
**Post date:** [August 8, 2023, 7:49am UTC](https://discourse.julialang.org/t/how-to-do-context-menus-in-gtk4/102569/3 "2023-08-08T07:49:45Z")

</div>

Ah, thanks very much! That did the job! Tried several variations for the action keys, but not this one … 😂
