gpt4 book ai didi

python - 如何使用pygtk拖动图像

转载 作者:太空狗 更新时间:2023-10-30 02:49:04 24 4
gpt4 key购买 nike

如何使用 pygtk 拖动/移动图像?

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()

关于python - 如何使用pygtk拖动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9852359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com