#!/usr/bin/python # 48 x 48 pixel image filename="apple-red.png" import cairo import gtk class ImageFace(gtk.DrawingArea): def __init__(self): gtk.DrawingArea.__init__(self) # load the image self.image = cairo.ImageSurface.create_from_png(filename) # the two callback needed self.connect("expose_event", self.expose) self.connect("button_press_event", self.on_button_press) # set the button press event to happen self.add_events(gtk.gdk.BUTTON_PRESS_MASK) self.set_size_request(610, 610) def expose(self, widget, event): cr = widget.window.cairo_create() # draw the image 100 times for x in range(10,560,60) : for y in range(10,560,60) : cr.set_source_surface(self.image, x, y) cr.paint(); return False def on_button_press(self, widget, event): # filter out all but the left mouse clicks if event.button == 1 : # find out where in the image we clicked x = event.x % 60 y = event.y % 60 # see if we are on an image if x > 12 and y > 17 and x < 50 and y < 50 : # print what image we clicked on print "you clicked on image %d x %d" % (int(event.x / 60), int(event.y / 60)) def main(): window = gtk.Window() images = ImageFace() sw = gtk.ScrolledWindow() window.add(sw) sw.add_with_viewport(images) window.connect("destroy", gtk.main_quit) window.show_all() gtk.main() if __name__ == "__main__": main()