Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python win32clipboard递归调用未按预期工作(但实际上更好)_Python_Windows_Pywin32_Win32com - Fatal编程技术网

Python win32clipboard递归调用未按预期工作(但实际上更好)

Python win32clipboard递归调用未按预期工作(但实际上更好),python,windows,pywin32,win32com,Python,Windows,Pywin32,Win32com,我做了这个密码 import win32clipboard as cb # Used to get the windows clipboard content def getText(): errorMsg = '''No text has been copied to the clipboard. Copy text to the clipboard and press ENTER:''' # The text is in the clipboard cb.

我做了这个密码

import win32clipboard as cb # Used to get the windows clipboard content

def getText():
    errorMsg = '''No text has been copied to the clipboard.
    Copy text to the clipboard and press ENTER:'''

    # The text is in the 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
如果我将“Hello world”复制到剪贴板并调用getText()两次,我会得到:

>>> print getText()
Hello world
>>> print getText()
No text has been copied to the clipboard.
Copy text to the clipboard and press OK: [Copied "Hello" and pressed ENTER]
Hello
现在,如果我尝试CTRL-V(粘贴)到另一个文本编辑器中,我会得到“Hello”-这很神奇,但不是我所期望的。我希望在我的剪贴板中有errorMsg。在剪贴板中保留“hello”并再次调用getText(),仍然会提示用户将内容复制到剪贴板


我不想更改代码的行为,但想了解它

注意,您给出的代码不会按原样运行。我相信这句话:

if ocrText == errorMsg:
实际上应该是:

if text == errorMsg:
此外,当您写入剪贴板时,应执行以下操作:

cb.OpenClipboard()
cb.EmptyClipboard()
cb.SetClipboardText(errorMsg)
cb.CloseClipboard()

即,在设置剪贴板数据之前,需要调用
emptycipboard
。当我进行这些更改时,它似乎按照您所描述的那样工作,错误消息在剪贴板中。

对此表示抱歉。在另一个上下文中使用了代码,并在这里对其进行了修改,以便更容易理解问题的内容。我现在已经更正了代码。我感谢您的回答,但它没有解释为什么代码检测到自上次调用以来剪贴板上没有添加任何新内容。事件虽然我粘贴时剪贴板中不存在errorMsg,但它必须以某种方式存在-否则if语句将失败我不确定-我认为这归结为
win32clipboard
-几乎有两个剪贴板-它自己的内部剪贴板和Windows剪贴板。在您的代码中,您从未实际设置窗口剪贴板,只设置了
win32clipboard
。使用
cb.EmptyClipboard()