Things have been going well. I did have a bit of a surprise when I added the possibility to drop files onto an open picture viewer. In C#/WPF, this is a simple event, like clicking a button. But in wxPython, one needs to instanciate a new class, wx.FileDropTarget, to do this.
All examples I found on the Web did this using two separate objects, so there was a need to have them communicate. But I thought, why not do them in the same object? This gave me the following code (but see CAUTION below):
import wx class DropApp(wx.Frame, wx.FileDropTarget): # two parent classes def __init__(self): self.app = wx.App() wx.Frame.__init__(self, parent=None, title='DropFile', size=(400, 250)) wx.FileDropTarget.__init__(self) self.SetDropTarget(self) # connecting the two parent classes :-) def OnDropFiles(self, x, y, names): # overriding method from wx.FileDropTarget print 'File dropped(' + str(type(names[0])) + '): ' + str(names) # process the file names in the context of the wx.Frame app ! def run(self): self.Show() self.app.MainLoop() app = DropApp() app.run()It seems to work fine in my picture viewer.
CAUTION: That was in wxPython 3. I have now upgraded to wxPython 4, and the above crashes. So it's probably not a good idea!
The following is probably closer to how it should be done:
from __future__ import print_function import wx print('wx.VERSION=', wx.VERSION) class Dropper(wx.FileDropTarget): def __init__(self, target): wx.FileDropTarget.__init__(self) self.target = target def OnDropFiles(self, x, y, names): return self.target.OnDropFiles(names) class DropApp(wx.Frame): def __init__(self): self.app = wx.App() wx.Frame.__init__(self, parent=None, title='DropFile', size=(400, 250)) self.SetDropTarget(Dropper(self)) def OnDropFiles(self, names): print('class DropApp : file dropped(' + str(type(names[0])) + '): ' + str(names)) # process the file names in the context of the wx.Frame app ! return True def run(self): self.Show() self.app.MainLoop() app = DropApp() app.run()
I guess I was trying to be too smart... I have recently read that multiple inheritance of wxPython classes is not recommended !
Similarly for error handling. Maybe the text should name the important exceptions, but I don't need that in the example.
Home. Email: (français, English, Deutsch).