1

I have a Python TUI application and I want it to save the result of a user selection from the keyboard.

It's pretty easy to find a Windows and Mac API to copy something to the user clipboard programmatically, but how to do so in Linux from Python?

It needs to:

  • work accross desktop environments: at least Gnome Shell and KDE.
  • work on X11 and Wayland.
  • work without installing a third party package from the Linux repo (such as xclip) since you can't pip install xclip. This means the pypi clipboard package is excluded since it relies on that.
  • it works without loading a GUI widget that requires the user to select the content, since the copy may happen in the background or in a TUI. The text may not be selected with the mouse.
  • it should not install GTK, QT, or other heavy dependency that wouldn't be used for anything else. Installing a pure python 3rd party package is ok.

xclip does it for XWindows, so there muse be an API for that. And wl-clipboard for Wayland allows it, so there must be some C lib I can call using ctypes maybe.

Bite code
  • 578,959
  • 113
  • 301
  • 329
  • Can this help ? https://stackoverflow.com/questions/3902263/how-can-python-access-the-x11-clipboard – Philippe Sep 28 '21 at 20:57
  • No, unfortunatly, the answers either use the clipboard lib, which uses a third party packages under the hood, or a GUI context, and my application doesn't have a GUI. I'll update the answer with this last part. In Windows and Mac, it's fairly simple to do, but I assume I'll have to ctype my way toward it under linux with variants accross DE. – Bite code Sep 28 '21 at 21:16
  • If you want to use X11 anyway, whether your application _displays_ a GUI is unimportant. If using only native Python code is important, perhaps edit the title to highlight this. – tripleee Oct 01 '21 at 09:06
  • Ok, I've done so. – Bite code Oct 02 '21 at 09:13

0 Answers0