Short Example of GSettings Bindings with Python using a Gtk Switch

I made this short example of Gtk.Switch to modify a setting in Gnome with a single line of code that supports bindings and callbacks for the properties of the widget (Including value_changed). As stated at Gnome’s GSettings Site, “Gsettings let’s you bind object properties directly to settings” and “it uses the default Gio mapping functions to map between the settings and property values”.

Although this PyGI example is very short, I thought it was a good idea to post it since it could be useful to someone else since this Gsettings bindings are documented in C and not in Python yet.

The script creates a Gtk Switch with its label both packed into a Gtk Window. The property of Gtk.Switch is “active”, same as Gtk.CheckButton (boolean). The setting I use for this example lets you hide or show the Desktop Icons in Gnome (Not sure if the best one, but you can experiment with the one of your choice); schema “org.gnome.desktop.background”, key “show-desktop-icons”, and  in python you can set GSettingsBindFlags with Gio.SettingsBindFlags. Example:  G_SETTINGS_BIND_DEFAULT  is Gio.SettingsBindFlags.DEFAULT .

If you change the same setting from your terminal or Dconf-Editor while the script is running, you will notice how the switch will update the change by its own at the same time.

#! /usr/bin/python

from gi.repository import Gtk, Gio

class Example(Gtk.Window):

    def __init__(self):
        # Setup the window with title and border width.
        Gtk.Window.__init__(self, type=Gtk.WindowType.TOPLEVEL,
                                  title="Gsettings Switch Example",
        # Create and bind the Gtk Switch with the gsettings schema and its key.
        switch = Gtk.Switch()
        setting ="org.gnome.desktop.background")
        setting.bind("show-desktop-icons", switch, "active", Gio.SettingsBindFlags.DEFAULT)

        # Create the label for the switch
        label = Gtk.Label("Show Icons on the Desktop")

        # Position the Switch and its Label inside a Horizontal child box.
        box =, 40)
        box.pack_start(label, False, False, 10)
        box.pack_end(switch, False, False, 10)

        # Add the child box to the window
        self.connect("destroy", Gtk.main_quit)

if __name__ == "__main__":

Python booleans in gconf with pygtk Message dialog

A small Python Script I use on my VM with GNOME-2 to prevent myself from deleting one of the Desktop Panels by accident. In GNOME 2 you can lock/unlock this panels by editing a GConf schema so you can avoid a wrong selection whenever you launch the right click menu to customize this widgets.

The script creates a Gtk Message Dialog with the options “Yes” or “No”. In my case, I made an executable icon (.desktop file) for this script and placed it into the bottom panel so it can be easily accessed.

You can also modify this script very easy if you want to try it with a different Gconf Setting by replacing the global variable for the schema and/or the string values.

#! /usr/bin/python

import gtk, gconf

KEY = "/apps/panel/global/locked_down"

class Switcher(gtk.MessageDialog):

    def __init__(self):
        client = gconf.client_get_default()
        value  = not ( client.get_bool(KEY) )
        state  = "lock down" if value else "unlock"

        gtk.MessageDialog.__init__(self, None, 0,
                                  "Gnome Panels Lock")
   "Are you sure you want to %s Gnome Panels?" % state)

        if == gtk.RESPONSE_YES:
            client.set_bool(KEY, value)
if __name__ == "__main__":

A simple GUI for Unity-2D Settings (Ubuntu 11.04)

This is a very simple configuration tool I made to save time browsing folders and adding values in Gconf, or writing commands in the terminal to customize certain aspects of the new Unity-2D Desktop.

There’s not much to tweak under the current version of Unity-2D. However, there are certain times -Specially while I’m running Ubuntu on a VM- that I want to set the Launcher to be hidden or vice versa, and Compositing Manager makes it look way better (Near regular Unity) adding window edge shadows, transparency in the Dash and Alt-Tab Switching thumbnail previews.

Download (.deb Installer): Compatible only with 11.04 Natty Narwhal

An updated version for 11.10 oneiric can be found in this post and for 12.04 Precise LTS in this post.

Notes: If you click the compositing manager checkbox, hit “Alt+Tab” to refresh your desktop since by enabled-disabled you get a black screen.
The ‘Always Show’ option for the launcher is tricky to set when it’s hidden (A native bug in Unity-2D caused by the ‘use-strut’ gconf key), you can use “Super” or “Alt+F2” keyboard shortcuts to bring the launcher back in position.

The code:

#! /usr/bin/python

# 2D-Desktop Settings 1.0-5
# Small desktop configuration tool for Unity-2D (11.04)
# Copyright 2011-2012 by Mariano Chavero
# Distributed under de GNU/GPL V3
# Visit <> for more information.

import gtk, gconf

class Settings(gtk.Window):

    def label(self, text):
        box = gtk.HBox()
        lbl = gtk.Label(text)
        lbl.set_alignment(0, 0.3)
        box.pack_start(lbl, False, False, 25)
        return box

    def Checkbox(self, text, key):
        client = gconf.client_get_default()

        box = gtk.HBox()
        bttn = gtk.CheckButton(text)
        bttn.connect("toggled", self.on_Checkbox_toggled, client, key)
        box.pack_start(bttn, False, False, 50)
        return box

    def RadioButton(self, text, key, val, group):
        client = gconf.client_get_default()

        box = gtk.HBox()
        bttn = gtk.RadioButton(group, text)
        bttn.set_active(client.get_int(key) is val)
        bttn.connect("toggled", self.on_RadioButton_toggled, client, key, val)
        box.pack_start(bttn, False, False, 50)
        return box

    def on_Checkbox_toggled(self, widget, client, key):
        client.set_bool(key, widget.get_active())

    def on_RadioButton_toggled(self, widget, client, key, val):
        if not widget.get_active(): return
        client.set_int(key, val)

        # A desktop strut value is set, required by the Unity 2D Launcher
        client.set_bool("/desktop/unity-2d/launcher/use_strut", not bool(val))

    def Separator(self):
        return gtk.HSeparator()

        # Loads the system theme icon of your choice :)
    def theme_load(self, icon_name):
        t = gtk.icon_theme_get_default()
        return t.load_icon(icon_name, 48, 0)

    def __init__(self):
        gtk.Window.__init__(self, type=gtk.WINDOW_TOPLEVEL)
        self.set_title("2D-Desktop Settings")
        self.connect("destroy", lambda q: gtk.main_quit())

        # Creates one group for our existant Radio Buttons
        rbttngroup = gtk.RadioButton(None)

        ### Layout ###
        Container = gtk.VButtonBox()

        Container.add(self.label("<b>Launcher Behaviour</b>"))

        Container.add(self.RadioButton("IntelliHide (Dodge with Windows)",
           "/desktop/unity-2d/launcher/hide_mode", 2, rbttngroup))

        Container.add(self.RadioButton("Auto Hide",
           "/desktop/unity-2d/launcher/hide_mode", 1, rbttngroup))

        Container.add(self.RadioButton("Always Show",
           "/desktop/unity-2d/launcher/hide_mode", 0, rbttngroup))


        Container.add(self.label("<b>Unity 2D Dash</b>"))

        Container.add(self.Checkbox("Show Dash using the Super Key (Windows Logo)",



        Container.add(self.Checkbox("Enable Window Shadows and Transparency",

        Container.add(self.Checkbox("Show Volumes and Devices",

        box = gtk.VBox()
if __name__ == "__main__":