1

I'm looking for some code to append a string to the clipboard and get the text from the clipboard ues Python3 and tkinter. And I find this question. So I tried these codes:

from tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('i can has clipboardz?')
r.destroy()

They're cleared my clipboard, but didn't append anything.

By the way, I tried print(r.clipboard_get()).

It's working out-of-the-box. But I can't append any text to the clipboard.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • @TigerhawkT3 It's working! but the text will lost after 10 seconds and the program must keep running. How to keep the text in clipboard and I don't need keep the program running? – Remi Guan Sep 07 '15 at 02:36
  • From that answer: "If you do paste it within that time, it will remain in the clipboard even after the program ends." You'll have to keep the program running until you actually paste it. After you've pasted it once, it will remain in the clipboard even after you close the program. – TigerhawkT3 Sep 07 '15 at 02:40
  • @TigerhawkT3 Well, I'm sorry about I didn't see that. Thanks a lot :) – Remi Guan Sep 07 '15 at 02:44

1 Answers1

-1

You can try using the clipboard library

https://pypi.python.org/pypi/clipboard/0.0.4

The gist of it is on that page

import clipboard
clipboard.copy("abc")  # now the clipboard content will be string "abc"
text = clipboard.paste()  # text will have the content of clipboard
Arya
  • 1,382
  • 2
  • 15
  • 36