<table cellspacing='0' cellpadding='0' border='0' ><tr><td valign='top' style='font: inherit;'>Hi,<br><br>I'm trying to create a rectangle when the user presses or releases the mouse over a cairo object.I'm using the eventBox to capture the mouse events.<br>Now the problem is the shapes is beng drawn when I call it from the expose_event event handler and nothing happens when I call the same from the mouse release event handler.<br><br>Here is my code :<br><br><span style="font-style: italic;"></span><span style="font-style: italic;">#!/usr/bin/env python<br><br>import sys<br>import cairo<br>import gtk<br><br><br>start = None #Starting co-ords<br>stop = None #Ending co-ords<br>box = None&nbsp; #Tuple of co-ords for drawing rect<br>ctx = None #Cairo Widget<br>def button_press(widget, event, surface):<br>&nbsp;&nbsp;&nbsp; global start<br>&nbsp;&nbsp;&nbsp; start = (int(event.x),int(event.y))<br>def motion_notify(widget, event,
 surface):<br>&nbsp;&nbsp;&nbsp; pass<br>def button_release(widget, event, surface):<br>&nbsp;&nbsp;&nbsp; global stop,ctx<br>&nbsp;&nbsp;&nbsp; ctx = None<br>&nbsp;&nbsp;&nbsp; ctx = widget.window.cairo_create()<br>&nbsp;&nbsp;&nbsp; ctx.set_source_surface(surface, 0,0)<br>&nbsp;&nbsp;&nbsp; ctx.paint()<br>&nbsp;&nbsp;&nbsp; print ctx<br>&nbsp;&nbsp;&nbsp; stop =&nbsp; (int(event.x),int(event.y))<br>&nbsp;&nbsp;&nbsp; box = (start[0],start[1])<br>&nbsp;&nbsp;&nbsp; draw_dotrect(ctx,box)<br>&nbsp;&nbsp;&nbsp; print "Released" + str(box)<br><br>def draw_dotrect(ctx,box):<br>&nbsp;&nbsp;&nbsp; ctx.set_source_rgba(0, 0, 0,0.5)<br>&nbsp;&nbsp;&nbsp; ctx.save()<br>&nbsp;&nbsp;&nbsp; ctx.new_path()<br>&nbsp;&nbsp;&nbsp; ctx.translate(3*20, 0)<br>&nbsp;&nbsp;&nbsp; square(ctx,box)<br>&nbsp;&nbsp;&nbsp; ctx.fill()<br>&nbsp;&nbsp;&nbsp; ctx.restore()<br>&nbsp;&nbsp;&nbsp; ctx.set_line_width(20 / 16)<br>&nbsp;&nbsp;&nbsp;
 ctx.set_tolerance(0.1)<br>&nbsp;&nbsp;&nbsp; ctx.set_line_join(cairo.LINE_JOIN_MITER)<br>&nbsp;&nbsp;&nbsp; ctx.set_dash([20/2.0, 20/2.0], 8)<br>&nbsp;&nbsp;&nbsp; ctx.save()<br>&nbsp;&nbsp;&nbsp; ctx.new_path()<br>&nbsp;&nbsp;&nbsp; ctx.set_source_rgb(0,0 ,0 )<br>&nbsp;&nbsp;&nbsp; ctx.translate(3*20, 0)<br>&nbsp;&nbsp;&nbsp; square(ctx,box)<br>&nbsp;&nbsp;&nbsp; ctx.stroke_preserve()<br>&nbsp;&nbsp;&nbsp; ctx.restore()<br>&nbsp;&nbsp;&nbsp; <br>def expose_event(widget, event, surface):<br>&nbsp;&nbsp;&nbsp; global ctx<br>&nbsp;&nbsp;&nbsp; ctx = widget.window.cairo_create()<br>&nbsp;&nbsp;&nbsp; ctx.set_source_surface(surface, 0,0)<br>&nbsp;&nbsp;&nbsp; ctx.paint()<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; #draw_dotrect(ctx,(20,20)) # If called from here,it works<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>def square(ctx,box):<br>&nbsp;&nbsp;&nbsp; ctx.move_to(0, 0)<br>&nbsp;&nbsp;&nbsp; ctx.rel_line_to(2*50, 0)<br>&nbsp;&nbsp;&nbsp;
 ctx.rel_line_to(0, 2*50)<br>&nbsp;&nbsp;&nbsp; ctx.rel_line_to(-2*50, 0)<br>&nbsp;&nbsp;&nbsp; ctx.close_path()<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; if len(sys.argv) != 2:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; raise SystemExit('usage: png_view.py png_file')<br><br>filename = sys.argv[1]<br><br>surface = cairo.ImageSurface.create_from_png(filename)<br>Width&nbsp; = surface.get_width()<br>Height = surface.get_height()<br><br>win = gtk.Window()<br>win.connect('destroy', gtk.main_quit)<br><br>event_box = gtk.EventBox()<br>win.add(event_box)<br><br>drawingarea = gtk.DrawingArea()<br>event_box.add(drawingarea)<br><br>drawingarea.connect('expose_event', expose_event,
 surface)<br>event_box.connect('button_press_event',button_press,surface)<br>event_box.connect('button_release_event',button_release,surface)<br>event_box.connect('motion_notify_event',motion_notify,surface)<br><br>drawingarea.set_size_request(Width,Height)<br><br>win.show_all()<br>gtk.main()</span><br><br>It doesn't throw me any error.I hardcoded the box to simplify things .&nbsp;&nbsp;&nbsp; I also create a new ctx in the release event handler else it throws an <span style="background-color: rgb(255, 255, 255);">error:</span><font size="2"><a style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="link" href="http://www.cairographics.org/manual/cairo-Error-Handling.html#CAIRO-STATUS-SURFACE-FINISHED:CAPS"><code class="literal">CAIRO_STATUS_SURFACE_FINISHED</code></a></font>
<br><br>Any ideas why ?<br><br>" life isn't heavy enough,it flies away and floats far above action"</td></tr></table><br>







      <hr size=1>
Start at the new Yahoo!7 for a better online experience - <a href="http://au.rd.yahoo.com/mail/taglines/au/y7mail/default/*http://au.docs.yahoo.com/somethingwonderful/index.php?p1=brand&p2=other&p3=au&p4=tagline" target=_blank>Start Here</a>.