Tag Archives: development

Developing desktop apps with Python-Clutter

13 Jul

If you want to develop some practical applications for Linux platforms then you should know at least one gui toolkit for creating user interfaces.So here I’ll show you how to employ one such library which is known as PyClutter(python-clutter).It is used to create windowed applications.Also obviously Python programming language will be used.So let’s dive in.

Installing PyClutter:

If you are using Ubuntu then you can fire up “Ubuntu Software Center” and search for “python-clutter”.Otherwise you can grab the library from here .

Using PyClutter:

First of all we have to set up the base on which our whole interface will be built.For this we will create a basic blank window.Here is the code to do that.

import clutter

def basic():

……..stage=clutter.Stage()

……..stage.show_all()

……..clutter.main()

basic()

Save the above code in a file(“…….” are used just to show indentation of code.So delete them,they will result in syntax error if not removed) and run it like any other python script.You’ll see a blank window on executing the script.Breakdown of the above code is as follows

first of all we create an empty stage on which we will draw various elements

next,we call “show_all()” on Stage’s instance which will make every element inside the window visible.Also it’ll make the window visible.

finally we call the “clutter.main()” so that we can see the output.

Now for building real apps we need to know more about this library.

Setting color:

There are two methods to do that

Method1: 

stage.set_color(clutter.color_parse(“Red”))

The above code will set the background color of the window to blue.

Method 2:

red=clutter.Color(255,0,0,255)

stage.set_color(red)

In the above code we first make the color using rgba technique.If you’ve any idea about html then you might be familiar with this method.Finally we set the color of the window.Any other element’s(like rectangles,texts etc.) color can be set similarly.

Setting size:

stage.set_size(500,400)

The window now will be 500×400.

Setting window title:

stage.set_title(“Basic window”)

Writing Text:

tex=clutter.Text()

tex.set_text(“Hello There!”)

tex.set_position(100,150)

tex.set_color(clutter.color_from_string(“white”))

The above code is self explanatory.Note that the text element should be added to the main window we created.For this,use

stage.add(tex)

When all of the above code will be put together,you’ll get the window like this

Resulting Window

Output

I’ll write more about PyClutter and how to use it for complex apps.Just stay in touch with my site.