下面的代码创建了一个带有列表框的框架来拖放。 我想了解下面的代码..
FileAndTextDropTarget是一个类。这个类用2个函数调用 - OnFileDrop& OnTextDrop ..我的问题是如何在没有传递参数文件或文本的情况下调用OnFileDrop或OnTextDrop ..
self.dt = FileAndTextDropTarget(self.OnFileDrop, self.OnTextDrop)
代码:
class DropTargetFrame(wx.Frame):
pdb.set_trace()
def __init__(self, parent, id=wx.ID_ANY, title="",
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=wx.DEFAULT_FRAME_STYLE,
name="DropTargetFrame"):
super(DropTargetFrame, self).__init__(parent, id,
title, pos,
size, style,
name)
# Attributes
choices = ["Drag and Drop Text or Files here",]
self.list = wx.ListBox(self,
choices=choices)
self.dt = FileAndTextDropTarget(self.OnFileDrop,
self.OnTextDrop)
self.list.SetDropTarget(self.dt)
# Setup
self.CreateStatusBar()
def OnFileDrop(self, files):
self.PushStatusText("Files Dropped")
for f in files:
self.list.Append(f)
def OnTextDrop(self, text):
self.PushStatusText("Text Dropped")
self.list.Append(text)
class FileAndTextDropTarget(wx.PyDropTarget):
"""Drop target capable of accepting dropped files and text"""
def __init__(self, file_callback, text_callback):
assert callable(file_callback)
assert callable(text_callback)
super(FileAndTextDropTarget, self).__init__()
# Attributes
self.fcallback = file_callback # Drop File Callback
self.tcallback = text_callback # Drop Text Callback
self._data = None
self.txtdo = None
self.filedo = None
# Setup
self.InitObjects()
def InitObjects(self):
"""Initializes the text and file data objects"""
self._data = wx.DataObjectComposite()
self.txtdo = wx.TextDataObject()
self.filedo = wx.FileDataObject()
self._data.Add(self.txtdo, False)
self._data.Add(self.filedo, True)
self.SetDataObject(self._data)
def OnData(self, x_cord, y_cord, drag_result):
"""Called by the framework when data is dropped on the target"""
if self.GetData():
data_format = self._data.GetReceivedFormat()
if data_format.GetType() == wx.DF_FILENAME:
self.fcallback(self.filedo.GetFilenames())
else:
self.tcallback(self.txtdo.GetText())
return drag_result
答案 0 :(得分:0)
据我了解,当你调用“SetDropTarget”时,你告诉wxPython在GUI的那一部分上观察鼠标事件。 TextDataObject和类似文件包含“智能”,以了解它支持的格式以及如何在它到达时呈现它(根据wxPython演示)。
所以当你选择并开始拖动它时,wxPython可以告诉你。另见: