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)
and style.css
GtkEntry.invalid { background-color: #ffaaaa; background: #ffaaaa; } GtkButton { engine: oxygen-gtk; 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!
source share