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

Accessing the clipboard


The clipboard is a system resource used to pass user data between applications in the operating system. This is most often associated with copying and pasting actions in an application. This recipe will show some basics on putting and getting data from the clipboard.

How to do it…

  1. Let's first define a helper function to get some text from the clipboard, as follows:

    def GetClipboardText():
        text_obj = wx.TextDataObject()
        rtext = ""
        if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open():
      if wx.TheClipboard.GetData(text_obj):
        rtext = text_obj.GetText()
        wx.TheClipboard.Close()
        return rtext
  2. Now, let's do the reverse and define a helper function to put text into the clipboard, as done with the following code:

    def SetClipboardText(text):
        data_o = wx.TextDataObject()
        data_o.SetText(text)
        if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open():
            wx.TheClipboard.SetData(data_o)
            wx.TheClipboard.Close()

How it works…

Both functions work by creating TextDataObject, which provides a platform-independent way to represent the systems' native data format. Then, TheClipboard object is opened, and it is used to either get data from the clipboard of the given type or put data in the clipboard from the application. This can be boiled down to a simple three step process for any clipboard interaction:

  1. Open clipboard

  2. Set or get DataObject

  3. Close the clipboard

Closing the clipboard after using is very important; it may prevent other processes from accessing it. The clipboard should only be kept open momentarily.

There's more…

The clipboard supports many other datatypes besides plain text, which can be used based on the situation and needs of the application.

Datatypes

Description

wx.BitmapDataObject

This is a bitmap data from the clipboard (drag and drop)

wx.CustomDataObject

This is a base class to represent application-specific data

wx.DataObjectSimple

This is a base class to create other data object types

wx.DataObjectComposite

This is a base class to support multiple formats

wx.FileDataObject

This is the data object for filenames

wx.HTMLDataObject

This is the HTML-formatted text container

wx.URLDataObject

This is the URL container data object

See also

  • Take a look at the next recipe in this chapter, Supporting drag and drop, for more on how clipboard data objects can be used to transfer data between controls.