[python-uk] Windows Clipboard Module

John Sampson jrs.idx at ntlworld.com
Wed Jun 29 04:52:39 EDT 2016


On 28/06/2016 18:45, Pete Forman wrote:
> John Sampson <jrs.idx at ntlworld.com> writes:
>
>> I am using win32clipboard module with Python 2.7 in Windows 7. I have
>> code which uses GetClipboardData() and SetClipboardText(argument).
>> Before running the code I copy text into the Windows clipboard from a
>> proprietary program. I know that this program adds garbage to the end
>> of the string, namely null-value bytes and possibly other invisible
>> stuff.
>>
>> When I paste the contents of the Windows clipboard into a Tkinter text
>> box I get the string obtained by GetClipboardData() although I have
>> since run SetClipboardText(argument).
>>
>> If I paste the contents of the Windows clipboard at this stage into
>> the Boxer text editor I get the string placed by
>> SetClipboardText(argument). If I paste into the Tkinter program again,
>> or into Vim or into Microsoft Word, I get the string obtained using
>> GetClipboardData(). I can repeat this as many times as I like - one
>> string into Boxer, the other string into the other programs.
>>
>> I deduce that both strings must be in the clipboard at the same time,
>> but it depends which program has focus which string is pasted in using
>> Control v.
>>
>> Is there a way of showing the contents of the clipboard, garbage and
>> all? I would like to find a way of making the Windows clipboard behave
>> as it is supposed to - new contents overwriting the old.
> I don't have access to a Windows PC at the moment but here are a couple
> of scripts I wrote 9 years ago to play with the clipboard.
>
> Bear in mind that the clipboard is not just text. It is written to in a
> number of formats and is designed that the reader pull off the best,
> whatever that might be.
>
> There are a number of clipboard viewer or manager applications available
> on Windows. Something to watch out for is that clipboard behaviour may
> be affected by adding viewers.
>
[clip]

Many thanks. What has fixed it for a newbie like me was to empty the 
clipboard explicitly with
EmptyClipboard() before running SetClipboardText(argument). I did not 
appreciate that programs draw off their selection from the clipboard 
when pasting.


More information about the python-uk mailing list