How to drag images with pygtk

前端 未结 1 1587
死守一世寂寞
死守一世寂寞 2021-01-14 02:18

How can I drag/move images using pygtk?

Here is a video of someone doing it, however the person did not give the source code.

相关标签:
1条回答
  • 2021-01-14 03:05

    here's a little demo (i don(t remmeber excatly were it comes from, sorry) which is very similar to the one found here: http://www.pygtk.org/pygtk2tutorial/examples/dragndrop.py

    import gtk
    
    class DragImage(gtk.Image):
        def __init__(self,image,layout):
            gtk.Image.__init__(self)
            self.drag = False
            self.drag_x = 0
            self.drag_y = 0
            self.layout = 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)
            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)
            self.layout.put( self.event_box, 0, 0 )
    
        def click(self, widget, event):
            self.drag =  True
            self.drag_x =  event.x
            self.drag_y =  event.y
            print(self.drag_x, self.drag_y)
    
        def release(self, widget, event):
            self.drag =  False
    
        def mousemove(self,widget,event):
            if self.drag:
                self.layout.move(self.event_box,self.x+int(event.x-self.drag_x),self.y+int(event.y-self.drag_y))
                self.x, self.y = self.layout.child_get(self.event_box,'x','y')
    
    class move_test(object):
        def __init__(self):
            window =  gtk.Window()
            layout =  gtk.Layout()
            img1 = DragImage('image1.jpg',layout)
            img2 = DragImage('image2.jpg',layout)
            window.add(layout)
            window.show_all()
    
    move_test()
    gtk.main()
    
    0 讨论(0)
提交回复
热议问题