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

WordPy offline blogging tool

Topics covered in this tutorial:

  • Glade
  • PyGTK
  • gtk.FileChooserDialog
  • gtk.FileFilter
  • gtk.TextView
  • gtk.TextBuffer
  • gtk.TextMark
  • gtk.MessageDialog

You can download the full source for this tutorial here.

One of the things that I found the other day while I was surfing the web looking for information on Python was this WordPress Python library. Since I use WordPress for this site I thought that I’d play with it a bit. The result is this tutorial about using the WordPress library, PyGTK, and Glade.

The first think to do is download the library, extract it form the archive, and install it. Enter the following on the command line in the directory you downloaded the library to in order to install it:

python setup.py install

Now you have the library installed. The next step is to create the GUI that we will use to interact with the library. The GUI will be created using Glade, if you are new to Glade or PyGTK you might want to read over my two turorials on the subject: Creating a GUI using PyGTK and Glade and Building an Application with PyGTK and Glade.

Continue reading WordPy offline blogging tool

Writing a Custom Widget Using PyGTK

One of the things that I wanted to add to my simple PyWine application was an easy way for people to rate their wine. There were lots of different ways to do it but since I was looking for a tutorial to write I decided that I wanted to do it the way that you rate songs in iTunes. If you’ve never used iTunes before, you can rate songs on a sliding scale from zero to five using stars. It basically functions like a slider or a Horizontal Scale except that when drawing it’s not a line, it’s a row of stars.

Python PyGTK Windows

The full source for this tutorial can be downloaded here.

The three the most useful links that I found on this subject were: A song for the lovers, the writing a widget turotial on the PyGTK website, and the widget.py example in the PyGTK cvs.

The skeleton of the following code will be mostly based off of the widget.py example, but since this example will try to accomplish a bit more there will be some extra code. In order to understand this tutorial better I suggest you give widget.py a couple of reads.

The starting point is a file names starhscale.py which starts off with some rather standard python stuff:

[code lang=”python”]
#!/usr/bin/env python

import gtk
import gobject
from gtk import gdk
raise SystemExit

import pygtk
if gtk.pygtk_version < (2, 0): print "PyGtk 2.0 or later required for this widget" raise SystemExit [/code]
Continue reading Writing a Custom Widget Using PyGTK

Building an Application with PyGTK and Glade

After working with PyGTK and Glade for my first tutorial, I decided to write another more complex tutorial but I couldn’t think of a topic (Note: If you have any suggestions for any tutorials or posts I’d love to hear them) so I decided to work on creating a simple application and hopefully useful topics would arise from there.

The idea I came up with (which will hopefully be simple) is to create a program that will allow me to keep track of the different types of wine that I drink and how much I like them. It’s something I’ve wanted to write for a while so I thought it would be good to combine learning PyGTK and doing it.

I’m going to call the project PyWine. The full source and Glade file for this tutorial can be found here

The first thing to do is start a new Glade project in the projects directory called PyWine. Create a new Window, call it “mainWindow,” and set the title to be “PyWine”. Then add a handler to the destroy signal just like in the first tutorial.

Next I add a Vertical Box to the Window with 4 rows, (from top to bottom) one row for a Menu bar, one row for a Toolbar, one row for a Tree or List View, and the final row for a Status Bar. Name the Tree View “wineView”

Python Glade

Continue reading Building an Application with PyGTK and Glade

Creating a Game in Python Using PyGame – Part 3 – Adding the bad guys

All right in this section of the tutorial we are going to start adding the bad guys. If you are familiar with the changes that we made in part two it should be pretty clear to you how are are going to create these bad guys. If you haven’t already you should check out part one and part two. If you would like the full source and all of the images you can get it here.

First we are going have to create a new class based on our basicSprite class. I created a new file called basicMonster.py and inside of that I created a class called Monster:

[code lang=”python”]
class Monster(basicSprite.Sprite):

For now we’re not going to go too crazy in our enemy AI, we’re just going to get them to choose a random direction to move and move in that direction until they hit a wall. We’ll also add in another option where after a certain amount of moves the monster will choose another random direction. This is just to add a little but more randomness into the Monster, it’s not really what we’d like to use at the end of the day but it works out all right.

Now we’ll take a look at the Monsters __init__ function:

[code lang=”python”]
def __init__(self, centerPoint, image, scared_image=None):

basicSprite.Sprite.__init__(self, centerPoint, image)
“””Save the original rect”””
self.original_rect = pygame.Rect(self.rect)
self.normal_image = image
if scared_image !=None:
self.scared_image = scared_image
self.scared_image = image

self.scared = False
“””Initialize the direction”””
self.direction = random.randint(1,4)
self.dist = 3
self.moves = random.randint(100,200)
self.moveCount = 0;

Continue reading Creating a Game in Python Using PyGame – Part 3 – Adding the bad guys

Using Menus in Tkinter

This is a quick post on how to use menu’s in Tkinter, if you are unfamiliar with Tkinter you might want to check out some of my other Tkinter related posts.

