Python action Clipboard error prompt: Pywintypes.error: (1418, ' getclipboarddata ', thread does not have an open clipboard)

Source: Internet
Author: User
Tags add time gettext

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.