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

Getting the XP clipboard in tkinter

4 views
Skip to first unread message

Edward K. Ream

unread,
Jun 30, 2002, 10:49:38 AM6/30/02
to
Hi,

If a clipboard_get() convenience method existed in tkinter, what would
it look like?

I would like to do something that seems trivial using Tkinter:
namely get the contents of the "global" clipboard in Windows XP. When
the focus in a text widget my app uses virtual Cut, Copy and Paste
events, like this:

w.event_generate(virtual_event_name("Copy"))

This works. That is, the text copied in this manner can be read by my
app and can be pasted by my app using the similar "Paste" event.

However, there are times when I need to get this same information when
the focus is in a canvas, and I just don't see how to do it. In such
cases, my app writes to the clipboard using:

app().root.clipboard_clear()
app().root.clipboard_append(s)

where app().root is a top level widget. Other apps can read what has
been written to the clipboard! But the obvious code to read the
clipboard:

s = app().root.selection_get()

takes an exception. I must be missing something really simple. The
following two statements also take exceptions:

s = app().root.selection_get("clipboard")
s = app().root.selection_get(".")

Please help. This is driving me crazy.

Edward
--------------------------------------------------------------------
Edward K. Ream email: edr...@tds.net
Leo: Literate Editor with Outlines
Leo: http://personalpages.tds.net/~edream/front.html
--------------------------------------------------------------------

Matthias Huening

unread,
Jul 1, 2002, 2:07:55 AM7/1/02
to
"Edward K. Ream" <edr...@tds.net> wrote in
news:3D1F1A81...@tds.net:

> The
> following two statements also take exceptions:
>
> s = app().root.selection_get("clipboard")
> s = app().root.selection_get(".")
>

Did you try this:
root.selection_get(selection="CLIPBOARD")

Worked for me...

Matthias

Brian Kelley

unread,
Jul 1, 2002, 9:03:27 AM7/1/02
to
Just in case you are using windows:

# if we are running on windows, attach to the
# windows clipboard
import win32clipboard as clip

def getFromClipboard():
'Returns a list of the lines of text in the clipboard.'
clip.OpenClipboard(0)
if clip.IsClipboardFormatAvailable(clip.CF_TEXT):
newText = clip.GetClipboardData(clip.CF_TEXT)
lines = newText.split('\r\n')
clip.CloseClipboard()
return lines

Brian Kelley
Whitehead Institute for Biomedical Research

Edward K. Ream

unread,
Jul 1, 2002, 9:33:03 AM7/1/02
to
Matthias Huening wrote:

> > The
> > following two statements also take exceptions:
> >
> > s = app().root.selection_get("clipboard")
> > s = app().root.selection_get(".")

> Did you try this:
> root.selection_get(selection="CLIPBOARD")
>
> Worked for me...

Yes! selection="CLIPBOARD" works. selection="clipboard" does not.
Somehow I was blind to case. Thanks very much for your help.

0 new messages