Documentation for using the system clipboard ?

Eric Brunel eric.brunel at pragmadev.com
Wed Mar 5 04:11:09 EST 2003


Frank wrote:
> I haven't been able to find documentation on how to use the system
> clipboard from Python/Tk.

Me neither...

> Could someone point me to the documentation
> for this feature please.  I would like to use it in code portable to
> Win32, Linux, and Solaris.

Since you mention Linux and Solaris, I suppose you just want to copy/paste text? 
I don't know any "standard" way to copy/paste graphics with XWindows, so the 
following code will only work for text:

## To copy
<any Tkinter widget>.clipboard_clear()
<any Tkinter widget>.clipboard_append("Text to copy", type='STRING')

## To paste
text2Paste = <any Tkinter widget>.selection_get(
                    selection='CLIPBOARD', type='STRING')

This should work with Windows and XWindows, but will have slightly different 
behaviours: on Windows, the clipboard is a container, so the copied text will 
remain there even if your application is closed. On XWindows, the clipboard just 
holds a reference to a text managed by your application (sort of...), so the 
copied text won't be available anymore if your application is closed. This has 
nothing to do with Python or Tkinter or Tk: it's just the way the clipboard 
works on Windows and XWindows.

Please note also that for some reason, some Unix applications do not use the 
standard clipboard to do their copy/paste, so don't be surprised if the text you 
copied cannot be pasted everywhere with the standard Edit->Paste. To check that 
the text has actually been copied, you can use the xclipboard utility.

HTH
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com





More information about the Python-list mailing list