Pasting an image from clipboard in Tkinter?

kyosohma at kyosohma at
Mon Jun 11 15:47:18 CEST 2007

On Jun 11, 7:23 am, "exhuma.twn" <exh... at> wrote:
> 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\", line 1403, in __call__
>     return self.func(*args)
>   File "X:\development\testing\tkwiki\", line 52, in <lambda>
>     Button( root, command=lambda: pasteImg(txt) ).pack()
>   File "X:\development\testing\tkwiki\", 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:  (see the "Drawing Images" section)

This link also talks about some of the same things:

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


More information about the Python-list mailing list