1

下面的代码创建了一个带有列表框的框架以进行拖放。我想了解下一行中的代码..

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
4

1 回答 1

0

据我了解,当您调用“SetDropTarget”时,您告诉 wxPython 在 GUI 的该部分上监视鼠标事件。TextDataObject 和类似的包含“智能”以了解它支持什么格式以及如何在它到达那里时呈现它(根据 wxPython 演示)。

所以当你选择一些东西并开始拖动它时,wxPython 可以分辨出来。也可以看看:

于 2012-05-21T17:33:49.180 回答