I am trying to get the dimensions of an Image when the window is resized and 
create a new image with the size of the new dimensions. Am I going about this 
correctly? The program seems to freeze upon starting. I am thinking that when I 
receive the 'size-allocate' signal and create a new image I am causing the 
signal to be generated again and then I get into a horrible cycle of resizing. 
Any help would be appreciated.

Thanks!

#!/usr/bin/env python

import gobject
import gtk
import Numeric
import PIL.Image
import StringIO
import sys

import gpsd

class Piper(gtk.Window):
    def __init__(self):
        super(Piper, self).__init__()

        self.set_title("Piper")

        self.set_size_request(500, 500)
        self.set_position(gtk.WIN_POS_CENTER)

        mb = gtk.MenuBar()

        filemenu = gtk.Menu()
        filem = gtk.MenuItem("_File")
        filem.set_submenu(filemenu)

        agr = gtk.AccelGroup()
        self.add_accel_group(agr)

        exit = gtk.ImageMenuItem(gtk.STOCK_QUIT, agr)
        key, mod = gtk.accelerator_parse("Q")
        exit.add_accelerator("activate", agr, key, 
            mod, gtk.ACCEL_VISIBLE)

        exit.connect("activate", gtk.main_quit)

        filemenu.append(exit)

        mb.append(filem)
        
        self.map = gtk.Image()

        self.statusbar = gtk.Statusbar()
        self.statusbar.push(1, "Ready")

        vbox = gtk.VBox(False, 2)
        vbox.pack_start(mb, False, False, 0)
        vbox.pack_start(self.map, True, True, 0)
        vbox.pack_start(self.statusbar, False, False, 0)

        self.add(vbox)

        self.map.connect("size-allocate", self.update_map)
        self.connect("destroy", gtk.main_quit)
        self.show_all()

    def update_map(self, widget, rect):
        print rect.x, rect.y, rect.width, rect.height
        image = PIL.Image.new("RGB", (rect.width, rect.height), (255, 0, 0))
        pixbuf = self.image_to_pixbuf(image)
        self.map.set_from_pixbuf(pixbuf)

    def image_to_pixbuf(self, image):
        fd = StringIO.StringIO()
        image.save(fd, "ppm")
        contents = fd.getvalue()
        fd.close()
        loader = gtk.gdk.PixbufLoader("pnm")
        loader.write(contents, len(contents))
        pixbuf = loader.get_pixbuf()
        loader.close()
        return pixbuf

Piper()
gtk.main()


                                          
_________________________________________________________________
Hotmail: Free, trusted and rich email service.
http://clk.atdmt.com/GBL/go/171222984/direct/01/
_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to