Retrieving text data from the clipboard

Recovering text data is also rather simple. Again, you need to retrieve and open the clipboard. Once you have the clipboard, call the method GetData(data), where data is an instance of some concrete subclass of wx.DataObject. As we'll see in a moment, the data parameter should not have any actual data of its own when the method is called. This is an unusual getter, in that the return value of the method is not what you would expect—your data. Instead, the return value of this method is True if the data in the clipboard is able to be output in some format compatible with the data object passed in to the method. In this case, since we are passing in a wx.TextDataObject, a True result means that the clipboard data can be converted to plain text. Another way to look at it is if the method returns True, the clipboard data has been placed into the data object. If your data object is not compatible with the format of the data in the clipboard the method returns False. So, boilerplate code to retrieve text data from the clipboard looks like this.

text_data = wx.TextDataObject() if wx.TheClipboard.Open():

success = wx.TheClipboard.GetData(text_data) wx.TheClipboard.Close() if success:

return text_data.GetText()

Note that when you retrieve the data from the clipboard it does not matter which application placed it there. The data in the clipboard itself is managed by the underlying operating system, and wxPython's responsibility is to make sure that the formats match up behind the scenes to ensure that you get only data formats that you can handle.

Was this article helpful?

0 0

Post a comment