PyLan a GTD todo application written in python and PyGTK – part two

March 11th, 2007

PyLan two

This is part two of the PyLan tutorial series, if you want to follow along with the code in detail, and have not done so already, you should read part one of this series.

In this tutorial I will go over the following items:

This tutorial is organized into the following sections:

  1. The GUI
  2. The todo.Task Object
  3. Adding a todo.Task object
  4. Showing the Calendar window
  5. Editing todo.Category and todo.Task items
  6. Pango markup in the gtk.TreeView
  7. Conclusion

Python GTD pyGTK

You can download the full source to this tutorial here.

Read the rest of this entry »


PyLan a GTD todo application written in python and PyGTK – part one

February 17th, 2007

Introduction

All right, many of you have probably heard of, or read, David Allen’s book Getting Things Done if not it’s a pretty interesting book about how to organize the things in your life.

As I was reading it I thought that a simple todo list could be created using Python and PyGTK pretty easily, so I decided to create the application and write a tutorial surrounding it. Since the actual application is quite large I have decided to break it up into a series of tutorials, each highlighting a few specific tasks. The full goal of this series is to show everything that is required when creating a python/PyGTK based application, for the GUI setup all the way to distribution.

These tutorials assume that you have a basic knowledge of python and PyGTK. If you have not already done so it is recommended that you take a look at some of my previous PyGTK based tutorials as I will be glossing over much of that which is explained there:

PyLan – part one

Python GTD pyGTK

So the application that we will be creating will be called PyLan, pronounced: plan. In brief it will be used to create a tree of tasks that can be organized into categories and subcategories.

You can download the full source to this tutorial here.

Part one of this tutorial will introduce you to the idea of the application and create the basic shell for it. It will outline the following:

Read the rest of this entry »


Gone for the weekend

February 2nd, 2007

Just a quick note for everyone, I will be unable to answer any comments on this blog or posts on the forum all weekend as I will be moving and we all know what happens when the phone companies swear that your high speed will be back up on a certain day.

So who knows, I may be away from cyberspace for a lot longer then weekend…

Creating a GUI using Python, WxWidgets, and wxPython

January 29th, 2007

After putting off this tutorial for as long as I possibly could (I’m not sure why) I have finally decided to buckle down and learn how to use WxWidgets using the WxPython Python bindings.

Installing

Getting WxWidgets and WxPython installed on my Debian Linux computer was as simple as installing the python-wxgtk2.6 Debian package. If you have any problems installing wxPython on your computer I suggest that you follow the instructions in the wxPython FAQ. Or you can follow the Getting Started instructions on the wxPython wiki.

Setting up the Window

Now that you have WxWidgets and WxPython installed it’s time to write a quick wxPython application. Start up your favourite python editor and enter the following:

#!/usr/bin/env python

import wx

if __name__ == "__main__":
	app = wx.App()
	app.MainLoop()

Save the file as wxHello.py, or whatever you would like. You can run the file if you want but you won’t actually see anything. The first thing that you will notice is that we import wx, this will import wxPython into your program.

The next thing that we do is create a wx.App and then call it’s MainLoop function, which starts the main GUI event loop. Here is a description of the wx.App class from the wxPython documentation :

The wx.App class represents the application and is used to:

  • bootstrap the wxPython system and initialize the underlying gui toolkit
  • set and get application-wide properties
  • implement the windowing system main message or event loop, and to dispatch events to window instances
  • etc.

Every application must have a wx.App instance, and all creation of UI objects should be delayed until after the wx.App object has been created in order to ensure that the gui platform and wxWidgets have been fully initialized.

Normally you would derive from this class and implement an OnInit method that creates a frame and then calls self.SetTopWindow(frame).

Notice the last little instruction there:

Normally you would derive from this class and implement an OnInit method that creates a frame and then calls self.SetTopWindow(frame).

So that is our next step, derive a class from wx.App and override the OnInit member:

#!/usr/bin/env python

import wx

class wxHelloApp(wx.App):
	"""The wx.App for the wxHello application"""

	def OnInit(self):
		"""Override OnInit to create our Frame"""
		frame = wx.Frame(None, title="wxHello")
		frame.Show()
		self.SetTopWindow(frame)
		return True

if __name__ == "__main__":
	app = wxHelloApp()
	app.MainLoop()

Now instead of creating a wx.App we create a wxHelloApp that is derived from a wx.App. All that the wxHelloApp does is override the OnInit() function. In that function it creates a wx.Frame (from the wxWidget docs):

A frame is a window whose size and position can (usually) be changed by the user. It usually has thick borders and a title bar, and can optionally contain a menu bar, toolbar and status bar. A frame can contain any window that is not a frame or dialog.

When we create the frame we set its parent to be nothing, and it’s title to be “wxHello”. If you run the wxHello.py file now you will be greeted by the following:

Python wxPython

Read the rest of this entry »

Forums Forums Forums!

January 18th, 2007

So after getting some comments from “mike” I decided to switch the forums over from phpBB to Vanilla, and moved over all of the comments.

Sadly if you are one of the few users who registered on the old forums you will have to register again, sorry for the inconvenience.

Please let me know what you think of the new forums, I’m not sure what it is but for some reason I really like what they’ve done with Vanilla. At first I was a bit put off by the look, but as I used it more I really liked the “flatter” discussion oriented look.

Hopefully I’m done fooling around with the forums and I can actually get back to some python!

So go browse the forums!

Back from holidays!

January 15th, 2007

Hey everyone I’m back from holidays, sorry for everyone that didn’t get a response to their questions while I was away, and thanks to all of the people that answered some of those questions for me.

When I got back I noticed that some tutorials have over 60 comments on them! This is great but it also means that many of the questions and answers are going to get lost as time goes on. I’ve also noticed that some people have questions about their own project or another aspect of python that unrelated to any tutorial I have posted.

As a result I decided to create a forum for this blog where people can ask questions about whatever they want as far as it is python related.

http://www.learningpython.com/forums/

This forum is still in its infancy so please be kind.

Holiday Break

December 19th, 2006

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!

mark.

TextWidget – A simple text class for PyGame

December 13th, 2006

Introduction

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:

#! /usr/bin/env python

import pygame

TEXT_WIDGET_CLICK = pygame.locals.USEREVENT + 1

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:

Read the rest of this entry »

Translating your Python/PyGTK application

December 3rd, 2006

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 (pywine.py)and “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.

Read the rest of this entry »

NaNoWriMo almost done!

November 30th, 2006

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.