469,364 Members | 2,332 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

Pasting an image from clipboard in Tkinter?

As many might know, windows allows to copy an image into the clipboard
by pressing the "Print Screen" button on the keyboard. Is it possible
to paste such an image from the clipboard into a "Text" widget in
Tkinter? Here is my first attempt with just trying to print out the
image data:

-----------------
def pasteImg(tgt):
global clipboardEnabled
if not clipboardEnabled: return

win32clipboard.OpenClipboard(0)
print win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
-----------------

This works fine with selecting text, but comes up with the following
error when trying to paste an image:

-----------------
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "X:\development\testing\tkwiki\tkwiki.py", line 52, in <lambda>
Button( root, command=lambda: pasteImg(txt) ).pack()
File "X:\development\testing\tkwiki\tkwiki.py", line 38, in pasteImg
print win32clipboard.GetClipboardData()
TypeError: Specified clipboard format is not available
-----------------

Obviously the clipboard does not know about that format. Does that
mean I have to wait until it's implemented or are there other ways to
access the image data?

Jun 11 '07 #1
5 5980
On Jun 11, 7:23 am, "exhuma.twn" <exh...@gmail.comwrote:
As many might know, windows allows to copy an image into the clipboard
by pressing the "Print Screen" button on the keyboard. Is it possible
to paste such an image from the clipboard into a "Text" widget in
Tkinter? Here is my first attempt with just trying to print out the
image data:

-----------------
def pasteImg(tgt):
global clipboardEnabled
if not clipboardEnabled: return

win32clipboard.OpenClipboard(0)
print win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
-----------------

This works fine with selecting text, but comes up with the following
error when trying to paste an image:

-----------------
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "X:\development\testing\tkwiki\tkwiki.py", line 52, in <lambda>
Button( root, command=lambda: pasteImg(txt) ).pack()
File "X:\development\testing\tkwiki\tkwiki.py", line 38, in pasteImg
print win32clipboard.GetClipboardData()
TypeError: Specified clipboard format is not available
-----------------

Obviously the clipboard does not know about that format. Does that
mean I have to wait until it's implemented or are there other ways to
access the image data?

I don't think you can paste to a text widget, but I could be mistaken.
This link talks about pasting an image into a window, but I don't
think it's really what you want...however, it might give you some
ideas:

http://effbot.org/zone/wck-3.htm (see the "Drawing Images" section)

This link also talks about some of the same things:
http://www.wadsworth.org/spider_doc/...b/tkinter.html

If I understand them correctly, it sounds like you could possibly
catch the the paste operation and convert the image to a TkImage and
then paste it. I think that since it is in the clipboard, then it is a
file object and this may work. I just don't know how you intercept a
paste.

Mike

Jun 11 '07 #2
On Jun 11, 3:47 pm, kyoso...@gmail.com wrote:
On Jun 11, 7:23 am, "exhuma.twn" <exh...@gmail.comwrote:
As many might know, windows allows to copy an image into the clipboard
by pressing the "Print Screen" button on the keyboard. Is it possible
to paste such an image from the clipboard into a "Text" widget in
Tkinter? Here is my first attempt with just trying to print out the
image data:
-----------------
def pasteImg(tgt):
global clipboardEnabled
if not clipboardEnabled: return
win32clipboard.OpenClipboard(0)
print win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
-----------------
This works fine with selecting text, but comes up with the following
error when trying to paste an image:
-----------------
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "X:\development\testing\tkwiki\tkwiki.py", line 52, in <lambda>
Button( root, command=lambda: pasteImg(txt) ).pack()
File "X:\development\testing\tkwiki\tkwiki.py", line 38, in pasteImg
print win32clipboard.GetClipboardData()
TypeError: Specified clipboard format is not available
-----------------
Obviously the clipboard does not know about that format. Does that
mean I have to wait until it's implemented or are there other ways to
access the image data?

