Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why css style don't work on GtkButton?

Please excuse my english.

I'm trying to change the background color of a GtkButton using a css file but I can't. I tried a few examples I found on the web, but none work. I post two examples. One in Python 3.2.3 and the other in C I'm using Gtk+ 3.6 and Kubuntu 12.10.

This is the code of one of them:

from gi.repository import Gtk, Gdk

class MainWindow(Gtk.Window):
    def __init__(self):
        super().__init__()
        vbox = Gtk.Box(spacing=10,orientation=Gtk.Orientation.VERTICAL)
        self.add(vbox)

        self.entries = [ Gtk.Entry() for i in range(3) ]
        for e in self.entries:
            vbox.pack_start(e, True, True, 0)
            e.connect("changed", self.on_entry_changed)
            e.set_text('123')

        button=Gtk.Button(label='ok')
        vbox.pack_end(button,True,True,0)

    def on_entry_changed(self,entry):
        ctx = entry.get_style_context()
        if not entry.get_text().isnumeric():
            ctx.add_class('invalid')
        else:
            ctx.remove_class('invalid')
cssProvider = Gtk.CssProvider()
cssProvider.load_from_path('style.css')
screen = Gdk.Screen.get_default()
styleContext = Gtk.StyleContext()
styleContext.add_provider_for_screen(screen, cssProvider,
Gtk.STYLE_PROVIDER_PRIORITY_USER) # With the others GTK_STYLE_PROVIDER_PRIORITY values get the same result.

window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()

and the style.css

GtkEntry.invalid {
    background-color: #ffaaaa;
    background: #ffaaaa;
}

GtkButton {
    engine: oxygen-gtk; /*tried also with 'none' and without setting engine*/
    background-color: green;
    background: green;
}

The entries works well... the bg color change. But the Button no, and theres no error messages.

EDIT3: (Deleted previews edits and change some tags) Summarizing... I tried to change the button color with all the Python, C, and C++ codes I found in the web unsuccessfully. I read all the tutorials I found and the GTK+ 3 reference manual. All that I know after that is that the problem is about Kubuntu themes: If I change the GTK theme from 'oxygen-gtk' to 'default' (in GTK Configuration), is the only way I found that the code works well, but this is not the idea and the button looks horrible.

So, the questions are:

  1. Why I can't change the background color of the button?
  2. Why I having this problem only with buttons? (Works well with other widgets)
  3. I get answers here and in GTK forums saying that is not a good practice to change button colors, but... What if I want a menu like the one in this image (link) (see red box buttons)? Wich is the best practis for that?

Thanks and greetings!

like image 468
Vanesa Milagros Fernandez Avatar asked Feb 18 '23 11:02

Vanesa Milagros Fernandez


1 Answers

I know this is quite old, but popped up in the first few google results, so I thought I'd share my experience.

Gtk.Button has an inline Gtk.Label for the button text, that doesn't inherit from the button by default, so you have to explicitly tell it to (or just specify the colour in it):

GtkButton GtkLabel {
    color: #fff; /* This changes the text color in the button */
}

As far as the answer from @sciamp, the GTK theme sets an image for the background and the borders as well, so you have to manually remove that with background-image: none; border-image: none; Hope this saves someone the struggle.

like image 98
Iskren Avatar answered Feb 20 '23 11:02

Iskren