Question: I'd like to make the background of a Gtk+ window transparent so that only the widgets in the window are visible. I found a few tutorials:

<http://mikehearn.wordpress.com/2006/03/26/gtk-windows-with-alpha-channels/>

<http://macslow.thepimp.net/?p=26>

But they both appear to listen to the "expose" event, and then delegate to Cairo to do the rendering. This means that other widgets added to the window are not rendered (for example, I've tried adding a button to the window as well).

I see that there's a method modify-bg on GtkWidget: <http://library.gnome.org/devel/gtk/stable/GtkWidget.html#gtk-widget-modify-bg>

However, GdkColor does not appear to accept a parameter for transparency: <http://library.gnome.org/devel/gdk/stable/gdk-Colormaps-and-Colors.html>

I've tried the GtkWindow.set\_opacity method as well, but this sets the opacity for the window contents as well, which is not what I want.

I'd appreciate any guidance anyone can provide in this.

Answer: 
For those using golang, here's one using gotk3 ( <https://github.com/gotk3/gotk3> ) :

```
package main

import (
    "github.com/gotk3/gotk3/cairo"
    "github.com/gotk3/gotk3/gdk"
    "github.com/gotk3/gotk3/gtk"
    "log"
)

var alphaSupported = false

func main() {
    gtk.Init(nil)

    win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
    if err != nil {
        log.Fatal("Unable to create window:", err)
    }
    win.SetTitle("Simple Example")
    win.Connect("destroy", func() {
        gtk.MainQuit()
    })

    // Needed for transparency
    win.SetAppPaintable(true)

    win.Connect("screen-changed", func (widget *gtk.Widget, oldScreen *gdk.Screen, userData ...interface{}) {
        screenChanged(widget)
    })
    win.Connect("draw", func (window *gtk.Window, context *cairo.Context) {
        exposeDraw(window, context)
    })

    l, err := gtk.LabelNew("I'm transparent !")
    if err != nil {
        log.Fatal("Unable to create label:", err)
    }

    win.Add(l)
    win.SetDefaultSize(800, 600)

    screenChanged(&win.Widget)

    win.ShowAll()
    gtk.Main()
}

func screenChanged(widget *gtk.Widget) {
    screen, _ := widget.GetScreen()
    visual, _ := screen.GetRGBAVisual()

    if visual != nil {
        alphaSupported = true
    } else {
        println("Alpha not supported")
        alphaSupported = false
    }

    widget.SetVisual(visual)
}

func exposeDraw(w *gtk.Window, ctx *cairo.Context) {
    if alphaSupported {
        ctx.SetSourceRGBA(0.0, 0.0, 0.0, 0.25)
    } else {
        ctx.SetSourceRGB(0.0, 0.0, 0.0)
    }

    ctx.SetOperator(cairo.OPERATOR_SOURCE)
    ctx.Paint()
}

```
With Gtk 3, you can just set the background color, see docs for [override\_background\_color](https://docs.gtk.org/gtk3/method.Widget.override_background_color.html) (deprecated)

```py
window.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(0,0,0,0))
window.set_visual(window.get_screen().get_rgba_visual())

```

or more properly, by using CSS and setting `background-color: rgba(...)` to the main window. Here's a code example for the latter strategy, copied from [here](https://discourse.gnome.org/t/top-level-window-transparency-in-newer-versions-of-gtk/2210):

```py
import gi

gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")

from gi.repository import Gdk
from gi.repository import Gtk

CSS = b"""
#toplevel {
    background-color: rgba(0, 0, 0, 0);
}
"""

style_provider = Gtk.CssProvider()
style_provider.load_from_data(CSS)

Gtk.StyleContext.add_provider_for_screen(
    Gdk.Screen.get_default(),
    style_provider,
    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)

button1 = Gtk.Button(label="Hello, world!")
box = Gtk.Box(spacing=50)
box.pack_start(button1, True, True, 50)

window = Gtk.Window(title="Hello World", name="toplevel")
window.set_visual(window.get_screen().get_rgba_visual())
window.add(box)
window.show_all()
window.connect("destroy", Gtk.main_quit)

Gtk.main()

```