[英]GTK Drag-and-drop Validation X11 Cursor lock up within drag_get_data
我一直無法在pygtk中進行拖放驗證。 我沒有主意,想要第二意見。
我的目標是僅允許刪除包含.jpg的文件。
具體來說,每當我在拖動動作回調中調用widget.drag_get_data時,X11光標就會鎖定。 由於我不得不殺死X11並重新啟動所有程序,因此調試變得乏味且麻煩。
這是我的源代碼,我認為問題專門出在drag_motion_cb和drag_data_received_cb方法中。 我已經離開了之前嘗試過的注釋掉的部分。
使用Google代碼搜索搜索drag_get_data不會顯示其他人在進行高級驗證。 所以我想其他人也會失敗。
我沒有主意,如果不能弄清楚的話,最終只會在我的linux端口中使用簡單的DnD(沒有適當的驗證)。
提前致謝。
import pygtk pygtk.require('2.0') import gobject import gtk
TARGET_TEXT_URI_LIST = 0
drop_targets = [ ("text/uri-list", 0, TARGET_TEXT_URI_LIST) ]
class TestApp(gobject.GObject): builder = gtk.Builder() window = None button = None
def init(self): gobject.GObject.init(self)
assert self.builder != None
self.builder.add_from_file("MainWindow.glade");
self.window = self.builder.get_object("window1")
self.button = self.builder.get_object("button1")
self.window.connect("destroy", gtk.main_quit)
self.button.drag_dest_set(gtk.DEST_DEFAULT_ALL, drop_targets, gtk.gdk.ACTION_COPY|gtk.gdk.ACTION_LINK|gtk.gdk.ACTION_MOVE)
self.button.connect("drag-data-received", self.drag_data_received_cb)
self.button.connect("drag-drop", self.drag_drop_cb)
self.button.connect("drag-motion", self.drag_motion_cb)
self.button.connect("drag-leave", self.drag_leave_cb)
self.window.show_all()
drop_data_ready = False drop_occurred = False drop_highlight = False drop_data = None
def drag_data_received_cb(self,widget,context,x,y,data,info,timestamp): print "drag_data_received_cb"
# Check to see if we have the drop data yet.
if False == self.drop_data_ready:
# If this is data we expected or can handle, then process it.
if TARGET_TEXT_URI_LIST == info and data.get_format() == 8 and data.get_length() > 0:
self.drop_data = data.get_uris()
self.drop_data_ready = True
context.drag_status(gtk.gdk.ACTION_COPY, timestamp)
# Actual drop handling code.
if True == self.drop_occurred:
# Reset state.
self.drop_occurred = False
print "RECEIVED DROP",self.drop_data
# Notify whether we handled the drop or not.
context.finish(True,False,timestamp)
# Clean up.
self.drag_leave_cb(widget, context, timestamp)
return True
def drag_drop_cb(self,widget,context,x,y,timestamp): target = widget.drag_dest_find_target(context, widget.drag_dest_get_target_list())
# Is it something we can handle?
if target == gtk.gdk.atom_intern("text/uri-list", False):
# Tell data recieved handler (do_drag_data_received) we can actually handle the drop.
self.drop_occurred = True
widget.drag_get_data(context,target,timestamp)
# We can handle this data type.
return True
else:
# We cannot handle the drop.
return False
pass
def drag_motion_cb(self,widget,context,x,y,timestamp):
if not self.drop_data_ready:
widget.drag_get_data(context, gtk.gdk.atom_intern("text/uri-list",False), timestamp)
return False
"""
target = widget.drag_dest_find_target(context, widget.drag_dest_get_target_list())
if target == gtk.gdk.atom_intern("text/uri-list", False):
if True == self.drop_data_ready:
pass
else:
#widget.drag_get_data(context, target, timestamp)
pass
"""
context.drag_status(gtk.gdk.ACTION_COPY, timestamp)
"""
if target == gtk.gdk.atom_intern("text/uri-list", False):
if True == self.drop_data_ready:
if repr(drop_data).find(".jpg") != -1:
# Tell Gdk we can handle this.
context.drag_status(gtk.gdk.ACTION_COPY, timestamp)
# Draw drop highlight (but only once).
if False == self.drop_highlight:
widget.drag_highlight()
self.drop_highlight = True
# Return here, don't fall through.
return True
else:
# Request drag data from the source.
widget.drag_get_data(context, target, timestamp)
# Fall-through to not allowing.
"""
# not something we can handle
#context.drag_status(0, timestamp) # Don't allow drop.
return True
pass
def drag_leave_cb(self,widget,context,timestamp): # Cleanup drag data. if True == self.drop_data_ready: self.drop_data = None self.drop_data_ready = False
# Un-draw the highlight.
if True == self.drop_highlight:
widget.drag_unhighlight()
self.drop_highlight = False
pass
gobject.type_register(TestApp)
def main(): car = TestApp() gtk.main()
if name == 'main': main()
我認為您必須在drag_data_received_cb中執行類似的操作
def drag_data_received_cb(self, obj, context, x, y, selection, target_id, etime):
try:
dtype = selection.get_data_type()
except AttributeError:## Old PyGTK
dtype = selection.type
if dtype=='image/jpeg':
image_data = selection.data
## do somthing with image_data
return True
return False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.