Help with ColorDialog Callback in Python

I’m using Python 3.11 and PyGObject 3.441 (Gtk4).

My window controller class adds the Gtk.ColorDialogButton to the window, then attaches Gtk.ColorDialog to the button:

colour_button: Gtk.ColorDialogButton = Gtk.ColorDialogButton()
colour_dialogue: Gtk.ColorDialog = Gtk.ColorDialog()
colour_button.set_dialog(self.colour_dialogue)

All is well and functions as expected, click the button and the dialogue appears. make a selection and press the Select button and the dialogue closes.

Having made the colour choice and pressed the ‘Select’ button I need the selection passed back via a callback to the window controller. i.e.

colour_dialogue.connect('notify::selected-item', controller.callback_function)

There is some fantastic documentation for ‘c’, rust and Vala, but I’m not experienced enough to translate from them to Python. And I continually find PyGOBject is woefully out of date.

If any one could provide a python example or a code snippet I would be grateful.

Regards
Greg

Solved …

colour_dialogue.connect('notify::selected-item', controller.callback_function)

Should be:

colour_dialogue.connect('notify', controller.callback_function)

I’m not sure where the ‘selected-item’ came from other than it appears in the signal names of other widgets in my code.
I had to try something :upside_down_face:

I’m not sure what that’s notifying on then, or whether it’s the best way to do it… If you only connect to notify generally, not for a specific property, you will probably find signal emissions happen when you don’t want them to, about properties you don’t care about. It also doesn’t make for clear code. :wink:

I would say to connect to changes/notify on ColorDialogButton:rgba instead; that seems far clearer.

Hi dboles,

Thank you for responding, your comment gave something to consider.

The GTK Documentation for ColorDialogButton mentions inherited signals from GtkWidget but none of these appeared to meet my needs. However the inherited ‘notify’ signal from GObject is mentioned:

GObject::notify
The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.

My understanding is therefore: the notify callback only informs you something has changed and you have to go and look what it was.

The Documentation for ColorDialog also mentions GObject::notify, something I missed on my first reading. As a result I looked again at my actual solution and noticed I had applied the callback to ‘colour_button’ not ‘colour_dialogue’. The code snippet should therefore read:

colour_button.connect('notify', form_row_data.callback_function)

Incidentally: ’ form_row_data’ in the snippet is a dataclass I use to pass various parameters that specify such items as a title, width, sub-title and the callback_function.

Regards

Greg

Yes, so my point is that you probably want notify::rgba, so that you will only be notified when that property changes, not when any does, otherwise you’re creating work having to figure out if the property you care about is the one that changed, when there is no need to incur that work.

My understanding is therefore: the notify callback only informs you something has changed and you have to go and look what it was.

Not quite; there is the optional ::detail syntax that lets you only receive notifications for a single specific property. See the doc:

This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in the g_signal_connect() call, like this:
g_signal_connect (text_view->buffer, "notify::paste-target-list", [...]

Ok - I think I now understand better. Also I must have made a mistake when I investigated ‘notify::rgba’ previously because I could get it to work. No error reported just no callback. Even PyCharms linter didn’t complain.

Anyway, using only ‘notify’, the callback function gets called three times when the ColorDialogButton is instantiated and passed a default colour setting:

colour_changed_cb called: (<Gtk.ColorDialogButton object at 0x7f423cee5380 (GtkColorDialogButton at 0x5632c7a58310)>, <GParamBoxed ‘rgba’>)
colour_changed_cb called: (<Gtk.ColorDialogButton object at 0x7f423cee5380 (GtkColorDialogButton at 0x5632c7a58310)>, <GParamObject ‘parent’>)
colour_changed_cb called: (<Gtk.ColorDialogButton object at 0x7f423cee5380 (GtkColorDialogButton at 0x5632c7a58310)>, <GParamObject ‘root’>)

And once when the ColorDialog ‘Select’ is pressed:

colour_changed_cb called: (<Gtk.ColorDialogButton object at 0x7f423cee5380 (GtkColorDialogButton at 0x5632c7a58310)>, <GParamObject ‘root’>)

If ‘notify::rgba’ is used then the callback is only called once when the ColorDialogButton is instantiated and once when the ColorDialog ‘Select’ is pressed.

The callback still only receives the one call when the 'Select button is pressed. i.e. still works the same and made no difference as to how the callback processing. In this case.

However from a general perspective I can appreciate the significance of adding the property tag. I feel I am now a little wiser. I thank you for you patients and persistence.
regards
Greg

1 Like

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