2012-05-21 23 views
1

下面的代码创建一个带有列表框的框架来拖放。 我想了解在下面的行代码..Python框架 - 列表框

FileAndTextDropTarget是class..This类被称为有2个功能 - OnFileDrop & OnTextDrop..My问题是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

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

所以,当你选择一些东西,并开始拖动它,wxPython可以告诉。另请参见: