Search code examples
pythoncolorsgtkpygtk

Gtk. widget - can't change bg color. Pygtk


I writing gtk interface with python code. the problem is:

init entry widget by gtkbuilder:

self.some_entry = self.builder.get_object('SomeEntry')

Define signal by typing button, after then must changed entry color:

def on_SomeButton_clicked(self, widget): self.some_entry.modify_bg(Gtk.StateType.NORMAL,Gdk.Color(20000,10000,10000))

but it doesn't work, such 'modify_base'. And I don't know why. Help please. Sorry for my English(


Solution

  • EDIT2: Turns out it was an entry box giving the problem which is another issue in and of itself because the background is not the property you need to modify but the BASE color property which can typically be set using:

    self.entry.override_background_color(Gtk.StateType.Normal, Gdk.RGBA(0.0, 1.0, 0.0, 1.0))
    

    However for OP it was not working so a CSS option was explored, listed at: https://mail.gnome.org/archives/gtk-app-devel-list/2005-November/msg00236.html

    EDIT: So working with PyGtk3 I was able to get a button changing color using you line of code:

    self.button.modify_bg(Gtk.StateType.Normal, Gdk.Color(20000, 10000, 10000))
    

    It was grey on initialization and dark red after the code ran. The only thing I could think of is make sure that the object you are trying to modify is actually in a NORMAL state after you run the code and make sure the signal you think is triggering is actually triggering.

    ==============

    Original post:

    Without having the full code here there are a few things that could be causing this. I just threw together a test program in Python based off of: http://pygtk.org/pygtk2tutorial/examples/helloworld.py

    When I set the Gtk State for the modify_bg I had to use:

    gtk.STATE_NORMAL
    

    Not sure if that is due to a different version of Gtk or what though. Then when I went to use Gdk I had to refer to it as:

    gtk.gdk
    

    The line that I ended up with to change the button color was:

    self.button.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(20000, 10000, 10000))
    

    Hopefully that works out, in order to get any more detailed though we would definitely need more code and to know what kind of errors you are getting.