Tk Source Code

View Ticket
Login
Ticket UUID: 9990770674adf8ffb6bb89d4b9dcfda14ae55697
Title: MacOS. Clipboard updates not updating the change count of the system pasteboard.
Type: Bug Version: 8.6
Submitter: anonymous Created on: 2023-05-22 11:13:46
Subsystem: 83. Mac OS X Build Assigned To: marc_culler
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2025-07-26 02:41:51
Resolution: Fixed Closed By: marc_culler
    Closed on: 2025-07-26 02:41:51
Description:

There appears to be a problem with Tk not updating the change count of the system pasteboard correctly. This was discovered while attempting to copy data to the clipboard on MacOS from Python/Tkinter and paste the contents to BBedit.

Subsequent analasyis by the support team there highlighted where they believe the issue arises, and appears to trace to the Tk usage/implementation of the MacOS pastebin. I beleive that it therfore must lay somewhere within this file https://github.com/tcltk/tk/blob/main/macosx/tkMacOSXClipboard.c but lack the skill/knowledge to see exactly how.

To quote from their response

It appears that whatever mechanics are invoked by Tkinter when root.clipboard_clear() or root.clipboard_append are called are not updating the change count of the system pasteboard.

It's a best practice for pasteboard clients to check the change count (to test for external changes to the pasteboard) in advance of importing the system pasteboard; particularly with large pasteboard contents, this saves a great deal of time and memory.

BBEdit (and presumably many other applications which we haven't discovered yet) does this, and it observes that the pasteboard change count is the same as before; so it does not reimport the system pasteboard.

Full discussion can be found here. https://github.com/python/cpython/issues/104613

To recreate the issue this minimal python/Tkinter script can be used to load the clipboard with a new timestamp on each button press. Pasting the result into BBedit results in only the initial timestamp being pasted as it never re-imports the system pasteboard on subsequent copy/paste operations.

import tkinter as tk
import datetime

def on_click():
    root.clipboard_clear()
    now = datetime.datetime.now()
    print(now)
    root.clipboard_append(now)

root = tk.Tk()
frame = tk.Frame(root, padx=10, pady=10)
frame.pack()
btn = tk.Button(frame, text="Copy time", command=on_click)
btn.pack()
root.mainloop()

I've marked the severity as minor as so far I have only managed to observe this behaviour as affecting BBedit, but as their support team notes it could well affect many more undiscovered applications.

Thanks.

User Comments: marc_culler (claiming to be Marc Culler) added on 2025-07-26 02:37:04:
Thank you, Christopher.  This ticket got away from me, and I had to
rediscover the solution you had proposed here when I worked on
[e94c8bc845].

I just tested the python script in this ticket, using the tip of
core-8-6-branch, and I can confirm that every time I press the button
the current time appears in Pasteboard Viewer.  So I think I can
close this one now.

Incidentally, in case you hadn't noticed this, I finally managed to
convince Ned Deily to embed Tcl.framework and Tk.framework inside of
Python.framework.  This means that you can move those frameworks aside
and replace them with symlinks to /Library/Frameworks/Tcl.framework
and /Library/Frameworks/Tk.framework.  That allows testing tkinter
against the current tip of whatever version of Tk Python happens to
be using.  (Hopefully that will be 9.0.2 before too long!)

chrstphrchvz added on 2025-07-25 22:25:11:

I believe this issue has been resolved by the recent fix for issue [e94c8bc845].


chrstphrchvz added on 2023-05-22 15:10:06:

The Tk Cocoa implementation of TkSelUpdateClipboard() has always used -[NSPasteboard addTypes:owner:] (see https://github.com/das/tcltk/commit/82e795503066). Using -[NSPasteboard declareTypes:owner:] instead seems to resolve the issue, but I am not certain it is the correct approach.


chrstphrchvz added on 2023-05-22 12:46:20:

I currently do not have BBEdit, but I believe I have reproduced the issue using https://github.com/sindresorhus/Pasteboard-Viewer with recent Tk Aqua 8.7 from the trunk branch on macOS 12.6.6 Monterey (Intel). I can reproduce this with the Tk text widget demo, so the issue is not due to Tkinter. I observe Pasteboard Viewer detecting a copy attempt from Tk only when Tk was not the last app to copy something, if that makes sense.