Copy text to clipboard. Explicitly uses CLIPBOARD selection, not PRIMARY. On X11 systems, wxPython can confuse between PRIMARY and CLIPBOARD selections, causing "already open" errors. This function ensures we always use CLIPBOARD. See: https://discuss.wxpython.org/t/wx-thecl
(text)
| 6 | |
| 7 | |
| 8 | def toClipboard(text): |
| 9 | """ |
| 10 | Copy text to clipboard. Explicitly uses CLIPBOARD selection, not PRIMARY. |
| 11 | |
| 12 | On X11 systems, wxPython can confuse between PRIMARY and CLIPBOARD selections, |
| 13 | causing "already open" errors. This function ensures we always use CLIPBOARD. |
| 14 | |
| 15 | See: https://discuss.wxpython.org/t/wx-theclipboard-pasting-different-content-on-every-second-paste/35361 |
| 16 | """ |
| 17 | clipboard = wx.TheClipboard |
| 18 | try: |
| 19 | # Explicitly use CLIPBOARD selection, not PRIMARY selection |
| 20 | # This prevents X11 confusion between the two clipboard types |
| 21 | clipboard.UsePrimarySelection(False) |
| 22 | |
| 23 | if clipboard.Open(): |
| 24 | try: |
| 25 | data = wx.TextDataObject(text) |
| 26 | clipboard.SetData(data) |
| 27 | return True |
| 28 | finally: |
| 29 | clipboard.Close() |
| 30 | else: |
| 31 | logger.debug("Failed to open clipboard for writing") |
| 32 | return False |
| 33 | except Exception as e: |
| 34 | logger.warning("Error writing to clipboard: {}", e) |
| 35 | return False |
| 36 | |
| 37 | |
| 38 | def fromClipboard(): |
no outgoing calls
no test coverage detected