wxPython中的拖放在计算机图形用户界面中,拖放是指点击一个虚拟对象,将其拖到不同位置或拖到另一个虚拟对象的动作对象(或支持此操作)。通常,它可用于调用各种操作,或在两个抽象对象之间创建各种类型的关联。拖放让您可以直观地完成复杂的事情。在拖放操作中,我们将一些数据从数据源拖到数据目标上。所以我们必须有一些数据一个数据源一个数据目标在wxPython中我们有两个预定义的数据目标。wx.TextDropTarget和wx.FileDropTarget。wx.TextDropTargetwx.TextDropTarget是用于处理文本数据的预定义放置目标。#dragdrop_text.pyfrompathlibimportPathimportosimportwxclassMyTextDropTarget(wx.TextDropTarget):def__init__(self,object):wx.TextDropTarget.__init__(self)self.object=objectdefOnDropText(self,x,y,data):自我.object.InsertItem(0,data)returnTrueclassExample(wx.Frame):def__init__(self,*args,**kw):super(Example,self).__init__(*args,**kw)self.InitUI()defInitUI(self):splitter1=wx.SplitterWindow(self,style=wx.SP_3D)splitter2=wx.SplitterWindow(splitter1,style=wx.SP_3D)home_dir=str(Path.home())self.dirWid=wx.GenericDirCtrl(splitter1,dir=home_dir,style=wx.DIRCTRL_DIR_ONLY)self.lc1=wx.ListCtrl(splitter2,style=wx.LC_LIST)self.lc2=wx.ListCtrl(splitter2,style=wx.LC_LIST)dt=MyTextDropTarget(self.lc2)self.lc2.SetDropTarget(dt)self.Bind(wx.EVT_LIST_BEGIN_DRAG,self.OnDragInit,id=self.lc1.GetId())树=self.dirWid.GetTreeCtrl()splitter2.SplitHorizo??ntally(self.lc1,self.lc2,150)splitter1.SplitVertically(self.dirWid,splitter2,200)self.Bind(wx.EVT_TREE_SEL_CHANGED,self.OnSelect,id=tree.GetId())self.OnSelect(0)self.SetTitle('拖放文本')self.Centre()defOnSelect(self,event):list=os.listdir(self.dirWid.GetPath())self.lc1.ClearAll()self.lc2.ClearAll()foriinrange(len(list)):iflist[i][0]!='.':self.lc1.InsertItem(0,list[i])defOnDragInit(self,event):text=self.lc1.GetItemText(event.GetIndex())tdo=wx.TextDataObject(text)tds=wx.DropSource(self.lc1)tds.SetData(tdo)tds.DoDragDrop(True)defmain():app=wx.App()ex=Example(None)ex.Show()app.MainLoop()if__name__=='__main__':main()本例我们在wx.GenericDirCtrl显示一个文件系统,选中目录的内容显示在右上角的列表控件中,文件名可以拖放到右下角的列表控件中。defOnDropText(self,x,y,data):self.object.InsertItem(0,data)returnTrue当我们将文本数据拖放到目标上时,数据通过InsertItem()方法插入到列表控件中dt=MyTextDropTarget(self.lc2)self.lc2.SetDropTarget(dt)a放置目标创建,我们使用SetDropTarget()方法将放置目标设置为第二个列表控件。self.Bind(wx.EVT_LIST_BEGIN_DRAG,self.OnDragInit,id=self.lc1.GetId())当拖动操作开始时,将调用OnDragInit()方法defOnDragInit(self,event):text=self.lc1.GetItemText(event.GetIndex())tdo=wx.TextDataObject(text)tds=wx.DropSource(self.lc1)..在OnDragInit()方法中,我们创建了一个包含文本数据的wx.TextDataObject。从第一个列表控件创建放置源。tds.SetData(tdo)tds.DoDragDrop(True)我们使用SetData()设置放置源的数据,并使用DoDragDrop()启动拖放操作。wx.FileDropTargetwx.FileDropTarget是一个放置目标,可以接受从文件管理器拖拽的文件。#dragdrop_file.pyimportwxclassFileDrop(wx.FileDropTarget):def__init__(self,window):wx.FileDropTarget.__init__(self)self.window=windowdefOnDropFiles(self,x,y,filenames):对于文件名中的名称:尝试:file=open(name,'r')text=file.read()self.window.WriteText(text)除了IOError作为错误:msg="Erroropeningfile\n{}".format(str(error))dlg=wx.MessageDialog(None,msg)dlg.ShowModal()返回False除了UnicodeDecodeError作为错误:msg="Cannotopennonasciifiles\n{}".format(str(error))dlg=wx.MessageDialog(None,msg)dlg.ShowModal()最后返回False:file.close()返回TrueclassExample(wx.Frame):def__init__(self,*args,**kw):super(Example,self).__init__(*args,**kw)self.InitUI()defInitUI(self):self.text=wx.TextCtrl(self,style=wx.TE_MULTILINE)dt=FileDrop(self.text)self.text.SetDropTarget(dt)self.SetTitle('文件拖放')self.Centre()defmain():app=wx.App()ex=Example(None)ex.Show()app.MainLoop()if__name__=='__main__':main()这个例子创建了一个简单的wx.TextCtrl,我们可以将文本文件从文件管理器拖到控件中。defOnDropFiles(self,x,y,filenames):fornameinfilenames:...我们可以一次拖放多个文件。try:file=open(name,'r')text=file.read()self.window.WriteText(text)我们以只读模式打开文件,获取其内容并将内容写入文本控制窗口。除了IOError作为错误:msg="Erroropeningfile\n{}".format(str(error))dlg=wx.MessageDialog(None,msg)dlg.ShowModal()returnFalse如果有输入/输出错误,我们将显示一个消息对话框并终止操作。self.text=wx.TextCtrl(self,style=wx.TE_MULTILINE)dt=FileDrop(self.text)self.text.SetDropTarget(dt)wx.TextCtrl是拖放目标。