The Clipboard is a system-wide, accessible way of getting data to and from one application to another. This recipe will show how to get text from the clipboard, as well as how to put text in the clipboard for other applications to access.
The following two functions can be used to get text from and put text on the clipboard:
def SetClipboardText(text): """Put text in the clipboard @param text: string """ data_o = wx.TextDataObject() data_o.SetText(text) if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open(): wx.TheClipboard.SetData(data_o) wx.TheClipboard.Close() def GetClipboardText(): """Get text from the clipboard @return: string """ 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
wxPython provides a singleton clipboard object that can be used to interact with the systems clipboard. This class works with data objects that are used to represent the underlying system data types. The use of the clipboard is a three-step process:
Open the Clipboard
Set/Get the DataObject
Close the Clipboard
The clipboard supports many other data types besides just text. wxPython provides built-in support for some additional types, as well as classes for defining your own custom types. The usage of these different data types follows the same general pattern as the TextDataObject
.
Data types |
Description |
---|---|
Used to get Bitmaps from and put Bitmaps on the Clipboard | |
Can hold any Python picklable data type | |
Can contain any arbitrary number of simple data types and make them all available at once | |
Used for holding filenames | |
Used for holding URLs |