Skip to content

Instantly share code, notes, and snippets.

@pmellati
Created December 3, 2013 16:01
Show Gist options
  • Save pmellati/7771779 to your computer and use it in GitHub Desktop.
Save pmellati/7771779 to your computer and use it in GitHub Desktop.
Shows how to configure a transparency-enabled Webkit WebView in Gtk 3. 2013.
# Transparency in Webkit and Gtk.
from gi.repository import Gtk, Gdk, GLib, WebKit
transparent_window_style_provider = Gtk.CssProvider()
transparent_window_style_provider.load_from_data("""
GtkWindow {
background-color: rgba(100,100,0,0);
background-image: none;
}""")
# If the following two lines are not run before everything else, extremely weird things happen!
GLib.threads_init()
Gdk.threads_init()
w = Gtk.Window(Gtk.WindowType.TOPLEVEL, title='Transparent Webkit and Gtk?')
w.connect('delete-event', Gtk.main_quit)
w.set_visual(w.get_screen().get_rgba_visual())
all_state_flags = [Gtk.StateFlags.ACTIVE, Gtk.StateFlags.BACKDROP, Gtk.StateFlags.DIR_LTR, Gtk.StateFlags.DIR_RTL, Gtk.StateFlags.FOCUSED, Gtk.StateFlags.INCONSISTENT, Gtk.StateFlags.INSENSITIVE, Gtk.StateFlags.NORMAL, Gtk.StateFlags.PRELIGHT, Gtk.StateFlags.SELECTED]
for flag in all_state_flags:
w.override_background_color(flag, Gdk.RGBA(0,0,0,0))
w.get_style_context().add_provider(transparent_window_style_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
wv = WebKit.WebView()
wv.set_transparent(True)
for flag in all_state_flags:
wv.override_background_color(flag, Gdk.RGBA(0,0,0,0))
wv.load_html_string(
'''
<html>
<head>
<style type="text/css">
body {
background-color: rgba(0,0,100,0.3);
}
</style>
</head>
<body>
Transparent gtk-webkit background!
</body>
</html>''', '')
w.add(wv)
w.show_all()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment