Holiday Break

Hello everyone,

As you may know the holiday season is fast approaching (for some of you it may already be here) and I am taking some…well holidays. So I will not be posting here for a few weeks or responding to any of your comments, I will respond to all the comments that I receive when I get back.

I hope that everyone has a happy holidays and gets all the time that they want to learn python!

Also, PyGame 1.8 is going to be released on December 22nd (hopefully) so you may be able to put some 1.8 into your games before Christams eve!

Happy Holidays!


TextWidget – A simple text class for PyGame


All right, this is just a little tutorial about working with text in pygame. Now, this isn’t the only way to work with text, there are many other methods to do this, in fact much of the time you’ll probably end up using images for interactive text. So this is mainly meant to serve as a informative guide to using the text features in PyGame, and how you might want to implement them.

The TextWidget object discussed in this tutorial can be used to make something that looks like this (but you’d probably want to use better looking colours):

The full source and necessary files for this tutorial can be downloaded here.

So, in order to make this easy to use and very reusable we’re going to create a class called TextWidget in a file called The top of the file is full of the standard python initialization:

#! /usr/bin/env python

import pygame


We import pygame and then we set a define TEXT_WIDGET_CLICK , which will be used later on as the event type when the TextWidget is clicked on.

The next thing to do is define the actual class:

class TextWidget(object):

Now the astute among you will recognize that this is a “new-style” python class (i.e. it’s base class is the object class) rather then a classic class. I did this for two reasons:

  1. I wanted to be able to control what happens when people set values in the class. So, for example, if you set the size of the font to be something, I wanted the display to automatically adjust to reflect the new size.
  2. I haven’t really used new-style classes before so I thought I’d try them out.

Note: If you are unfamiliar with properties or new-style classes you might want to give this a read:

Continue reading TextWidget – A simple text class for PyGame

Translating your Python/PyGTK application

In this tutorial we will go over the (very) basic steps that you can use in order to translate (localize or internationalize) your PyGTK application. For this tutorial we are going to use the PyWine application that we have been working with in two previous tutorials:

The full source and necessary files for this tutorial can be downloaded here.

Python translation

Creating our Portable Object Files

In order to translate our text we are going to use the gettext module. The gettext module is basically a wrapper for the GNU gettext utility whose documentation can be found here.

