Archive for posts tagged ‘python’

Python wedding vows

Kevin Day, February 24th, 2008

I was inspired by this valentine’s day card, and a quote by Blake Ross in Founders at Work. So this wasn’t my original idea, but I still consider my implementation to be heartfelt and sincere.

I asked Kristen if I could use these vows in the wedding, but she didn’t seem thrilled about it. I’ll have to work on her some more.

conditions = ["better", "worse", "richer", "poorer", "sickness", "health"]
while(True):
    for time in [time for time in life if time in conditions]:
        have = True
        hold = True
        love = True
        cherish = True
    if death:
        break

PyGTK Hello World program for images

Kevin Day, February 2nd, 2008

I’m learning PyGTK to create GUIs with python, but it’s been slow going. My current application primarily uses images, but most of the beginning tutorials for PyGTK use buttons for their “hello world” applications. The PyGTK tutorial is thorough, but I find it easier to work with lots of smaller examples that aren’t as in-depth.

Below is my simple “hello world” application for working with images. When it runs, it displays the first image of a directory. When you click on the image, the next image in the directory is displayed. I kept it basic so that it would be easy to pick up on the PyGTK stuff without getting caught up in other details.

Here’s the code, and below it is a description:

#!/usr/bin/env python
# hello_world_image.py

import pygtk
pygtk.require('2.0')
import gtk

class image:
    def __init__(self):
        self.window = gtk.Window()
        self.event_box = gtk.EventBox()
        self.image = gtk.Image()
        self.image_num = 0
        self.image.set_from_file(self.make_image_name(self.image_num))
        self.event_box.connect("button_release_event", self.change_image)
        self.event_box.add(self.image)
        self.window.add(self.event_box)
        self.window.show_all()

    def make_image_name(self, image_number):
        return "photos/img_" + str(image_number) + ".jpg"

    def change_image(self, widget, event):
        self.image_num += 1
        self.image.set_from_file(self.make_image_name(self.image_num))
        return True

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    image()
    main()

To add an image to a PyGTK application, just create an Image widget with gtk.Image() and tell it where to find the image with the set_from_file method.

The trick is that Image widgets do not react to events (button clicks, key strokes, etc.) so you have to put the Image widget in an Event Box widget. The event box doesn’t take up space in the GUI; it’s just an invisible container that will emit a signal when an event occurs. In this application, the change_image callback function is bound to the event box, but it still operates directly on the Image widget.

For this program to work it assumes that the images are in a folder named “photos” which is in the same directory the application is running in. Also, the images have to be named sequentially: img_0, img_1, img_2, etc. This is how I plan to use the program, but you could just as easily read the contents of the folder into a list and use that for the image names.

Here’s my directory of images:
Image Directory

And here’s my working program:
Hello World w Images

If you’re wondering, it’s not my dog. It’s just an image I found on Flickr.

Hope this little program helps other people starting off with PyGTK.

Obligatory “got a new toy” post

Kevin Day, December 29th, 2007

For Christmas I received a Nokia N800 internet tablet, which was at the top of my wish list. So far it’s been great to use. Couldn’t be happier.

My initial ideas for applications to write for it are (in increasing order of complexity):

  1. Digital picture frame that pulls images from Flickr based on tags supplied by the user.
  2. Fantasy football live stats calculator
  3. Grocery store helper that suggests what food to buy based on the food I have in my apartment, cost, and nutritional value.

I’m just starting with the Flickr-frame application for my desktop, and once it’s finished then I’ll try porting it to the N800. At the moment I’m using PyGTK for the GUI.

Python in Ohio

Kevin Day, November 18th, 2007

A recent thread on reddit about cool Google Trends had me looking for interesting trends myself.  I’m not sure if Google Trends always had this feature, but I just noticed that you can filter the results by state. 

When I ran a search of programming languages within Ohio only, I found that “python” hasn’t been a significant search term in Ohio until 2005, where it is most popular in the Columbus area. 

Programming Languages - Ohio

However, I think most python searches are for the actual snake, based on this search for snakes:

Reptiles - Ohio

I guess python has a long way to go to before becoming popular in Ohio.

Python Magazine

Kevin Day, October 10th, 2007

The Python Magazine released its first issue this month, and one of their main articles tells how to create custom widgets for PyGTK. Awesome! All of the articles look pretty good and have a lot of technical detail. For this first issue you can download the PDF for free. Check it out:

http://pythonmagazine.com

Initial thoughts on Python GUI frameworks

Kevin Day, October 7th, 2007

In the last few weeks I’ve tried two different Python frameworks for writing GUI applications: wxPython and PyGTK. Here’s my initial thoughts on each.

wxPython

wxPython comes with an awesome set of examples, and it seems to be the favorite Python framework among programmers. Copying and pasting from the examples was easy, and I had my own simple applications working in no time.

I also played around with Boa Constructor, which is a GUI that will produce wxPython code for widgets as they are added and edited. Unfortunately, the Linux version seems to have a few bugs. The most frustrating part was when I ran into a bug just by following the tutorial.

PyGTK

Later I switched to PyGTK because I found that it’s supported by Nokia’s series of internet tablets. PyGTK has a 400-page tutorial with several examples that I’ve been working through. Once I got familiar with the different widgets it wasn’t too hard putting together my own applications, although I’m still running into a few problems here and there. I’ve read that Glade is nice program for building GTK interfaces, so I’ll probably try that out soon.

Conclusion

Both wxPython and PyGTK seem like great ways to build Python GUIs. It’ll take a few more weeks of hacking to really get a feel for them though. For the moment I’m sticking with PyGTK because of the potential for using it with Nokia tablets (the N800 is on sale now for $243 at buy.com…hmm…) . When I get enough time I’ll post a short tutorial for a PyGTK application based on my recent experiences.