While porting Sketch to PyGTK2 I came across a strange change in the
behavior of event objects in PyGTK2. The sample program below connects
to the button press, motion and release events and stores the event
object of the button_press event in an instance variable. In the motion
event handler the coordinates of the mouse in the saved press event are
always the same as the coordinates returned by get_pointer()!
I haven't followed all the C code yet, but it seems as though PyGTK
reuses the same underlying gdk event struct. Storing an explicit copy of
the press event works around the problem.
Here the code:
import gtk
class MyDrawingArea(gtk.DrawingArea):
def __init__(self):
gtk.DrawingArea.__init__(self)
self.add_events(gtk.gdk.POINTER_MOTION_MASK
| gtk.gdk.BUTTON_PRESS_MASK
| gtk.gdk.BUTTON_RELEASE_MASK
| gtk.gdk.POINTER_MOTION_HINT_MASK)
self.connect('button_press_event', self.button_press)
self.connect('motion_notify_event', self.motion_notify)
self.connect('button_release_event', self.button_release)
self.press_event = None
def button_press(self, widget, event):
print "button_press"
self.press_event = event
def motion_notify(self, widget, event):
x, y = self.get_pointer()
if self.press_event is not None:
print "motion_notify (%d, %d) (%g, %g)" \
% (x, y, self.press_event.x, self.press_event.y)
def button_release(self, widget, event):
print "button_release"
self.press_event = None
def main():
win = gtk.Window()
win.set_name("Test Input")
win.connect("destroy", gtk.mainquit)
win.set_border_width(5)
drawing_area = MyDrawingArea()
drawing_area.set_size_request(200, 200)
win.add(drawing_area)
drawing_area.show()
win.show()
gtk.main()
main()
--
Intevation GmbH http://intevation.de/
Sketch http://sketch.sourceforge.net/
MapIt! http://www.mapit.de/
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/