Making Gtk.Window Transparent?

9,093

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:
9,093

Related videos on Youtube

pilu
Author by

pilu

Updated on September 18, 2022

Comments

  • pilu
    pilu over 1 year

    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()
    
    • Admin
      Admin over 12 years
      If you solved your problem, please add the answer as new answer instead of editing it into your question. Thanks!
  • pilu
    pilu over 11 years
    It does? Which part exactly?
  • AliN
    AliN almost 10 years
    How can we use it to make windows transparent?