Entry Widget with a Switcherable Border Colour - Highlighting a missing entry

I’m using GTK4 and Python 3.11 to write an application which uses form data entry. Some of the entry fields are mandatory i.e. they must be completed. I would like to highlight the incomplete entries on pressing the save button. I have everything working except missing field highlighting.

Its my understanding styling can only be done using css and is static, that is it cannot be overridden under program control, i.e. select an alternative css class. It therefore appears impossible to achieve the highlighting I’m looking for.

If any one can point me in the right direction, an application with similar functionality, an alternative approach etc. I would be grateful.


You definitely can change the CSS classes of any widget during execution using the add_css_class and remove_css_class methods. You can even use the error CSS class to get a red outline (at least with Libadwaita).

For instance, in your clicked signal handler:

if entry.props.text == "":

Thanks Romain for taking the time to reply, appreciated. I confess I didn’t pickup on the remove_css_class and its potential so I’ll follow that thread.

I am aware of Libadwaita but have never really considered it. Possibly because when I look at the Gtk Doc / Apis page its not mentioned so I’m guessing its a Gnome library. However thanks to the API link. I’ll take a closer look.


Yes, Libadwaita is for apps targeting the GNOME platform. Anyway, the error class may be available in GTK, and if it isn’t, it’s easy to create your own.

Thanks again Romain, useful stuff.
I managed to get it to work as per your original suggestion, add / remove the css_class. I already had a flag identifying required entries and I simply counted the number of flags and the number of completed entries then comparing the two values. The rest is history, as they say.

1 Like

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