[pygtk] widget movement via context.arc

Vihan Pandey vihanpandey at gmail.com
Tue Feb 21 16:52:55 WST 2006


Hello everyone,

    I finally managed to achieve a degree of movement in the widget. I saw
the program written by Havoc Pennington and Johan Dahlin :


http://svn.sicem.biz/gazpacho/trunk/gazpacho/workspace.py

some VERY impressive code.

I also saw Alberto Ruiz's article :

http://www.pygtk.org/articles/cairo-pygtk-widget-signals-es/cairo-pygtk-widget-signals.html

It was very helpful as well, unfortunately as i don't speak Spanish i had to
rely on Google's translator to make sense of some of the text.


There is a question i do have with respect to Alberto's article. In all
cases of movement of a widget we are using a pressing and moving function(or
a do_button_press_event, do_button_release_event, do_motion_notify_event).
The essential logic is to get the mouse pointer co-ordinates via event.x and
event.y and use the same either calculating a displacement and
adding/subtracting the same with the widget position co-ordinate in
context.arc(as Alberto has done). I'm not well versed with GDK yet.
Therefore how does one get synchronised motion with the mouse pointer during
a click and drag event using context.arc ? Currently i can use the code
below to click and drag and move the widget in a North West - South East
orientation only.



the code:

import gtk
import gtk.gdk
import math

class Ellipse(gtk.DrawingArea):
    def __init__(self):
        gtk.DrawingArea.__init__(self)
    self.add_events(gtk.gdk.BUTTON_PRESS_MASK |
                gtk.gdk.BUTTON1_MOTION_MASK)

    self.connect("expose_event", self.expose)
    self.connect("button_press_event", self.pressing)
    self.connect("motion_notify_event", self.moving)

    self.desp = 0

    def pressing(self, widget, event):
    #print "Button Press"
    self.pressing_x = event.x

    def moving(self, widget, event):
    #print "Move"
    if (self.pressing_x - event.x) > 1:
        self.desp = self.desp + 0.1
    else:
        self.desp = self.desp - 0.1


    self.pressing_x = event.x

    self.draw(self.context)

    self.queue_draw()


    def expose(self, widget, event):
        self.context = widget.window.cairo_create()

        self.context.rectangle(event.area.x, event.area.y,
                               event.area.width, event.area.height)
        self.context.clip()

        self.draw(self.context)

        return False

    def draw(self, context):
        rect = self.get_allocation()

    x = rect.width / 2
    y = rect.height / 2
    ex = x
    ey = y
    ew = rect.width / 4.0
    eh = rect.height / 12.0

    context.save()
    context.translate (ex, ey)
    context.scale (ew / 2.0 , eh / 2.0)

    context.arc (0 - self.desp, 0 - self.desp, 1., 0, 2 * math.pi)
    #  This is where it all happens. How do i modify this to get a clean
movement in all directions
    #


    print self.desp

    context.set_source_rgb(0, 0, 0)
    context.fill_preserve()
    context.set_source_rgb(1, 1, 1)
    context.stroke()
    context.restore ()

def main():
    window = gtk.Window()
    e1 = Ellipse()

    window.add(e1)
    window.connect("destroy", gtk.main_quit)
    window.show_all()

    gtk.main()

if __name__ == "__main__":
    main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20060221/01657243/attachment.htm


More information about the pygtk mailing list