5

Is it possible for python to clear the clipboard? If so ... how can I do it?

I need this so in my quiz program, students can't copy paste answers from the internet and other files.

EDIT: Im using WinXP and Python 2.6

juliomalegria
  • 24,229
  • 14
  • 73
  • 89
Katherina
  • 2,153
  • 7
  • 26
  • 34
  • 2
    ¬¬ The will still be able to write them down, don't they? – OscarRyz Feb 03 '12 at 02:57
  • similar question, same basic answer: [How do I read text from the (windows) clipboard from python?](http://stackoverflow.com/questions/101128/how-do-i-read-text-from-the-windows-clipboard-from-python) – kmote Feb 03 '12 at 02:58
  • @OscarRyz they can type in their answers – Katherina Feb 03 '12 at 03:06
  • @kmote hi, im having this error when importing win32clipboard. ImportError: DLL load failed: The specified module could not be found.... have any idea? already download pywin32 from here http://sourceforge.net/projects/pywin32/files/pywin32/Build216/pywin32-216.win32-py2.6.exe/download – Katherina Feb 03 '12 at 03:11

2 Answers2

11
from ctypes import windll
if windll.user32.OpenClipboard(None):
    windll.user32.EmptyClipboard()
    windll.user32.CloseClipboard()

No external libraries needed.

yak
  • 8,851
  • 2
  • 29
  • 23
1

Yes you can for that you have to use PyWin32 module which is a python module for windows ectension.

Take a look at its EmptyClipboard method.

The EmptyClipboard function empties the clipboard and frees handles to data in the clipboard. The function then assigns ownership of the clipboard to the window that currently has the clipboard open.

RanRag
  • 48,359
  • 38
  • 114
  • 167
  • Hi i download pyWin32. how do i import it please? im having error when using import win32clipboard, win32con, random. im getting this ImportError: DLL load failed: The specified module could not be found. – Katherina Feb 03 '12 at 03:07
  • @Katherina: which version of PyWin32you downloaded.Download the one corresponding to Python2.6 – RanRag Feb 03 '12 at 04:21
  • yes i did download the installer for python 2.6. idk why it game me that error on import. anyway ill try to find it out again. – Katherina Feb 03 '12 at 07:36