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",
                                  resizable=False,
                                  border_width=10)
        # Create and bind the Gtk Switch with the gsettings schema and its key.
        switch = Gtk.Switch()
        setting = Gio.Settings.new("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 = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 40)
        box.pack_start(label, False, False, 10)
        box.pack_end(switch, False, False, 10)

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

if __name__ == "__main__":
    Example()

Easily create Application Launchers in Gnome-3 using the Terminal

This is just an easy, quick and short way to create Application Launchers (.desktop files) in GNOME using a pre-installed text editor in most UNIX and Linux systems called Pico.

Step 1: Create the file in your preferred directory (Mine is my home folder) then hit Enter.

Step2: Edit the file.

As you can see in the screenshot, the format is very short and simple. In the head of the file you have to type [Desktop Entry] followed by four parameters: The first for your type of file which is Application, the second is the name for your launcher, the third is the name (If is a bin file) or path to your executable file, and the fourth is the icon which also can be set with its name if you are going to use one from the system. Hit ctrl-x, then ‘y’ to save the file.

Step 3: Make the file executable using chmod +x

Done! :)


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,
                                   gtk.MESSAGE_QUESTION,
                                   gtk.BUTTONS_YES_NO,
                                  "Gnome Panels Lock")
        self.format_secondary_text(
   "Are you sure you want to %s Gnome Panels?" % state)

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


Small Configuration Tool for Ubuntu Unity-2D 12.04 LTS Precise Pangolin (Updated Apr 11th 2012)

An update to the small desktop tool for Unity 2D to work with Ubuntu 12.04 LTS Precise Pangolin.

Download: For 12.04 Precise Pangolin only

QuickLists

Features:

-> Dash and Icon right click Shortcuts (QuickLists) for launcher settings and compositing manager’s extra effects, which brings window edge shadows and “alt-tab thumbnail previews” very handy for multi-tasking.

Unity 2D Desktop Tool

-> Updated Gtk3 GUI: Few available options to tweak Unity 2D not included in ‘System Settings’ for Launcher and Dash, Applications Lenses and a checkbox to enable OpenGL for Graphics Rendering as long as your system has a supported graphics card. If so, you might not be able to see any differences in your desktop regarding its visual components but just an overall graphics performance improvement.
Note: The Compositing Manager checkbox available in previous versions of the GUI has been removed. The switcher shortcut in the dash or quicklist in the icon handles this features and brings a pop up message dialog which helps to refresh the desktop so window shadows are instantly enabled.


Small Desktop Settings Tool for Ubuntu Unity-2D 11.10 (Updated Apr 3rd 2012)

This is an update to the small tool I made for Unity 2D to save time modifying its settings from the terminal or dconf-editor.

An updated Version for 12.04 LTS can be found in this post

Download: (Requires an up to date Ubuntu 11.10 System)

Features:

-> Icon right click Shortcuts (QuickLists) for launcher settings and compositing manager, which brings window edge shadows and alt-tab thumbnail previews, very handy for multi-tasking.

The same shortcuts can be found by searching for them in the dash.

-> An updated (From PyGtk) Gtk+3 GUI with the two available Unity-2D preferences for dash and launcher and a few extras.

Notes: Window Shadows will not show until you click everywhere in your desktop to refresh the compositing effect once enabled via the checkbox icon in the GUI. (The Quicklist and Dash shortcuts will launch a message dialog that will refresh the compositing effects automatically.)
The ‘Always Show’ option for the launcher will not work while the launcher is hidden (A native bug in Unity-2D caused by the ‘use-strut’ gsettings key), you can use “Super” or “Alt+F2″ keyboard shortcuts to force the launcher to show.


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 <http://www.gnu.org/licenses/> for more information.

import gtk, gconf

class Settings(gtk.Window):

    def label(self, text):
        box = gtk.HBox()
        lbl = gtk.Label(text)
        lbl.set_use_markup(True)
        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.set_active(client.get_bool(key))
        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_resizable(False)
        self.set_border_width(10)
        self.set_title("2D-Desktop Settings")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_icon(self.theme_load("gnome-fs-desktop"))
        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.Separator())

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

        Container.add(self.Checkbox("Show Dash using the Super Key (Windows Logo)",
           "/desktop/unity-2d/launcher/super_key_enable"))

        Container.add(self.Separator())

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

        Container.add(self.Checkbox("Enable Window Shadows and Transparency",
           "/apps/metacity/general/compositing_manager"))

        Container.add(self.Checkbox("Show Volumes and Devices",
           "/apps/nautilus/desktop/volumes_visible"))

        box = gtk.VBox()
        box.add(Container)
        self.add(box)
        self.show_all()
        gtk.main()
if __name__ == "__main__":
    Settings()

Follow

Get every new post delivered to your Inbox.