I have recently started using pygtk/PyGObject and have been trying to apply or change the background color or a simple button or any other widget using the following line of code obtained from one of the QA here.
self.button.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0.0, 1.0, 0.0, 1.0))
But that does not seem to apply or work.
The whole sample test program is here.
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWIndow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.button = Gtk.Button(label="Click")
self.button.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0.0, 1.0, 0.0, 1.0))
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
Gtk.main_quit()
win = MyWIndow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Is there anything that I am missing? Thanks in advance.
This method was used on Gtk+ 2.0 and seems it was also used on the first versions of Gtk+ 3.0 but it was deprecated in version 3.16:
From the Python API:
New in version 3.0.
Deprecated since version 3.16: This function is not useful in the context of CSS-based rendering. If you wish to change the way a widget renders its background you should use a custom CSS style, through an application-specific Gtk.StyleProvider and a CSS style class. You can also override the default drawing of a widget through the Gtk.Widget ::draw signal, and use Cairo to draw a specific color, regardless of the CSS style.
More info on Migration to CSS.
Your example using predefined css classes (suggested and destructive actions):
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWIndow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.button = Gtk.Button(label="Click")
self.button.get_style_context().add_class("suggested-action")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
self.button.get_style_context().add_class("destructive-action")
win = MyWIndow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()