How to use the clipboard (Phoenix)

Keywords : Clipboard.


Introduction :

From wikipedia :

In computing, the clipboard is a portion of memory where information that has been copied or cut from a computer application is stored.

This storage is meant as a short-term volatile place to keep information that will be used again shortly.

wx.TheClipboard is a class for manipulating clipboard in wxPython.

First we must call the Open() method to get ownership of the clipboard.

If successful, the method returns True.

Then we put data on the clipboard with the SetData() method.

This method accepts a simple data object.

We have three predefined simple data objects :

To retrieve data from Clipboard you call method GetData().

It accepts simple data object as well.

In the end we close the clipboard with Close() method and relinquish ownership of it.

sample_one.py example shows a simple usage of the the clipboard in wxPython.

(info by ZetCode / Jan Bodnar).


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one

img_sample_one.png

We have two textcontrols and two buttons.

We input some text into the first textcontrol and paste in the the second one with our two buttons.

Notice how we retrieve data from the wxTextDataObject :

text = td.GetText()

   1 # sample_one.py
   2 
   3 """
   4 
   5 Author : Jan Bodnar
   6 Website : zetcode.com
   7 
   8 """
   9 
  10 import wx
  11 
  12 # class MyFrame
  13 # class MyApp
  14 
  15 #---------------------------------------------------------------------------
  16 
  17 class MyFrame(wx.Frame):
  18     def __init__(self, parent, id, title):
  19         wx.Frame.__init__(self, None, -1, title, size=(350, 300))
  20 
  21         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  22 
  23         #------------
  24 
  25         panel1 = wx.Panel(self, -1)
  26 
  27         self.tc1 = wx.TextCtrl(panel1, -1, '', (50,50), (85, -1))
  28         self.tc2 = wx.TextCtrl(panel1, -1, '', (180,50), (85, -1))
  29 
  30         wx.Button(panel1, 1, '&Copy', (50,200))
  31         wx.Button(panel1, 2, '&Paste', (180,200))
  32 
  33         #------------
  34 
  35         self.Bind(wx.EVT_BUTTON, self.OnCopy, id=1)
  36         self.Bind(wx.EVT_BUTTON, self.OnPaste, id=2)
  37 
  38         #------------
  39 
  40         self.Centre()
  41 
  42     #-----------------------------------------------------------------------
  43 
  44     def OnCopy(self, event):
  45         text = self.tc1.GetValue()
  46         if wx.TheClipboard.Open():
  47             wx.TheClipboard.Clear()
  48             wx.TheClipboard.SetData(wx.TextDataObject(text))
  49             wx.TheClipboard.Close()
  50 
  51 
  52     def OnPaste(self, event):
  53         if wx.TheClipboard.Open():
  54             td = wx.TextDataObject()
  55             success = wx.TheClipboard.GetData(td)
  56             wx.TheClipboard.Close()
  57             if not success: return
  58             text = td.GetText()
  59             if text: self.tc2.SetValue(text)
  60 
  61 #---------------------------------------------------------------------------
  62 
  63 class MyApp(wx.App):
  64     def OnInit(self):
  65         frame = MyFrame(None, -1, 'wx.TheClipboard')
  66         frame.Show(True)
  67         self.SetTopWindow(frame)
  68 
  69         return True
  70 
  71 #---------------------------------------------------------------------------
  72 
  73 app = MyApp(0)
  74 app.MainLoop()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Jan Bodnar (sample_one.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

04/01/21 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to use the clipboard (Phoenix) (last edited 2021-01-05 17:56:25 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.