I’m working on a an app (my 2nd GUI app) that generates a hash value (AKA: checksum) based on a selected file. Right now, the hash value is displayed in a Text Widget, which is has the ‘state’ set to ‘disabled’, so that said value can’t be messed with. I’d like to be able to copy that to the system clipboard without using yet another dependency. I’ve seen some methods posted in a number of places, but they all require at least one dependency (mostly ‘pyperclip’) or a ‘sub process’.
Is there any way to do this using the standard Python library, please? I really don’t want to have to do some pip install for such a simple operation, as I can use a work around; it’s just a little untidy, so to speak.
Thanks.
edit: I should have said, I’m using a Linux based OS.
“Copy to the clipboard” is one of those things that sound easy until you actually try to do it, like “let’s fly to the moon” or “why can’t everyone just get along?”.
Especially on Linux. From the project notes for pyperclip:
“On Linux, this module makes use of the xclip or xsel commands, which should come with the os. Otherwise run “sudo apt-get install xclip” or “sudo apt-get install xsel” (Note: xsel does not always seem to work.) Otherwise on Linux, you will need the gtk or PyQt4 modules installed.”
So to get copy and pasting to work on Linux, you have to be prepared to try four different techniques (with at least one of them allegedly unreliable).
If you are sure that this app of yours is only for personal use and won’t be used by people on other flavours of Linux, or MacOS, or Windows, or other non-Linux unixes and POSIX systems, then you could copy the relevant code from pyperclip into your own project and use that instead.
And then be prepared to deal with bugs in copy/pasting yourself.
But if you expect to distribute this app to others, you will be better off using the external dependency (assuming the licence is suitable).
Alternatively, you could try a different copy/paste library. There is, allegedly, at least two others: pyperclip3 and clipboard.
Finally, your GUI framework may already support copy and paste out of the box. What GUI framework are you using? If it is Tkinter, this may work for you:
(Disclaimer: I have no idea if it copies and pastes into the system clipboard so that other applications can use that data, or just an internal clipboard.)
In fact, my intention is to release this app into the public domain and as such, I need it to be as “friendly” as it can be.
I was hoping that there could be some redirection of the standard output that I could use, so instead of the print() function going to the screen, it could be redirected to the system clipboard, but if only it were that simple, right? I was kinda thinking that if it were as simple to do as it is to type, then I’d have either been aware of it or I’d have found it by now.
Thanks for your time and the link that you’ve provided (that’s a very interesting read and I’ll squirrel that away as I’m sure it will come in handy).
My untidy work around is to have a button that switches the widget state back to ‘normal’ so that the hash value can then be ‘copied’ to the clipboard in the usual way. I may even use a Notebook Widget, so that multiple hash values, together with the file names, are inserted into said Widget and from which they can be either copied in the usual way, or even written to a file.
Cheers.
edit: yes, Tkinter is the GUI interface I’m using, as it comes with all (I think) Python installs.
Redirection of stdout won’t help. Although it is an interesting idea… untested, something like this might work.
# Untested.
class MagicClipboard:
# A partial implementation of the file interface.
def write(self, text):
pyperclip.copy(text)
return len(text)
def read(self, num=-1):
text = pyperclip.paste()
return text
sys.stdout = MagicClipboard()
This is not a full implementation of the file interface, but it should give you a place to start.
But having said that, it is surely just as simple to call pyperclip.copy() as it is to call print().
And like many simple things, print() disguises a fair chunk of complexity behind a simple interface. It is nearly 70 lines of C.
I will again, squirrel this way and inveterate pyperclip at some point: this looks interesting and could very well be of use to me.
For my App, I don’t want to have other users be expected to use anything that’s outside of a regular Python install, which I don’t think my App requires; the only dependencies I have are tkinter and hashlib, which (unless I’m very much mistaken) don’t need to be pip installed [note to self: need to check on this].
I’ll fire-up a VM with a clean Linux install just to make sure, as I’ve added all sorts of stuff to my dev environment over the years. As for the Windows OS, I’ll cross that bridge as and when, if ever.
Once again, my thanks for your time and trouble; enjoy your weekend.
Update:
Check done and on the clean Linux install that I tested, tkinter needed to be installed:
@steven.daprano sorry to be a bother (and maybe this would be clear to me if a knew a little more about Python), but a quick question (that I hope affords you a quick answer): the variable num that you set to -1, this is for why?