Problem phenomenon:
By breaking points, step-by-step debugging can copy and paste clipboard data normally. But the direct operation will be error Pywintypes.error: (1418, ' GetClipboardData ', the thread does not open the Clipboard)
Cause of the problem:< Span style= "COLOR: #cc7832" > < Span style= "COLOR: #cc7832" > < Span style= "COLOR: #6a8759" >
The reason is to run directly, There is no way to control the order in which the main thread runs, it may appear that settext () has not closed the Clipboard, and GetText () has opened the Clipboard. But after the break, you can do it in the order that you wrote in the code.
/span>
Workaround:
After calling Clipboard.settext (content), be sure to add Time.sleep (3)
This pit stuck me for a few days ...
The source code is as follows:
#encoding = Utf-8
Import Win32clipboardAs W
Import Win32con, Time
Class Clipboard (Object):
@Staticmethod
DefGetText ():
W.openclipboard ()
D = W.getclipboarddata (Win32con. Cf_unicodetext)
W.closeclipboard ()
Return D
@staticmethod
def SetText (astring):
W.openclipboard ()
W.emptyclipboard ()
W.setclipboarddata (Win32con. Cf_unicodetext, astring)
W.closeclipboard ()
if __name__ = = "__main__":
Content = "Xiaohuhu"
Clipboard.settext (content)
Time.sleep (3) #一定一定要加暂停, otherwise it will prompt Pywintypes.error: (1418, ' GetClipboardData ', the thread does not have the Clipboard Open)
A = Clipboard.gettext ()
print (a)
Python action Clipboard error prompt: Pywintypes.error: (1418, ' getclipboarddata ', thread does not have an open clipboard)