<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 #Tuple of co-ords for drawing rect<br>ctx = None #Cairo Widget<br>def button_press(widget, event, surface):<br> global start<br> start = (int(event.x),int(event.y))<br>def motion_notify(widget, event,
surface):<br> pass<br>def button_release(widget, event, surface):<br> global stop,ctx<br> ctx = None<br> ctx = widget.window.cairo_create()<br> ctx.set_source_surface(surface, 0,0)<br> ctx.paint()<br> print ctx<br> stop = (int(event.x),int(event.y))<br> box = (start[0],start[1])<br> draw_dotrect(ctx,box)<br> print "Released" + str(box)<br><br>def draw_dotrect(ctx,box):<br> ctx.set_source_rgba(0, 0, 0,0.5)<br> ctx.save()<br> ctx.new_path()<br> ctx.translate(3*20, 0)<br> square(ctx,box)<br> ctx.fill()<br> ctx.restore()<br> ctx.set_line_width(20 / 16)<br>
ctx.set_tolerance(0.1)<br> ctx.set_line_join(cairo.LINE_JOIN_MITER)<br> ctx.set_dash([20/2.0, 20/2.0], 8)<br> ctx.save()<br> ctx.new_path()<br> ctx.set_source_rgb(0,0 ,0 )<br> ctx.translate(3*20, 0)<br> square(ctx,box)<br> ctx.stroke_preserve()<br> ctx.restore()<br> <br>def expose_event(widget, event, surface):<br> global ctx<br> ctx = widget.window.cairo_create()<br> ctx.set_source_surface(surface, 0,0)<br> ctx.paint()<br> <br> #draw_dotrect(ctx,(20,20)) # If called from here,it works<br> <br> <br>def square(ctx,box):<br> ctx.move_to(0, 0)<br> ctx.rel_line_to(2*50, 0)<br>
ctx.rel_line_to(0, 2*50)<br> ctx.rel_line_to(-2*50, 0)<br> ctx.close_path()<br> <br> if len(sys.argv) != 2:<br> 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 = 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 . 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>.