Archive | Python RSS feed for this section

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.

Advertisements

The Impatient Developer’s Guide to Python

27 Jun

If you don’t want to go through those 200 pages for just learning the basics of Python but still want to learn Python,you’re at the right place.In this post I’ll be telling you about how to code in Python.I already assume that you at least know the basics of computer programming or have programmed before.So let’s dive in.

First of all,you should understand that Python doesn’t have ‘{‘ and ‘}’ to represent start and end respectively of any structural block so we use indentations to tell the python’s interpreter about start and end of any structural block like functions,if else statements,loops etc.To make things clear i’m gonna show you the example

Example 1: C’s syntax:-  

void sum(){

<programs body>

}

while in Python the above function will be written as follows

Python’s syntax:-

def sum():

<indentation><program body>

In the above code “<indentation>” denotes that you’ve to indent the program’s body,you don’t have to type the word “<indentation>” though,just press the tab key to indent the code.Now we can proceed further.I’d like to tell you about python’s shell.It’s an interactive place where you can write your commands and have them executed.The symbol “>>>” denotes the Python shell’s prompt.It’s “interactive” because it shows the result immediately after executing the instruction.If you type 2+3 at the “>>>” prompt then you’ll get 5 as output.

>>>2+3

5

>>>(2+5)/7

7

Diving In: In order to practice Python,you should start Python’s Interactive Shell.If you’re using Ubuntu or Mac OS X then you must be having Python installed already.If you’re using Windows you can easily get the installer from here.On Ubuntu simply type “python” in the Terminal without quotes.You will get something like this

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Showing output,printing to the console: For this use “print” command.

Example:

>>>print(“Hello World”)

Hello World

Defining Variables in Python: For defining simple variables to store integer,float and string like values you can write

>>>var=1

>>>var=”Rajat”

>>>var=1.0

There’s no need of doing something like “int var” or “float var” like other programming languages.

Introducing Dictionary,Lists and Tuples: These are unique data types and are very powerful.I’m gonna show you how you can use them,one by one.

Dictionary: You can define a dictionary type variable as follows:

>>>dictvar={“name”:”rajat”,”age”:23}

Now you can visualize it as one item list with associated values.In the above case if “name” is an item then “rajat” is it’s value.Now if you want to access value of any item,you can do it as follows

>>>dictvar[“name”]

‘rajat’

To add new entry,type:

>>>dictvar[“gender”]=”female”

List: You can define a list type variable in the following way:

>>>listvar=[1,2,”rajat”,3,”saxena”]

Now to access the elements from the above list you can do something like this

>>>listvar[0]

1

To add new element to the above list,type

>>>listvar.append(“newvalue”)

Tuple: You can define a tuple type variable as follows:

>>>t=(“1″,”2″,”rajat”)

Defining Functions: Creating new functions in Python is very easy.Following is the syntax of a function in Python

>>>def <function name>(<arguments list>):

…            <program’s body>

Here is the small example of a function:

>>>def sayhello():

…           print(“Hello”)

Now calling any function is very easy too:

>>>sayhello()

Hello

 Defining Class: As Python is an object oriented high level programming language you can obviously create classes in Python.Here’s how you can do this:

>>>class <classname>(<SuperClass,if any>):

…          def  __init__(self):

…                  <init’s body>

…          <rest to the body of this class>

Example of a class:

>>>class myclass:

…              def __init__(self):

…                     print(“Hello”)

Now to make the instance of above class,type:

>>>m=myclass()

Hello

Looping: Easiest way to doing this is as follows

>>>for i in range(5):

…          print(str(i))

0

1

2

3

4

In the above example you must have noticed two new things “str” and “range”,so here’s the explanation.The in built function str() converts integer value to string and the function range() generates a list containing the integer elements from 0 to argument-1 where argument is what we pass to the range() function.Have a look at it

>>>range(8)

[0,1,2,3,4,5,6,7]

I think if you put the above Python skills under your belt,then you can certainly make simple programs using Python.Under any difficulty feel free to write to me,I’ll try resolve your issue with Python

Image

Which Python book to follow?

11 Jun

If you’ve read my previous post Why learn Python? and you are really impressed to see what python can do for you,you might be wanting to try your hands on python.For this you’ll definitely need a good book of python language which can guide you during the times when you’re stuck somewhere in between.Believe me,a good book can be a great companion and an asset to the programmer.So,I’m gonna review 3 Python books for you to choose from.

Book #1 : Head First Python

hfp

Head First Python Cover

If you haven’t programmed before or you don’t want to put too much effort in learning Python,then this book is for you. But it is to be noted that this book is very lengthy and you’ll find many things in the book are redundant.Also,If you are an experienced programmer in Python then this book is not for you.The area where this book scores maximum is that it teaches the user how to build realistic applications using modern technologies rather than explaining Python’s Standard API(Application Programming Interface).

If you use this book,you will be able to:-

  • Write CGI scripts using Python
  • Create apps with Google App Engine which can be used with different social networks
  • Create Android apps
  • Database Management using Python
Book #2 : Programming with Python 3
pwp3
Programming with Python 3
Cover
Huff!Believe me this book is python newbie’s nightmare.If you are a newbie and don’t have any knowledge about python then this book is not for you.On the other hand if you’re an experienced python programmer who wants to increase his/her command over the language,then this book can prove beneficial to you.To tell the truth,I picked up this book in the first place for learning python but the book didn’t really help me.Besides regular stuffs this book will provide you some great topics.
Salient features of this book are as follows:-
  • The chapter on Regular Expressions is really nice
  • The book tells you about Networking using Python
  • The book teaches advance techniques like extracting meta data from files
  • The book teaches about debugging,testing and profiling
Book #3 : Python for Unix and Linux System Administrators
pfual
Python for Unix and Linux 
System Administrators Cover
To be frank with you,I’ve not read this book yet but I’ve had a look at it.So I can at least tell you what this book contains.As the name say it all,this book is exclusively for users who use Linux and Unix.
This book has chapters on following topic:-
  • IPython
  • SNMP
  • OS Soup
  • Package Management
  • Using easy_install
  • Processes and Concurrency
Attention : It is to be noted that all of the above books don’t have good chapters on “Building GUIs”.So,if you want to write GUI applications in Python then you have to use other libraries like PyGTK,PyQT etc.Just keep in touch,I’ll be reviewing GUI toolkits for Python soon

Why learn Python?

8 Jun

Whether you are a programmer or not,knowing any computer language is not a loss at all.

A good programming should has following qualities:-
-highly reliable
-fast enough
-easy to learn

Python has it all.It is designed to be easy and fast.Also the programs written in Python can be easily understood by human beings.

If you learn this language,you’d be able to:-
-write programs which can control your operating systems,it is the core programming language on Ubuntu.
-write applications for Android platform.
-write CGI scripts.
-write utility programs like Image Viewer.
-develop applications for social platforms like Facebook.
-create database applications without even installing any dbms as Python has in-built SQLite database system.
-run your own webserver and show off your contents to the world

Also there is nothing which will stop you from using Python.There are python-bindings(libraries) for everything.For example:-
-want to develop high quality GUIs?Use Pyclutter
-want to embed a web browser into your app?Use gtkmozembed
-want to process XML?Use lxml

I think all of the above would be enough to convince you to use Python for your programming purposes.

Facebook Indicator

7 Jun

I’ve developed a simple python script which can show your fb notifications using Ubuntu’s in-built OSD system.

You can check it out at: https://github.com/raynesax/findicate