(Reposted from the old blog)
This time I’m going to talk about putting an image as the application background in Gtk. In Gtk we are used to leave the colors of the application to the theme, but sometimes we will need to use an image as background. I already wrote how to draw a pixbuf in a gtk.DrawingArea (Esp), we could use that, but we will “draw” directly on the widget window instead.
Yes, I said the widget’s window instead the widget itself. You should know that every widget that has been packed in a container has a gtk.gdk.window object and is the responsible for containing your widget. Well, we can draw on that object.
The code should look like this:
#!/usr/bin/env python import gtk def draw_pixbuf(widget, event): path = '/home/markuz/wallpapers/WMwall1024x768.gif' pixbuf = gtk.gdk.pixbuf_new_from_file(path) widget.window.draw_pixbuf(widget.style.bg_gc[gtk.STATE_NORMAL], pixbuf, 0, 0, 0,0) window = gtk.Window() window.set_title('Drawing Test') window.set_size_request(640,480) window.connect('destroy',gtk.main_quit) hbbox = gtk.HButtonBox() window.add(hbbox) hbbox.connect('expose-event', draw_pixbuf) button = gtk.Button('Press Me!') hbbox.pack_start(button, True, False, 10) window.show_all() gtk.main()
And the result should be like this:
Putting an image as background in Gtk
It is just a window with an HBoxButton as container and a Button in the middle. The button draws normal, but the HButtonBox is drawing its gtk.gdk.window with a pixbuf.
9,079 total views, 1 views today