hello, i'm trying to make a simple canvas, where i can move images by
dragging them with the mouse, i don't want to use the drag'n'drop functions
because i want to know when there were collisions between the images, so i
did it with the usual events 'motion-notify-event','button-press-event' and
'release-press-event'. but when there are too many images in the screen, the
position of the mouse given by motion-notify-event is wrong and the images
shakes...
do somebody know how can i solve this problem?
here is a example of the problem, see by yourself:
-----------------------------------------------------------------------------------------------------------------------------------------------
import gtk
class img( gtk.Image ):
#list with all images
list = []
def __init__(self, image, layout, img_to_follow):
gtk.Image.__init__(self)
self.drag = False
self.drag_x = 0
self.drag_y = 0
self.layout = layout
#position of the image in the layout
self.x = 0
self.y = 0
self.set_from_file( image )
self.event_box = gtk.EventBox()
self.event_box.set_visible_window( False )
self.event_box.add( self )
#images cannot have these events....
self.event_box.add_events( gtk.gdk.POINTER_MOTION_MASK |
gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK )
self.event_box.connect("button-press-event", self.click)
self.event_box.connect("button-release-event", self.release)
self.event_box.connect("motion-notify-event", self.mousemove)
if img_to_follow:
self.img_to_follow = img_to_follow
self.follow = True
else:
self.follow = False
self.layout.put( self.event_box, 0, 0 )
self.list.append( self )
def click(self, widget, event):
#drag begin
self.drag = True
self.drag_x = event.x
self.drag_y = event.y
def release(self, widget, event):
self.drag = False
def mousemove(self, widget, event):
if self.drag:
for k in self.list:
if k.follow == False:
#follow
self.layout.move( k.event_box, k.x+int(
event.x-self.drag_x), k.y+int(event.y-self.drag_y) )
else:
self.layout.move( k.event_box, k.img_to_follow.x + 10,
k.img_to_follow.y + 10 )
#refresh logic position
k.x, k.y = self.layout.child_get( k.event_box, 'x', 'y' )
class move_test( object ):
def __init__( self ):
win = gtk.Window( gtk.WINDOW_TOPLEVEL )
layout = gtk.Layout()
#try to put more images here
img1 = img( 'img.png', layout, None )
img2 = img( 'img.png', layout, img1 )
img3 = img( 'img.png', layout, img2 )
img4 = img( 'img.png', layout, img3 )
img5 = img( 'img.png', layout, img4 )
img6 = img( 'img.png', layout, img5 )
img7 = img( 'img.png', layout, img6 )
img8 = img( 'img.png', layout, img7 )
img9 = img( 'img.png', layout, img8 )
img10 = img( 'img.png', layout, img9 )
img11 = img( 'img.png', layout, img10 )
img12 = img( 'img.png', layout, img11 )
img13 = img( 'img.png', layout, img12 )
win.add( layout )
win.show_all()
move_test()
gtk.main()
-----------------------------------------------------------------------------------------------------------
sorry for any possible english error XD
bye!
victor matheus
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/