9

Whenever I try the following in my python interpreter. I am able to copy the word hello to the command line, even after I close the interpreter

from Tkinter import Tk
r = Tk()
r.clipboard_append(" hello ")

However if I put this in a file called test.py and then try

python test.py

This will not work, I can't append this to the system clipboard

Does any one know why not or know what difference between running it in a script and in the interpreter would cause

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
samirahmed
  • 1,219
  • 2
  • 12
  • 16
  • If you're leaving the interpreter open it's due to just that(for the first case). Once python runs and cleans its self up it will clear what's in the clipboard. – dennmat Feb 17 '12 at 14:54
  • As I specified in the question, I close the interpreter and it still copies it to the clipboard. – samirahmed Feb 17 '12 at 22:45
  • 1
    Missed that part, however you should call clipboard_clear first anyway to make sure there's nothing dirty in there. However I'm not sure why it would be different between the two as they really are the exact same thing. Hopefully someone has the answer for you. – dennmat Feb 17 '12 at 23:16
  • Even `python 3` doesn't seem to address this problem. – Prakhar Mishra Mar 02 '16 at 14:44

3 Answers3

7

Apparently it won't work until Tkinter is in it's mainloop. This works on my system:

from Tkinter import *
r = Tk()
r.clipboard_append("hello")
r.mainloop()
Patrick T Nelson
  • 1,234
  • 12
  • 21
  • what if I dont want a mainloop? I want my program to terminte after this. `r.update()` does not help. using a timeout as a workaround (see below) does work – phil294 Nov 28 '16 at 16:56
2

I see this difference in behavior too. The suggested tkinter solution for placing text on the clipboard works fine via the command line, but leaves the clipboard empty when used in a program. Using mainloop() at the end of the program works but means the program won't end, and using r.update() doesn't seem to help.

Note: If the clipboard is pasted to another application before the program ends (by making the program hang at the end waiting for user input), then the tkinter solution works fine, even after the program ends. But if the program ends before the clipboard is pasted to another program, the clipboard ends up being empty.

deel
  • 21
  • 1
1

Like deel, the suggestion of Patrick T Neslon and others to use:

from Tkinter import *
r = Tk()
r.clipboard_append("hello")
r.mainloop()

didn't work in a non-gui application (update didn't work either) (Win 7/64, Python 2.7.10/32). This works though it seems to be a hack, inspired by https://bugs.python.org/issue23760:

from Tkinter import *
r = Tk()
r.clipboard_clear()
r.clipboard_append("hello")
r.after(500, tk.destroy)
r.mainloop()

100 ms usually worked on my system so went with 500. This for a utility that I will be using, not something I'd want to distribute very widely unless somebody with much more experience says its reliable...

RunDeep
  • 186
  • 1
  • 10