We are going to start of with a basic python Tkinter app:

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

class App(Frame):

def __init__(self, master):

“””Initialise the base class”””

“””Set the Window Title”””
self.master.title(“TkInter Menus”)
“””Display the main window
with a little bit of padding”””
self.grid(padx=15, pady=15,sticky=N+S+E+W)

if __name__ == “__main__”:
root = Tk()
app = App(root)

Python Tkinter WindowWhat this gives us is basically a plain old blank Tkinter window, not too exciting. The next thing we are going to do is add create our root menu, before going any further you might want to read the excellent information about menu widgets in PythonWare’s Tkinter introduction. You don’t have to but it’s an excellent source of information.

Continue reading Using Menus in Tkinter

Creating a Game in Python Using PyGame – Part Two – Creating a level

Python RSS ReaderAll right in Part one we actually created a semi-working almost-game, in part two we’re going to go a bit further, in part two we’re going to add the walls that will make help make PyMan (our python based PacMan clone) an actual game.

Note: Part one has been mistakenly half-deleted by me, so it is not fully available at this time. I am working to re-write it so it should be up soon.

Python RSS ReaderNote: I also changed the images that I used in Part one so now we have a new smaller snake.

You can download the full source for Part Two here.

What we are going to do is base some of our level code on a great PyGame tutorial over at DevShed specifically the idea of the level layout. This is something that has always puzzled me about games, how they create their levels? I’ve read some articles and beginnings of books but I’ve never really be able to understand it, but the multidimensional array approach discussed in the DevShed article is nothing if not simple.

So the idea is that our game world, or game board, will basically be a grid of 24×24 pixel items, and we will have a multidimensional array or list that we will used to describe it.

So for example lets say we have a grid like this:

0 0 0 0 0
1 1 1 1 1
1 0 0 0 1
1 1 1 1 1
0 0 0 0 0

Now that grid may result in the following:

PyGame Window

Where 0 is nothing and where 1 is a blue square. We could then add 2’s if we wanted to and make those red squares, or anything else that we wanted. So that is basically how our levels are going to be defined.

Continue reading Creating a Game in Python Using PyGame – Part Two – Creating a level

Creating a Game in Python Using PyGame – Part One


In order to follow along with this tutorial you will need Python 2.4 and PyGame 1.7.0+ installed. Since I’m doing this all on a Mac and Python 2.4.2 is not available from MacPython or any other site as a disk image I decided to install from source.

If you are try to build Python on a Mac as well you should probably install into the /Library/Frameworks path as explained by this site.

Basically once you have downloaded the source from python.org unpack the tar ball as follows:

tar -zxvf Python-2.4.2.tgz

Once that has finished change to the Python-2.4.2 directory that was just created and install it by issuing the following commands:

./configure --enable-framework
sudo make frameworkinstall

For Windows or Linux you should be able to install easily given the instructions on the Python site.

This will install Python in the /Library/Frameworks path, and create a symlink to the python executable in /use/local/bin, which is not part of the PATH environment variable on new OS X builds, so you will probably want to add it:

export PATH=/usr/local/bin:$PATH

Part One

The full source of this tutorial can be downloaded here.

Python SnakeSo let’s actually start creating this game using PyGame. For our snake image in the game I’m going to use the snake to the left for now. As you can see I’m not artist, but I was able to install the gimp on my Mac and get that image to a point that I think is relatively acceptable. If anyone out there has any graphics skills and can whip me up a better looking python in a 64×64 png I’d gladly use it!

So the first thing we are going to do is create a new PyDev project in Eclipse. I’m going to use Python 2.4 for this project because it is the version that is compatible with my PyGame Installation.

Python foldersThen I’m going to create a new file called PyMan.py, this will be the main file of our game for now. The architecture may change as I go through this and discover better ways to use the files in my projects but for now this will work as a main file. In the directory that I create my project I will also create a subfolder entitled “data” and in that subfolder I will have another folder entitled “images” where I will store the above snake.png image.

Note: A lot of the information in this post was taken from the Pete Shinner’s great Line by Line Chimp and Python Pygame Introduction tutorials.
Continue reading Creating a Game in Python Using PyGame – Part One

Creating a GUI in Python using Tkinter – Part 2

This post is going to build off of Creating a GUI in Python using Tkinter which introduces the basic elements needed to create a GUI using TKinter. This post will take that introduction a bit further and build a more complex GUI application and use some more widgets.

The GUI that we are going to start creating is the GUI that we will eventually use for the RSS reader that I am creating.

Not much having to do with the RSS reader will happen in this post since we will just be creating the GUI shell, but it will help to explain some of the variable names.

GuiLayoutWe are going to start off with a simple shell that will hopefully resemble the following layout. We will have RSS sites on the left, then the selected sites RSS story titles on the right, and finally the selected stories text on the bottom.

To create this GUI we will be using the following widgets:

Continue reading Creating a GUI in Python using Tkinter – Part 2

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