From: Bill B. <bil...@pe...> - 2006-02-18 11:50:28
|
I'm testing out adding drag and drop capabilities to my PythonCard app. I'm looking to have files dropped onto a wxList. So far I have code like this: <code> class FileDropTarget(wx.FileDropTarget): def __init__(self, obj): wx.FileDropTarget.__init__(self) self.obj = obj def OnDropFiles(self, x, y, filenames): [self.obj.Append(f) for f in filenames] </code> and then later (in my GUI class): <code> class GUI(model.Background): def on_initialize(self, event): self.fileList = self.components.fileList dropTarget = FileDropTarget(self.fileList) self.fileList.SetDropTarget(dropTarget) </code> The above works fine. I can drop the files on the wxList control and the filenames are appended to the List. Now, I need to know when files have been dropped on the List, so I added this line (under on_initialize): <code> self.fileList.Bind(wx.EVT_DROP_FILES, self.on_fileList_DropFiles) </code> and then later (in my code) I have a method that looks like this: <code> def on_fileList_DropFiles(self, event): print 'files have been dropped' </code> When I drop files on the List, I can't seem to get the DROP_FILES event to fire. IOW, my program never prints 'files have been dropped'. Does anyone know what I'm doing wrong? In case it matters, I'm also binding this event to the fileList: <code> self.fileList.Bind(wx.EVT_RIGHT_DOWN, self.on_fileList_RightDown) </code> and this event works fine. Here's my specifications: OS: Windows 2000 Pro SP4 PythonCard: 0.8.1 Python: 2.4.1 wxPython: 2.6.1.0 Thanks, Bill |
From: Bill B. <bil...@pe...> - 2006-02-18 11:57:26
|
> <code> > class GUI(model.Background): > def on_initialize(self, event): > self.fileList = self.components.fileList > dropTarget = FileDropTarget(self.fileList) > self.fileList.SetDropTarget(dropTarget) > </code> > Oops - The above didn't come out correctly. The code (properly indented looks like this: <code> class GUI(model.Background): def on_initialize(self, event): self.fileList = self.components.fileList dropTarget = FileDropTarget(self.fileList) self.fileList.SetDropTarget(dropTarget) </code> Bill |
From: Alex T. <al...@tw...> - 2006-02-18 15:43:43
|
Bill Burns wrote: > I'm testing out adding drag and drop capabilities to my PythonCard app. > I'm looking to have files dropped onto a wxList. > > So far I have code like this: > > <snip> > The above works fine. I can drop the files on the wxList control and the > filenames are appended to the List. > > Now, I need to know when files have been dropped on the List, so I added > this line (under on_initialize): > Why not simply do this in the OnDropFiles() function, i.e. <code> class FileDropTarget(wx.FileDropTarget): def __init__(self, obj): wx.FileDropTarget.__init__(self) self.obj = obj def OnDropFiles(self, x, y, filenames): [self.obj.Append(f) for f in filenames] print "component", self.obj.name, "received files", filenames --- THIS </code> > When I drop files on the List, I can't seem to get the DROP_FILES event > to fire. IOW, my program never prints 'files have been dropped'. > > Does anyone know what I'm doing wrong? > I don't. However, I think the event mechanism for dropping is different from the usual one. If you could simply bind to EVT_DROP_FILES, then I think there'd be no need for the SetDropTarget complexity, would there? However, I do know for sure that the above change works and prints out the name of the component on which they've been dropped, and (just the new) file names on a drop event, so it can be used even if you have multiple drop targets. -- Alex Tweedly http://www.tweedly.net -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.1.375 / Virus Database: 267.15.11/264 - Release Date: 17/02/2006 |
From: Bill B. <bil...@pe...> - 2006-02-18 18:41:45
|
[Alex modifying Bill's code] > Why not simply do this in the OnDropFiles() function, > i.e. > <code> > class FileDropTarget(wx.FileDropTarget): > def __init__(self, obj): > wx.FileDropTarget.__init__(self) > self.obj = obj > > def OnDropFiles(self, x, y, filenames): > [self.obj.Append(f) for f in filenames] > print "component", self.obj.name, "received files", filenames > --- THIS > </code> > [Bill] >> When I drop files on the List, I can't seem to get the DROP_FILES event >> to fire. IOW, my program never prints 'files have been dropped'. >> >> Does anyone know what I'm doing wrong? >> [Alex] > I don't. > However, I think the event mechanism for dropping is different from the > usual one. If you could simply bind to EVT_DROP_FILES, then I think > there'd be no need for the SetDropTarget complexity, would there? > > However, I do know for sure that the above change works and prints out > the name of the component on which they've been dropped, and (just the > new) file names on a drop event, so it can be used even if you have > multiple drop targets. Alex, Thank you for the reply!! I'm just a python hobbyist which means - I'm not that good, yet :-) I hadn't considered putting the 'print' statement in the OnDropFiles function. How simple! BTW - I really don't want to print the names of the files. I was only using that as an example. I actually want to check: 1). If they're the type of files I want to accept (PDF & Tiff). 2). If the files are Landscape or Portrait. So now (using your suggestion), I can simply do something like this: <code> def OnDropFiles(self, x, y, filenames): if checkTheseFiles(filenames): [self.obj.Append(f) for f in filenames] </code> Where 'checkTheseFiles' is another function to maybe check the type and orientation. Although, I could check the file extension easily enough right within 'OnDropFiles'. Thanks again for the help!! Bill |