user285594
user285594

Reputation:

Python - how to make transparent window with gtk.Window but not with Gtk.Window?

How to make gtk.Window ( Not with import Gtk, Gtk.Window i mean ) transparent? for example doing this same with import gtk, gtk.Window ?

#!/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_size_request(300, 220)
    self.set_border_width(11)

    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)

    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.3)
    cr.set_operator(cairo.OPERATOR_SOURCE)
    cr.paint()
    cr.set_operator(cairo.OPERATOR_OVER)

MyWin()
Gtk.main()

Upvotes: 3

Views: 4454

Answers (1)

Tristan Brindle
Tristan Brindle

Reputation: 16824

(Note: a much easier way is just to use Gtk.Window.set_opacity(), which works with both the old pygtk and new introspection-based bindings. This has the disadvantage that on my system at least, it seems to make the window controls transparent as well as the window contents.)

The following is your code adapted to GTK2 and the old pygtk bindings. As you can see, it's nearly identical. The only real change is that instead of checking for and then setting an RGBA Gdk.Visual, we check for an RGBA gtk.gdk.Colormap (and of course we have to change the draw callback to expose-event, but I'm sure you knew that already :-) )

#!/usr/bin/env python
import cairo
import gtk

class MyWin (gtk.Window):
  def __init__(self):
    super(MyWin, self).__init__()
    self.set_position(gtk.WIN_POS_CENTER)

    self.set_size_request(300, 220)
    self.set_border_width(11)

    self.screen = self.get_screen()
    colormap = self.screen.get_rgba_colormap()
    if (colormap is not None and self.screen.is_composited()):
        print "yay"
        self.set_colormap(colormap)

    self.set_app_paintable(True)
    self.connect("expose-event", self.area_draw)
    self.show_all()

  def area_draw(self, widget, event):
    cr = widget.get_window().cairo_create()
    cr.set_source_rgba(.2, .2, .2, 0.3)
    cr.set_operator(cairo.OPERATOR_SOURCE)
    cr.paint()
    cr.set_operator(cairo.OPERATOR_OVER)
    return False

MyWin()
gtk.main()

Upvotes: 6

Related Questions