6

How do I do it? A lot of sites say I can just call .modify_bg() on the button, but that doesn't do anything. I'm able to add an EventBox to the button, and add a label to that, and then change its colors, but it looks horrendous - there is a ton of gray space between the edge of the button that doesn't change. I just want something that looks like this:

img
(source: kksou.com)

The site claims to have just done modify_bg() on the button. But that doesn't work for me. =(.

The right answer probably involves creating a style, or something with a gtkrc file, etc. Can someone point me in that direction?

Community
  • 1
  • 1
Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • 2
    why would the creators of GTK make it so hard to do such a simple thing. they even have a function called modify_bg. it doesn't do anything. i've been trying to do this for like 2 hours now. – Claudiu Aug 06 '09 at 19:58

1 Answers1

16

Here's a little example:

import gtk

win = gtk.Window()
win.connect("destroy", gtk.main_quit)

btn = gtk.Button("test")

#make a gdk.color for red
map = btn.get_colormap() 
color = map.alloc_color("red")

#copy the current style and replace the background
style = btn.get_style().copy()
style.bg[gtk.STATE_NORMAL] = color

#set the button's style to the one you created
btn.set_style(style)

win.add(btn)
win.show_all()

gtk.main()
JasonFruit
  • 7,764
  • 5
  • 46
  • 61
  • ooh finally. that actually works. i saw an example like this, but the website i saw it on said that .modify_bg was invented to avoid having to do stuff like this. thanks!! – Claudiu Aug 06 '09 at 21:42
  • How to implement this in C – ganeshredcobra Feb 25 '14 at 09:06
  • This explains why most gtk apps (Gimp, Gnucash, gedit) are that drab grey, because of how complicated it is to add a dash of color here and there... – Tom Dec 25 '17 at 21:30