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 TextWidget.py. The top of the file is full of the standard python initialization:

[code lang=”python”]
#! /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:

[code lang=”python”]
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

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:

[code lang=”python”]
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}

[code lang=”python”]
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

Creating a GUI in Python using Tkinter

Up until now we’ve focused on python applications that only run in the command line. For a lot of tasks this is great, but in order to be really impressive we’re going to want to create a GUI for some of our python applications.

There are a lot of python GUI toolkits out there, but for our first application we are going to use Tkinter, which is considered somewhat of a standard right now and is installed when pyhon is installed. Another nice thing about the Tkinter GUI is that it is cross platform and offers a native look and feel on *nix, Windows, and OS X.

Tkinter Example One

So lets start off with a simply “Hello World” GUI app:

[code lang=”python”]
#! /usr/bin/env python
from Tkinter import *

class GUIFramework(Frame):
“””This is the GUI”””

def __init__(self,master=None):

“””Initialize the base class”””

“””Display the main window”””

“””Create the Text”””
self.HelloLabel = Label(master, text=”Hello World!”)

if __name__ == “__main__”:
guiFrame = GUIFramework()

Running this on OS X results in the following:

hello world python gui

And running it in Windows Xp gives you:

hello world python gui
Continue reading Creating a GUI in Python using Tkinter


If you are familiar with Object Oriented programming you probably already know what classes are. If you are unfamiliar with the idea of classes in programming I suggest you give the class topic a read over at wikipedia, it should familiarize you with the idea of a class.

Once you know what a class is you should read the tutorial over a python.org’s section on classes in Python. I’ll hi-light some of the main points here, but for an in-depth understanding of the way that classes work in Python, it’s a must read.

First off we’ll take a look at the syntax of a class:

[code lang=”python”]
class classname:
Continue reading Classes