Why css style doesn't work on GtkButton?

Sorry for my english.

I am trying to change the background color of a GtkButton using a css file, but I cannot. I tried a few examples that I found on the Internet, but no one works. I am writing two examples. One in Python 3.2.3 and the other in C I use Gtk + 3.6 and Kubuntu 12.10.

This is the code for 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 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; } 

Records work well ... bg color change. But the "No" and "No error messages" buttons.

EDIT3: (Deleted previews edit and modify some tags) Summarizing ... I tried to change the color of the button with all the Python, C and C ++ codes that I found on the Internet unsuccessfully. I read all the tutorials I found and the GTK + 3 reference manual. All I know after that is that the problem is with Kubuntu themes: if I change the GTK theme from "oxygen-gtk" to "default" (in GTK configuration), this is the only way I found that the code works well, but that is not an idea, and the button looks awful.

So the questions are:

  • Why can't I change the background color of the button?
  • Why do I have this problem with buttons only? (Works well with other widgets)
  • I get answers here and on the GTK forums saying that it is not a good practice to change the colors of the buttons, but ... What if I need a menu similar to the image in this image ( link ) (see red buttons)? Is this the best practice for you?

Thanks and welcome!

+4
source share
3 answers

I know this is pretty old, but appeared in the first few results of Google, so I decided to share my experience.

Gtk.Button has a built-in Gtk.Label for button text, which by default is not inherited from the button, so you need to explicitly specify it (or just specify the color in it):

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

Regarding the answer from @sciamp, the GTK theme sets the image for the background and borders, so you need to manually remove this with background-image: none; border-image: none; background-image: none; border-image: none; Hope this saves someone.

+5
source

This should work (I mean this works for me!):

 GtkButton { border-image: none; background-image: none; background-color: green; } 
+2
source

It is difficult, but I do not think that it can be done directly.

I believe that the main reason is that the button does not display the background. All he does is frame the frame around his area, and then draw any children inside. Remember that GtkButton is a container, it usually contains GtkLabel for a text label, but can contain any widget.

I managed to change the background color of text labels, but it is affected only by a much quieter box around the text itself. This is not what you want.

An indirect solution is to subclass GtkButton to create a variant that actually displays its background. This, of course, is rather rude on topics and should be avoided.

+1
source

All Articles