Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Documentation for using the system clipboard ?

817 views
Skip to first unread message

Frank

unread,
Mar 5, 2003, 2:36:43 AM3/5/03
to
I haven't been able to find documentation on how to use the system
clipboard from Python/Tk. 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.
Frank

Eric Brunel

unread,
Mar 5, 2003, 4:11:09 AM3/5/03
to
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....@pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

jodocus

unread,
Mar 5, 2003, 10:36:02 AM3/5/03
to
On Wed, 05 Mar 2003 10:11:09 +0100, Eric Brunel wrote:


> 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.

Are you really sure about this? I have never noticed any text vanishing
from the X selection after closing an application. Also, I can't recall
ever reading about that in any discussion about how selections should be
implemented in X.

R.

Grant Edwards

unread,
Mar 5, 2003, 11:17:34 AM3/5/03
to
In article <pan.2003.03.05.16...@zonnet.nl>, jodocus wrote:
> On Wed, 05 Mar 2003 10:11:09 +0100, Eric Brunel wrote:

>> 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.
>
> Are you really sure about this?

Well, like Eric said: Sort of.

It depends on the apps involved.

Under X, there are selections and there are cut buffers.

A "selection" doesn't contain any data, it's just an abstract
object the only useful property of which is who owns it. When
you want to paste from a selection, you tell the X server. The
X server sends an event to the owner of the selection telling
it to cough up some data and send it to you. If that app isn't
running anymore (or just chooses to ignore the request), you
get no data.

A cut buffer OTOH, is a global container that actually can
contain data. Some apps also copy the selected data to a cut
buffer when a selection is made. Correspondingly, some apps
when told to paste will try to fetch the selection data first,
and if that fails they'll use the contents of the cut buffer as
a fallback.

> I have never noticed any text vanishing from the X selection
> after closing an application. Also, I can't recall ever reading
> about that in any discussion about how selections should be
> implemented in X.

If you're talking strictly about X11 selections, then yes,
they're only valid as long as the app that last owned the
selection is alive and willing to send data. If an app grabs a
selection then dies, there's no selection owner from which the
X server can request data when you want to get the "contents"
of the selection.

However, most (but not all) X apps use a cut buffer as a
"fallback" mechanism so that you can get selected data from a
no-longer living selection owner.

--
Grant Edwards grante Yow! Yow! We're going to
at a new disco!
visi.com

Paul Foley

unread,
Mar 6, 2003, 8:41:51 PM3/6/03
to
On Wed, 05 Mar 2003 10:11:09 +0100, Eric Brunel wrote:

>> 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')

In fact, it only works for Latin-1 text with no control characters.
If you want it to work for other types, don't use STRING. E.g., you
can use type PIXMAP if you want graphics. [I don't know how you do
that from Tk, though. If you use type PIXMAP, the data should be the
pixmap's resource ID on the X server, not the content]

There's nothing limiting you to transferring plain text on X, any more
than on Windows (less, in fact; the X selection mechanism is *far*
more powerful than the Windows clipboard)

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

Try type='TARGETS'; that should get you a list of target types that
the selection owner is willing to provide. Pick the one that you
want, and ask for that (it's possible to ask for several types at once
with type MULTIPLE, but I don't know how you'd go about telling it
what types you want, or where to put the results, using the interface
provided by Tkinter)

--
Qui beneficium dedit, taceat; narret qui accepit -- Seneca

(setq reply-to
(concatenate 'string "Paul Foley " "<mycroft" '(#\@) "actrix.gen.nz>"))

Frank

unread,
Mar 6, 2003, 10:41:10 PM3/6/03
to
Thanks to all: Eric, jodocus, Grant and Paul, for your help.
I figured it was time for a good book anyway, so I got Mark Lutz's
"Programming Python, 2nd". He has a paragraph (8.4.2.1) about the
clipboard, and uses it in his pyEdit example code. However, nothing
compares to the details and warnings I found in this group.

Thanks,
Frank

0 new messages