Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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()
share|improve this question
    
If you solved your problem, please add the answer as new answer instead of editing it into your question. Thanks! –  htorque Jan 5 '12 at 23:35

1 Answer 1

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()
share|improve this answer
    
Looks like a copy paste from zetcode –  hbdgaf Oct 23 '12 at 15:49
    
zetcode.com/gfx/pycairo/root –  hbdgaf Oct 23 '12 at 15:55
1  
It does? Which part exactly? –  BigWhale Nov 1 '12 at 17:59
    
How can we use it to make windows transparent? –  AliNa May 18 at 9:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.