Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
wxPython Application Development Cookbook

You're reading from   wxPython Application Development Cookbook Over 80 step-by-step recipes to get you up to speed with building your own wxPython applications

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781785287732
Length 264 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Cody Precord Cody Precord
Author Profile Icon Cody Precord
Cody Precord
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. wxPython Starting Points FREE CHAPTER 2. Common User Controls 3. UI Layout and Organization 4. Containers and Advanced Controls 5. Data Displays and Grids 6. Ways to Notify and Alert 7. Requesting and Retrieving Information 8. User Interface Primitives 9. Creating and Customizing Components 10. Getting Your Application Ready for Release Index

Supporting drag and drop

Many applications allow users to open files by dragging a file from the operating system and dropping it in the application. wxPython, of course, provides support for this as well, through its controls using DropTargets. This recipe will show how to set up a DropTarget to allow handling the dragging and dropping of files in an application.

How to do it…

  1. First, let's create a drop target object to accept files that are dragged over and dropped in the application with the following code:
    class MyFileDropTarget(wx.FileDropTarget):
        def __init__(self, target):
            super(MyFileDropTarget, self).__init__()
            self.target = target
    
        def OnDropFiles(self, x, y, filenames):
            for fname in filenames:
                self.target.AppendText(fname)
  2. Next, all that is left is to connect the drop target to the window that should accept the dropped file(s). An example of this is shown in the following code:
    class MyFrame(wx.Frame):
        def __init__(self, parent, title=""):
            super(MyFrame, self).__init__(parent, title=title)
            
            # Set the panel
            self.text = wx.TextCtrl(self, style=wx.TE_MULTILINE)
            self.text.AppendText("Drag and drop some files here!")
            dropTarget = MyFileDropTarget(self.text)
            self.text.SetDropTarget(dropTarget)

How it works…

Drag and drop functions with the use of DropSources and DropTargets. In this case, we wanted to allow files to be dropped in the application, so FileDropTarget was created and associated with the TextCtrl window. DropTargets have several virtual callback functions that can be overridden to intercept different actions during the drag and drop events. As FileDropTarget is specialized for files, it only required overriding OnDropFiles, which is called with the list of filenames that were dropped in the application. It is necessary to subclass the drop target in order to intercept and handle the data it receives.

In order for a window to accept drag and drop actions, it must have a DropTarget set; the DropTarget then gives feedback on whether the data can be accepted or not as well as handling the reception of the data. Try out the example code with this recipe and you will see the mouse cursor change as you drag a file over; then, try again by dragging some text from another application to see the difference.

There's more…

It's also possible to create more application-specific drag and drop handling if needed for custom datatypes by deriving a custom drop target class from PyDropTarget. This class provides several more overridable methods to allow the handling of various events during the action. Here's a table explaining this:

Methods

Description

OnEnter(x, y, dragResult)

This is called when a drag object enters the window. The dragResult value returned from this method sets the custom cursor to provide feedback to the user (wx.DragCancel, wx.DragCopy, and so on).

OnDragOver(x, y, dragResult)

This is called while the object is dragged over the window. It returns a dragResult to give visual feedback.

OnLeave()

This is called when the drag object leaves the window.

OnDrop(x, y)

This is called when the drag object is dropped in the window. This method should return True if the data is accepted.

OnData(x, y, dragResult)

This is called after the data is accepted in OnDrop. The dropped data object is contained in drop targets data object (refer to GetDataObject). This should then typically just return the default passed in dragResult.

You have been reading a chapter from
wxPython Application Development Cookbook
Published in: Dec 2015
Publisher:
ISBN-13: 9781785287732
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image