Search code examples
pythongtk3pygobject

How to change background of textview in pygobject "gtk3"?


i am writing a text editor in gtk 3 using python. in gtk 2 i used to do self.modify_base(Gtk.StateType.NORMAL, Gtk.gdk.Color(bg)) in PyGObject i think it has to do something with style context but i am not sure what is the right way, i found only this documentation on the web, but it is not enough: python gtk3 tutorial i thought this code maybe the beginning of it:

    context = self.get_style_context()
    context.set_background(Gtk.STYLE_PROPERTY_BACKGROUND_COLOR)

so any ideas ?


Solution

  • i found the answer, first thing you gotta import Gdk.

    from gi.repository import Gdk
    

    supposing that Gtk.TextView instance is called doc

    doc.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0, 0, 0, 1))
    

    change the Gdk.RGBA(0, 0, 0, 1) as you wish

    rgba, each value lies between 0.0 and 0.1, r stands for red, g for green, b for blue and a stands for alpha.