Change colour or widget

I wish to have a small square which I can change the colour of with Python. I used to use an EventBox with modify_bg, but modify_bg is deprecated. I currently have a ColorButton which looks fine, but the user can click it and change the colour and I don’t want to allow that. If I disable Sensitive I can still change the colour using Python, but it greys out and the colours look terrible and that is important because the colour will be used for matching. Is there a way of stopping user entry without greying it out? If not is there a better way of doing this? If all else fails, can I be reminded of how to use the EventBox? Preferably using Glade…

GtkEventBox is pretty much strictly for cases where you need an widget that can catch events on behalf of a widget that can’t. It’s unclear from your question whether you actually need that, but colouring a widget doesn’t really have anything to do with GtkEventBox.

Probably what you’re going to want to do is either add a style class (in glade if you want):

<object class="GtkBox">
    <class name="my-css-class"/>

CSS stylesheet:

.my-css-class {
  background-color: #f00;

Or get the GtkStyleContext and set the colour that way.

Use Gtk.DrawingArea and use Cairo to draw a solid color from the “draw” signal handler. You will need a write a subclass, and control the color with a property.

@andyholmes @ebassi
Woa! I’m a newbie and those things are all well beyond me. I have no idea what a style-class or a sub-class is and even less how to create one.
Both the methods I used were very simple. Should I assume from your suggestions that there is no non-deprecated way of changing the colour of an EventBox and there is no way of de-sensitising a ColorBox without it greying out? If so I’ll probably just use the deprecated modify_bg characteristic.

I’d strongly recommend you start learning how styling and the type system used in GTK work, as they are part of the toolkit, and it’s likely you’ll need them.

For the time being, you can keep using the deprecated API; it will keep existing for as long as GTK3 is available.

Thanks, it would take me weeks to learn that and since I have finished the app apart from improving a couple of aesthetic points I think I’ll manage with using the deprecated modify_bg.

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