Pasting an image from clipboard in Tkinter?

exhuma.twn exhuma at gmail.com
Mon Jun 11 14:23:48 CEST 2007


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?




More information about the Python-list mailing list