Monday, January 19, 2009

First GUI "Hello World!" using Python

This is exciting!! I always want to learn how to program a GUI application for Linux (GNOME). Finally I made it!

There are many programming language can do a GUI application for Linux. I myself is a Web application programming using ASP and PHP. Sometimes I also program application for Windows using Delphi. The reason I use Python is simply because it is pre-installed. :P~ And lot of people saying that program Python using Glade is very simple and fast especially the graphical interface.

Lets try a simply one first. Open the terminal, and type gedit helloworld.py. Copy and paste the codes below:-

#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk

class Whc:
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", self.destroy)
self.window.set_title("Hello World!")
self.window.set_default_size(200,100);

self.label = gtk.Label("Hello World!")
self.window.add(self.label)
self.label.show()
self.window.show()

def destroy(self, widget, data=None):
gtk.main_quit()

def main(self):
gtk.main()

if __name__ == "__main__":
base = Whc()
base.main()

Save and close it. Then type python helloworld.py and a window with the word "Hello World!" will appear on the top left side of your desktop.



You can also make it become self executable. Just change its mode using chmod command: sudo chmod +x helloworld.py

Then you can type ./helloworld.py to run it.

Python also allows you to compile it into Bytecode (.pyc). Just type python in the terminal and press enter. At the >>> prompt, type the codes below line by line:-

import compiler

compiler.compileFile("helloworld.py")

exit()


Then a file called helloworld.pyc will be generated. Change its mode to executable:

sudo chmod +x helloworld.pyc

and you can run it directly now:

./helloworld.pyc
.


You can also create a simple compiler yourself that can save a lot of time typing the codes in Python everytime. Create a file called pycompiler.py and paste the codes below:-

#!/usr/bin/python

import sys
import compiler

compiler.compileFile(sys.argv[1])


Immediately use it to compile itself:-

python pycompiler.py pycompiler.py

And change it to executable mode.

sudo chmod +x pycompiler.pyc

Next time you can use it to compile the .py file, the usage is:

./pycompiler.pyc FILE...

:)~



Additional links:

GNOME information @ wikipedia
GTK information @ wikipedia
PHP-GTK information @ wikipedia

Tutorial video of Building a GUI applications with Python, GTK and Glade

3 comments:

  1. Fuyoh! Programming is complicated.

    ReplyDelete
  2. Hi, I'm trying to get this to work but I'm getting the error: "Whc has no attribute 'destroy'". Do you know what's wrong?

    ReplyDelete
  3. I had the same error, the indenting is wrong in the above example and has to be corrected by user. "def destroy" and "def main" have to be defined within Whc, and so have to be on the same identation level as __init__ for Whc to find and call these.

    Indenting is an essential part of python code and you need to mention this.

    ReplyDelete