Getting and setting the clipboard in Windows

S

Stephen Boulet

I'm using the following code in clipboard.py. The problem is that it
sets the clipboard text correctly sometimes and sometimes doesn't alter
it at all. What am I doing wrong?

import win32clipboard,sys

def getclipboard():
.....# Get current string from clipboard. If string empty, error out.
.....win32clipboard.OpenClipboard(0)
.....if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_TEXT):
.........s = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
.........win32clipboard.CloseClipboard()
.....else:
.........win32clipboard.CloseClipboard()
.........from wxMessageBox import showerror
.........showerror('Error!','No text on clipboard')
.........sys.exit()
.....return s

def setclipboard(s):
.....# Paste string to clipboard
.....win32clipboard.OpenClipboard(0)
.....win32clipboard.EmptyClipboard()
.....win32clipboard.SetClipboardText(s)
.....win32clipboard.CloseClipboard()

Stephen
 
M

Michel Claveau - abstraction méta-galactique non t

Hi !


This code run, on my CPU :


from win32clipboard import *
import win32con

if __name__=='__main__':
OpenClipboard()
txt=GetClipboardData(win32con.CF_TEXT)
CloseClipboard()
print txt

OpenClipboard()
EmptyClipboard()
SetClipboardText("AZERTYUIOP123")
CloseClipboard()


@-salutations
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top