windows - Python win32clipboard recursive call not working as expected (but actually better) -
i made code
import win32clipboard cb # used windows clipboard content def gettext(): errormsg = '''no text has been copied clipboard. copy text clipboard , press enter:''' # text in clipboard cb.openclipboard() text = cb.getclipboarddata() cb.closeclipboard() if text == errormsg: raw_input(errormsg) text = gettext() # recursive call cb.openclipboard() cb.setclipboardtext(errormsg) cb.closeclipboard() return text
if copy "hello world" clipboard , call gettext() twice get:
>>> print gettext() hello world >>> print gettext() no text has been copied clipboard. copy text clipboard , press ok: [copied "hello" , pressed enter] hello
now if try ctrl-v (paste) text editor "hello" - amazing, not expected. expected have errormsg in clipboard. keeping "hello" in clipboard , call gettext() again still prompts user copy content clipboard.
i don't want change behavior of code, understand it
note code you've given won't run is. believe line:
if ocrtext == errormsg:
should be:
if text == errormsg:
additionally when write clipboard should this:
cb.openclipboard() cb.emptyclipboard() cb.setclipboardtext(errormsg) cb.closeclipboard()
i.e., need call emptyclipboard
before setting clipboard data. when make these changes, appears working described, error message in clipboard.
Comments
Post a Comment