I don't think you can paste to a text widget, but I could be mistaken.
This link talks about pasting an image into a window, but I don't
think it's really what you want...however, it might give you some
ideas:

http://effbot.org/zone/wck-3.htm (see the "Drawing Images" section)

This link also talks about some of the same things:http://www.wadsworth.org/spider_doc/...spipylib/tkint...

If I understand them correctly, it sounds like you could possibly
catch the the paste operation and convert the image to a TkImage and
then paste it. I think that since it is in the clipboard, then it is a
file object and this may work. I just don't know how you intercept a
paste.

Mike
Unfortunately, when they talk about "pasting" they talk about a PIL
method called paste, which (if I understood correctly) deals with
blitting one image onto another. Not "pasting" as in copy/paste from
clipboard.

Jun 11 '07 #3
On Jun 11, 9:07 am, "exhuma.twn" <exh...@gmail.comwrote:
On Jun 11, 3:47 pm, kyoso...@gmail.com wrote:
On Jun 11, 7:23 am, "exhuma.twn" <exh...@gmail.comwrote:
As many might know, windows allows to copy an image into the clipboard
by pressing the "Print Screen" button on the keyboard. Is it possible
to paste such an image from the clipboard into a "Text" widget in
Tkinter? Here is my first attempt with just trying to print out the
image data:
-----------------
def pasteImg(tgt):
global clipboardEnabled
if not clipboardEnabled: return
win32clipboard.OpenClipboard(0)
print win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
-----------------
This works fine with selecting text, but comes up with the following
error when trying to paste an image:
-----------------
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "X:\development\testing\tkwiki\tkwiki.py", line 52, in <lambda>
Button( root, command=lambda: pasteImg(txt) ).pack()
File "X:\development\testing\tkwiki\tkwiki.py", line 38, in pasteImg
print win32clipboard.GetClipboardData()
TypeError: Specified clipboard format is not available
-----------------
Obviously the clipboard does not know about that format. Does that
mean I have to wait until it's implemented or are there other ways to
access the image data?
I don't think you can paste to a text widget, but I could be mistaken.
This link talks about pasting an image into a window, but I don't
think it's really what you want...however, it might give you some
ideas:
http://effbot.org/zone/wck-3.htm(see the "Drawing Images" section)
This link also talks about some of the same things:http://www.wadsworth.org/spider_doc/...spipylib/tkint...
If I understand them correctly, it sounds like you could possibly
catch the the paste operation and convert the image to a TkImage and
then paste it. I think that since it is in the clipboard, then it is a
file object and this may work. I just don't know how you intercept a
paste.
Mike

Unfortunately, when they talk about "pasting" they talk about a PIL
method called paste, which (if I understood correctly) deals with
blitting one image onto another. Not "pasting" as in copy/paste from
clipboard.
I don't do much with images as of yet, but from what I've read, it
seems that blitting is a common method. Here's a fairly interesting
article on the process using wxPython (sorry...I wasn't finding
anything under Tkinter):

http://wiki.wxpython.org/index.cgi/WorkingWithImages

It might give some ideas.

Mike

Jun 11 '07 #4
On Mon, 11 Jun 2007 14:23:48 +0200, exhuma.twn <ex****@gmail.comwrote:
As many might know, windows allows to copy an image into the clipboard
by pressing the "Print Screen" button on the keyboard. Is it possible
to paste such an image from the clipboard into a "Text" widget in
Tkinter? Here is my first attempt with just trying to print out the
image data:

-----------------
def pasteImg(tgt):
global clipboardEnabled
if not clipboardEnabled: return

win32clipboard.OpenClipboard(0)
print win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
-----------------

This works fine with selecting text, but comes up with the following
error when trying to paste an image:

