Book Image

wxPython Application Development Cookbook

By : Cody Precord
Book Image

wxPython Application Development Cookbook

By: Cody Precord

Overview of this book

wxPython is a GUI toolkit for the Python programming language built on top of the cross-platform wxWidgets GUI libraries. wxPython provides a powerful set of tools that allow you to quickly and efficiently building applications that can run on a variety of different platforms. Since wxWidgets provides a wrapper around each platform’s native GUI toolkit, the applications built with wxPython will have a native look and feel wherever they are deployed. This book will provide you with the skills to build highly functional and native looking user interfaces for Python applications on multiple operating system environments. By working through the recipes, you will gain insights into and exposure to creating applications using wxPython. With a wide range of topics covered in the book, there are recipes to get the most basic of beginners started in GUI programming as well as tips to help experienced users get more out of their applications. The recipes will take you from the most basic application constructs all the way through to the deployment of complete applications.
Table of Contents (17 chapters)
wxPython Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
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.