Actions and widgets on GTK

I want to have several buttons connected to the same action, to have something similar to a calculator or a keyboard; instead of programming an action for each button, I want them all to do the same thing. But, is there any way to know which button I’m calling that action from? I’m working with Vala.

Sorry for my English and thank you very much in advance.

Why do you need a way to know which button you’re calling the action from? Actions are, by design, separated from the UI. If you want a reference to the button, you can use the Gtk.Button::clicked signal instead.

Hi,

You can use an action with a parameter, for example:

# Python code
action = Gio.SimpleAction.new('myaction', GLib.VariantType('s'))
action.connect('activate', on_myaction_activate)
actiongroup.add_action(action)

Then connect a detailed name with different params to identify your buttons:

# Python code
button_apples.set_detailed_action_name('mygroup.myaction::apples')
button_oranges.set_detailed_action_name('mygroup.myaction::oranges')

You can then retrieve the parameter (apples or oranges here) in the action callback:

# Python code
def on_myaction_activate(action, param):
    print(param.unpack())

Examples are in Python, but should be similar in Vala.