Dropping in action

The code sample in listing 18.3 displays how to create a frame that will accept file drops. You can test this code by dragging a file from your Explorer or Finder window onto the frame, and viewing the file information displayed in the frame. Figure 18.3 displays what the code looks like when it's running.

□ Drop Target

Drop some files here:

1 file<s) dropped at (87,102):



The drop target frame

Figure 18.3

The drop target frame

Listing 18.3 The code for a file drop target import wx class MyFileDropTarget(wx.FileDropTarget)

wx.FileDropTarget.__init__(self) self.window = window

Declaring the drop target def OnDropFiles(self, x, y, filenames): <-J Dropping file handler data self.window.AppendText("\n%d file(s) dropped at (%d,%d):\n" %

for file in filenames:

self.window.AppendText("\t%s\n" % file)

class MyFrame(wx.Frame):

wx.Frame.__init__(self, None, title="Drop Target", size=(500,3 00))

label = wx.StaticText(p, -1, "Drop some files here:")

text = wx.TextCtrl(p, -1, "", style=wx.TE_MULTILINE|wx.HSCROLL) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(label, 0, wx.ALL, 5) sizer.Add(text, 1, wx.EXPAND|wx.ALL, 5) p.SetSizer(sizer)

dt = MyFileDropTarget(text) <— Making the text a target text.SetDropTarget(dt)

app = wx.PySimpleApp() frm = MyFrame() frm.Show() app.MainLoop()

So far, we've limited the data transfer discussion to predefined wxPython objects. Next, we'll discuss how to put your own data on the clipboard.

Was this article helpful?

0 0

Post a comment