473,394 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 6801
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vlado | last post by:
Hello, I need to copy/paste image from Java application to/from the System clipboard. On windows everything is just fine, but on Mac OS is not. The problem is that when I transfer image to the...
1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF...
3
by: sneffe | last post by:
Hi, Does anyone have an example of how to paste or inserting an image into a rtf file? I have tried the following but the image is represented as a lot of weird letters and numbers FileOpen(1,...
7
by: lgbjr | last post by:
Hello All, Im using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
2
by: sj | last post by:
I am just learning to use Tkinter and am having problems displaying image files. I am able to display an image using tutorials (such as http://www.daniweb.com/code/snippet296.html) But when I try...
2
by: =?Utf-8?B?SkRhdmlkZQ==?= | last post by:
Hello! I'm writing an namespace extension in C++ using ATL. I'm dealing with files which are not on the machine where the NSE is running. I want to provide a way to copy the remote files into the...
12
by: gobblegob | last post by:
I want to paste from clipboard with the F12 key please give some examples because i cannot uderstand any Key Remapping i have read.
5
by: bshul | last post by:
Hi, I am hoping someone knows how to finish this javascript. The script copies selected text to the windows clipboard from, for instance, a webpage. This text is a phone number. It then executes the...
6
by: Jerry Spence1 | last post by:
I am using a network camera that saves the image in a byte array in memory (via their ocx component, and in the OnNewImage event I can get the pointer to the image and the byte length as...
20
by: Joe Duchtel | last post by:
Hello - I have the following code to get a bitmap from the clipboard and to save it to a *.png file ... Dim lData As IDataObject = Clipboard.GetDataObject() If...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.