The first step is to edit our python file ( “mark” all of the strings that we want to translate in our python code. To do this we will use the _(“xxx”) standard that most people use to mark strings that need to be translated. Otherwise in situations like the following:

#Get the treeView from the widget Tree
self.wineView = self.wTree.get_widget("wineView")

“wineView” might be thought of as a string to be translated when it clearly should not be. So we need to go through the .py file and wrap each of my English strings in _(). Here are all of the strings that need to be marked in their marked form:

self.sWine = _("Wine")
self.sWinery = _("Winery")
self.sGrape = _("Grape")
self.sYear = _("Year")
self.show_error_dlg(_("Error opening file"))
self.show_error_dlg(_("Error opening file"))
file_dialog = gtk.FileChooserDialog(title=_("Select Project")
filter.set_name(_("pyWine database"))
filter.set_name(_("All files"))

The next step is to generate the Portable Object Template file using the command line gettext utility. If you are using Linux or OS X this should be no problem and gettext will probably already be installed. If you are using Windows the tools are available as .exe files from the GNU gettext FTP or this gettext for Win32 sourceforge project.

Continue reading Translating your Python/PyGTK application

NaNoWriMo almost done!

Just one more note to everyone NaNoWriMo is almost done so I will finally be able to get back to some python code. If anyone is interested I have indeed written the required 50,000 words this month. You can even check out my nice looking bar graph of words per day at my profile.

Whew, it really is tiring writing that much.

I’ve also put up a little Tutorial Index at the request of one reader. Hopefully other people find it helpful as well.

Halfway through NaNoWriMo

Well I’m halfway through NaNoWriMo and everything is going pretty well so far, but today I was surfing the net and looking at some python stuff and I must admit I miss programming in python in my free time.

While writing a novel in one month is fun and all it’s also pretty stressful and sometimes you just want to relax in the coolness of some neat PyGTK or PyGame application, oh and PyGame 1.8 is being released on December for everyone that wants to use PyGame with Python 2.5.

Oh well back to writing, but before I go here are some interesting links:

  1. How to Think Like a Computer Scientist: Learning with Python
  2. PySoy: a high-level python game module
  3. NuFox: a python XUL toolkit written ontop of the Twisted Networking Framework and Nevow.

Well now I really should get back to writing, happy coding!


Hi everyone just a quick post to let you know that I probably won’t be posting much during this month as I have decided to participate in nanowrimo which leaves me almost no time to come up with tutorials.

I as mentioned in the past Satoshi Tanabe over at satolog has translated a few of my tutorials over into Japanese, which is very cool. It also turns out that someone over at has also translated one of my tutorials (Building an application with PyGTK and Glade) into Chinese, again very cool! Thanks a lot for the translations guys.

I’ve also been working on some pyGame in my spare time over the last few months, I’ve been working on a level editor and a widget set:

PyGame Window

Of course that doesn’t show very much but it show show my scrollbars! The problem with building the level editor is that y you always have to keep working on the smallest details (like a scrollable area0 before you can get to the real heart of the problem. Oh well it’s till fun!

Talk to you all soon!

WordPy 0.2 – Using XML to Save and Load Data

All right, so we have our base WordPy application running, so let’s try to extend it a bit more by letting you load and save blog posts to and from an xml file. Please note that this tutorial simply shows one method of saving and loading data using xml, there are many different methods and this method was chosen for its simplicity.

If you are unfamiliar with the first WordPy tutorial you should probably read it fist in order to have a better understanding of some of what happens in this tutorial.

You can download the complete code for this tutorial here.


The first thing we need to-do is open up the wordpy glade project and make some changes:

  1. We’ll start off by adding another item to our VBox in Glade. You can do this by holding down shift and clicking on the WordPy window until you see the GTKVBox come up in the properties window. Then simply change it’s size value from 4 to 5.
  2. In the empty space add a menu bar. Then on the Packing tab of the menu bar’s properties set the position to be zero, so that the menu is at the top of the window.
  3. Then edit the menu so that only the File, Edit, and Help menu’s remain.
  4. Add handlers to each of the files menu items: on_file_new, on_file_open, on_file_save, on_file_save_as

GLADE Window PyWine

The Code

That’s it for editing the GUI, now we have to go and edit the code. The first step is to connect all of the menu events with our code:

dic = {"on_wndMain_destroy" : self.quit
		, "on_btnBold_clicked" : self.on_btnBold_clicked
		, "on_btnItalic_clicked" : self.on_btnItalic_clicked
		, "on_btnLink_clicked" : self.on_btnLink_clicked
		, "on_btnBlockQuote_clicked" : self.on_btnBlockQuote_clicked
		, "on_btnDel_clicked" : self.on_btnDel_clicked
		, "on_btnIns_clicked" : self.on_btnIns_clicked
		, "on_btnImage_clicked" : self.on_btnImage_clicked
		, "on_btnUnorderedList_clicked" : self.on_btnUnorderedList_clicked
		, "on_btnOrderedList_clicked" : self.on_btnOrderedList_clicked
		, "on_btnListItem_clicked" : self.on_btnListItem_clicked
		, "on_btnCode_clicked" : self.on_btnCode_clicked
		, "on_btnMore_clicked" : self.on_btnMore_clicked
		, "on_btnSettings_clicked" : self.on_btnSettings_clicked
		, "on_btnpost_clicked" : self.on_btnpost_clicked
		, "on_file_new" : self.on_file_new
		, "on_file_open" : self.on_file_open
		, "on_file_save" : self.on_file_save
		, "on_file_save_as": self.on_file_save_as}
def on_file_new(self, widget):

def on_file_open(self, widget):

def on_file_save(self, widget):

def on_file_save_as(self. widget):

Continue reading WordPy 0.2 – Using XML to Save and Load Data

IronPython Hello World Tutorial

As some of you may have heard IronPython 1.0 has been released. If you don’t’ know what IronPython is, it’s:

a new implementation of the Python programming language running on .NET. It supports an interactive console with fully dynamic compilation. It is well integrated with the rest of the .NET Framework and makes all .NET libraries easily available to Python programmers, while maintaining full compatibility with the Python language.

The cool thing about IronPthon is that it also works with the Windows Presentation Foundation which will be released when Windows Vista is released and available for Windows XP and Windows Vista.

I thought that this was pretty interesting so I thought I’d try playing with it and write a quick tutorial.


For this tutorial you need the following:

For this tutorial I has assumed that you have installed IronPython to:



Once you have everything installed load up Microsoft Expression Interactive Designer (MEID from now on) or sparkle as it used to be called and close the Welcome Screen. You might notice that MEID is very slow, on my system the program just seems to c-r-a-w-l, but it does allow you to do some pretty neat things.

After it has finished loading and clearing the Welcome screen you should be left with a blank canvas. The canvas is too large for the Hello World program we’re creating so resize the canvas until it has reached a suitable size.

Then with the canvas selected, select the Background item in the Appearance pallet and set the background of the canvas (our window) to something suitable using the colour picker below. I selected a nice dark shade of green.


Continue reading IronPython Hello World Tutorial


Recently I’ve been playing around in PyGame for the past PyWeek and having a blast! Sadly my PyWeek entry was mostly a bust as real life got the better of me, but it reminded my how much I enjoy python and doing simple game programming.

Hopefully I’ll continue on with both my PyGTK and PyGame work! If you like Python and are interested in creating games, I definately think that you should check out PyGame.

Extending our PyGTK Application

In this tutorial we are going to extend our PyWine application to allowing you to edit the items that you have added to the list and save an load the wine lists you create so that you don’t have to keep entering them in all the time.

You can download the complete code for this tutorial here.

If you are not familiar with the PyWine application or with working with Glade and PyGTK I suggest you read my first two tutorial on the subject:

The GUI – Glade

The first thing that we are going to do is open up out PyWine glad project and add an edit button to our toolbar:

  1. Make space for the Edit button by selecting the toolbar and settings its size property to 2.
  2. Add a Toolbar Button in the newly created empty space.
  3. Call the button btnEditWine, set its label to be “Edit Wine”, and its icon to be the stock Edit icon. Next add a handler for the clicked event.
  4. We’re going to change the menu up a little bit, instead of a menu that says Add | Wine, we are going to set the menu up to read Wine | Add and Wine | Edit. Do this just like we did in the previous PyWine tutorial and make the Wine | Edit clicked handler the same function as your btnEditWine button’s clicked event handler.

GLADE Window PyWine

Continue reading Extending our PyGTK Application