Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to append to clipboard

Tags:

python

tkinter

tk

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

like image 341
samirahmed Avatar asked Feb 17 '12 02:02

samirahmed


2 Answers

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()
like image 121
Patrick T Nelson Avatar answered Oct 05 '22 01:10

Patrick T Nelson


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.

like image 43
deel Avatar answered Oct 05 '22 00:10

deel