-----------------
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "X:\development\testing\tkwiki\tkwiki.py", line 52, in <lambda>
Button( root, command=lambda: pasteImg(txt) ).pack()
File "X:\development\testing\tkwiki\tkwiki.py", line 38, in pasteImg
print win32clipboard.GetClipboardData()
TypeError: Specified clipboard format is not available
-----------------

Obviously the clipboard does not know about that format. Does that
mean I have to wait until it's implemented or are there other ways to
access the image data?
According to http://msdn2.microsoft.com/en-us/library/ms649039.aspx, there
is format that you should pass to GetClipboardData telling the data type
you expect to get. The format you should specify to get a bitmap image is
named CF_BITMAP in the Windows API. AFAIK, this constant is not exposed in
the Python world, so you have to pass directly the numeric value, which is
2.

But even if you do get the clipboard contents, you'll get it in BMP
format, that tk/Tkinter does not understand by default. So you'll need a
means to convert it to the only format known to tk/Tkinter by default,
which is GIF. PIL is certainly able to do that (I don't use it myself);
you may also rely on an external conversion utility.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Jun 12 '07 #5
On Jun 12, 11:24 am, "Eric Brunel" <see.signat...@no.spamwrote:
On Mon, 11 Jun 2007 14:23:48 +0200,exhuma.twn <exh...@gmail.comwrote:
As many might know, windows allows to copy an image into the clipboard
by pressing the "Print Screen" button on the keyboard. Is it possible
to paste such an image from the clipboard into a "Text" widget in
Tkinter? Here is my first attempt with just trying to print out the
image data:
-----------------
def pasteImg(tgt):
global clipboardEnabled
if not clipboardEnabled: return
win32clipboard.OpenClipboard(0)
print win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
-----------------
This works fine with selecting text, but comes up with the following
error when trying to paste an image:
-----------------
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "X:\development\testing\tkwiki\tkwiki.py", line 52, in <lambda>
Button( root, command=lambda: pasteImg(txt) ).pack()
File "X:\development\testing\tkwiki\tkwiki.py", line 38, in pasteImg
print win32clipboard.GetClipboardData()
TypeError: Specified clipboard format is not available
-----------------
Obviously the clipboard does not know about that format. Does that
mean I have to wait until it's implemented or are there other ways to
access the image data?

According tohttp://msdn2.microsoft.com/en-us/library/ms649039.aspx, there
is format that you should pass to GetClipboardData telling the data type
you expect to get. The format you should specify to get a bitmap image is
named CF_BITMAP in the Windows API. AFAIK, this constant is not exposed in
the Python world, so you have to pass directly the numeric value, which is
2.

But even if you do get the clipboard contents, you'll get it in BMP
format, that tk/Tkinter does not understand by default. So you'll need a
means to convert it to the only format known to tk/Tkinter by default,
which is GIF. PIL is certainly able to do that (I don't use it myself);
you may also rely on an external conversion utility.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Well, by inspecting the clipboard contents, I already suspected it
might be "2". So I did a getData with the format set to "2".
So, when I try to paste simple text, it does complain that the
requested format is not on the clipboard. So far, correct. So I press
the print screen button, which loads an image into th clipboard. When
pasting I get this:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "X:\development\testing\tkwiki\tkwiki.py", line 55, in <lambda>
Button( root, command=lambda: pasteImg(txt) ).pack()
File "X:\development\testing\tkwiki\tkwiki.py", line 39, in pasteImg
print win32clipboard.GetClipboardData(2)
error: (6, 'GetClipboardData:GlobalLock', 'The handle is invalid.')

I'm not sure what this means.
.... Ah... bugger. It won't work (yet):
http://mail.python.org/pipermail/pyt...ay/000056.html

Jun 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Vlado | last post: by
3 posts views Thread by sneffe | last post: by
7 posts views Thread by lgbjr | last post: by
2 posts views Thread by sj | last post: by
2 posts views Thread by =?Utf-8?B?SkRhdmlkZQ==?= | last post: by
6 posts views Thread by Jerry Spence1 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.