Ubuntu – Making Gtk.Window Transparent

cairogtk3pythontransparencywindow

Here's my code and I simply cannot figure out why my top level window doesn't become transparent.

xwininfo says that windows has 32 Bit depth. I am running Ubuntu 11.10 and if I call .set_opacity(.5) on the window itself, it will become transparent. But all the content of the window too. I'd like a transparent background, but widgets should stay opaque.

I looked at the notify-osd code and they are calling set_opacity on the main window. Yet, I cannot figure out why other widgets on the osd stay opaque. Here's the code:

#!/usr/bin/env python

from gi.repository import Gtk, Gdk

class MyWin (Gtk.Window):
    def __init__(self):
        super(MyWin, self).__init__()
        self.set_position(Gtk.WindowPosition.CENTER)
        self.set_border_width(30)
        self.screen = self.get_screen()
        self.visual = self.screen.get_rgba_visual()
        if self.visual != None and self.screen.is_composited():
            print "yay"
            self.set_visual(self.visual)

        box = Gtk.Box()
        btn1 = Gtk.Button(label="foo")
        box.add(btn1)
        self.add(box)

        self.set_app_paintable(True)
        self.connect("draw", self.area_draw)
        self.show_all()

    def area_draw(self, widget, cr):
        cr.set_source_rgba(0, 0, 0, .3)
        cr.paint()

#    def screen_changed(self, widget, cr):
#        self.screen = widget.get_screen()
#        self.visual = self.screen.get_rgba_visual()
#        if self.visual != None and self.screen.is_composited():
#            self.set_visual(self.visual)

MyWin()
Gtk.main()

Best Answer

I solved the problem, here's the code that works. The important part is setting the correct operator for operation that is being performed on the surface.

#!/usr/bin/env python

import cairo
from gi.repository import Gtk, Gdk

class MyWin (Gtk.Window):
    def __init__(self):
        super(MyWin, self).__init__()
        self.set_position(Gtk.WindowPosition.CENTER)
        self.set_border_width(30)
        self.screen = self.get_screen()
        self.visual = self.screen.get_rgba_visual()
        if self.visual != None and self.screen.is_composited():
            print "yay"
            self.set_visual(self.visual)

        box = Gtk.Box()
        btn1 = Gtk.Button(label="foo")
        box.add(btn1)
        self.add(box)

        self.set_app_paintable(True)
        self.connect("draw", self.area_draw)
        self.show_all()

    def area_draw(self, widget, cr):
        cr.set_source_rgba(.2, .2, .2, 0.9)
        cr.set_operator(cairo.OPERATOR_SOURCE)
        cr.paint()
        cr.set_operator(cairo.OPERATOR_OVER)

MyWin()
Gtk.main()
Related Question