Posts Tagged ‘Python’

Python and GTK

February 16th, 2009 Comments off

In my ongoing quest to learn python, I’m writing my first real GUI application since I was traumatized by dealt with swing and Java at university. Since I’m a GNOME user, I’ve gone with GTK. In the past day or two, I have learnt the following;

  1. Use glade for designing your interfaces. Do not pass go, do not attempt to play with direct gtk calls directly, just use glade. It’s far less frustrating, and means you will have something that actually, you know, does something, sooner than later, thus keeping you interested and motivated.
  2. When you dynamically generate a widget, such as another tab, it is imperative that you call show() on this widget, as the code snippet below illustrates.

tabs = self.wTree.get_widget('tabsholder') # tabsholder is the GtkNotebook
foo = gtk.Label("Tab text")
tabs.append_page(foo, gtk.Label("Label text"))

That last line is really important, otherwise the code will run without error, but nothing will actually happen.

Big sigh.

Python and custom Exception classes – Namespaces required

February 9th, 2009 1 comment

Having just spent the last three days fighting with python and it’s handling of custom exceptions, I thought I’d document the problem here for other people who trip over the same problem. Call it document-as-you-fail learning. In hindsight, it’s kind of obvious what’s happened, but it’s just slightly frustrating in the meantime.

Let’s say you’re writing a project called Ripley. In the process of writing this, you spin the reusable code into a nice library, which you then import into your CLI or GUI program like so;

import ripley

Everything works great. Then one day you decide you need some exception code for an internal error. You rock on over to the website, and copy in their example code. Maybe you having something like this in your library.

class RipleyError(Exception):
	def __init__(self, ivalue):
		Exception.__init__( self, ivalue )
		self.value = ivalue
		print "RipleyError Raised!"
	def __str__(self):
		return self.value

This is supposed to be called with something like this;

class session:
	def go():
		raise RipleyError("I'm testing out my error class.")

In your main program in a file called you have something like this;

print "Starting."
session = ripley.session(server)

except RipleyError, ex:
	print "A RipleyError was raised %s" % ex
	print "Failed with non-specific error"
	print "No error raised."
	print "Last thing."

*Deep breath*

You run the program, and the following happens;

edward@entropy:~/working/libmedusa-python$ python ./
RipleyError Raised!
Last thing.

Hang on, the error has been raised, but it’s not being located when you call except on it?

As it turned out, the problem lay in the name space of the two files. The ‘fix’ is to attach the appropriate namespace to the Exception class, like so;

except ripley.RipleyError, ex:
	print "A RipleyError was raised %s" % ex

The problem is that python appears not to be too perturbed that it’s being asked to check for an Exception class that does not in fact, exist.

Update: My preview is horribly horribly broken.

Categories: Tech Tags: ,