Tkinter in Python 3.4 on Windows dont post internal clipboard data to the Windows clipboard on exit

2024/10/15 12:30:16

I use the following code to place result of my small scripts in clipboard.

from tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append("Result")

It works fine on Python version 3.3.5 and earlier. But when I upgrade to Python 3.4 it was receive empty clipboard. I tried the 3.4.0, 3.4.1 and 3.4.2 and all received one result (empty clipboard). If I prevent the script from the immediate exit, adding input() after clipboard_append(), I see that clipboard contains the correct "Result".

Update1:

The modified test script shows that the problem is not a timing issue:

from tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append("Result")
input()
r.destroy()

If I run this script, wait a few seconds, press Enter, I receive empty clipboard.

If I run script, switch to any other window and press Ctrl+V, I receive "Result" and "Result" remains in clipboard after script exits.

I think in tcl/tk 8.6 clipboard_clear() affect system clipboard, but clipboard_append affect only internal tcl/tk clipboard that transfered to system clipboard only by OS request. Looks like it was done in Linux, that don't have system clipboard.

Update2:

Last mention clipboard in Tk changelog refers to the 2004:

2004-05-03 (bug fix)[939389, 822002, 732662] Correctly post internal 
clipboard data to the Windows clipboard on exit. (hobbs)

It seems that the error corrected in 2004 returned to us 10 years later.

Answer

I suspect that 'Result' never gets to the clipboard, rather than being cleared. 3.4 on Windows come with tcl/tk 8.6 instead of 8.5. I suspect that this is the difference, and it is possibly a timing issue. Does r.destroy after the append work? Explicitly destroying the tk root before exiting python is generally a good idea. We had problems with the test suite until we started doing this.

https://en.xdnf.cn/q/69287.html

Related Q&A

How do I group date by month using pd.Grouper?

Ive searched stackoverflow to find out how to group DateTime by month and for some reason I keep receiving this error, even after I pass the dataframe through pd.to.datetimeTypeError: Only valid with D…

Python Too many indices for array

I am reading a file in python using pandas and then saving it in a numpy array. The file has the dimension of 11303402 rows x 10 columns. I need to split the data for cross validation and for that I …

Removing named entities from a document using spacy

I have tried to remove words from a document that are considered to be named entities by spacy, so basically removing "Sweden" and "Nokia" from the string example. I could not find …

Install wxPython in osx 10.11

When I try to install wxPython, it shows an error: > The Installer could not install the software because there was no > software found to install.How can I fix it?

merging recurrent layers with dense layer in Keras

I want to build a neural network where the two first layers are feedforward and the last one is recurrent. here is my code :model = Sequential() model.add(Dense(150, input_dim=23,init=normal,activation…

How to manually mark a Celery task as done and set its result?

I have this Celery task:@app.task def do_something(with_this):# instantiate a class from a third party libraryinstance = SomeClass()# this class uses callbacks to send progress info about# the status a…

How to sort a numpy array based on the values in a specific row?

I was wondering how I would be able to sort a whole array by the values in one of its columns.I have :array([5,2,8,2,4])and:array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14],[15, 16…

python regex match optional square brackets

I have the following strings:1 "R J BRUCE & OTHERS V B J & W L A EDWARDS And Ors CA CA19/02 27 February 2003", 2 "H v DIRECTOR OF PROCEEDINGS [2014] NZHC 1031 [16 May 2014]&…

How to open console in firefox python selenium?

Im trying to open firefox console through Selenium with Python. How can I open firefox console with python selenium? Is it possible to send keys to the driver or something like that?

Can python coverage module conditionally ignore lines in a unit test?

Using nosetests and the coverage module, I would like coverage reports for code to reflect the version being tested. Consider this code:import sys if sys.version_info < (3,3):print(older version of …