A Gotcha How To: An “Inactive”, “Disabled”, or “Ghosted” Button which also has a Set Action Name and associated Simple Action

Having tried and failed to enable and disable a button in my application I thought I would share my findings. Comments and feedback welcome.

I was using the buttons superclass method “set_action_name” to control the button. If there is no method call to the buttons Gtk.Actionable interface “set_action_name”. Then “set_sensitive” is enough.

If the button is associated with a Gtk.SimpleAction then a call to Gtk.Actionable interface “set_action_name” will have been set. The consequences being: trying to control the button using “set_sensitive” fails. However Gtk.SimpleAction has a method “set_enabled” which also enables and disables the signal but it interacts with the buttons set_sensitive setting.

Below is a table identifying the combinations that work and those that don’t:

Button.set_sensitive(boolean) SimpleAction.set_enabled(boolean) Button State:
true na Active
false na Inactive
true true Active
true false Active
false true Active
false false Inactive

Below is some pseudocode to demonstrate the solution:

// The button setup
button = Gtk.Button();
button.set_sensitive(false);  // trying to control the button here will FAIL.

// The simple action setup
simple_action = GLib.SimpleAction("app.button_cb", null);
simple_action.set_enabled(false);  // use this to control the button.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.