Python 3.12 in a venv, Kubuntu 24.04.02. I'm using the tkinter clipboard in a python program to hold a small amount of text, to ctrl-V into text editors and browser fields. This bit works fine. I would now like the clipboard to be set to a defined value on program exit. Empty would be fine. I don't want sensitive data lying around to be accidentally pasted after program exit. This is a Minimal Reproducible Example that illustrates the problem. #! /home/neil/pye/bin/python3 import tkinter as tki import time class App(tki.Frame): def __init__(self, parent): tki.Frame.__init__(self, master=parent) self.B0 = tki.Button(self, text='set clip', command=self.set_clip) self.B0.grid() self.quitbut = tki.Button(self, text='Quit', command=self.quit) self.quitbut.grid() def set_clip(self): self.clipboard_clear() self.clipboard_append(f'{time.time()}') print('set clipboard') def quit(self): print('clearing up before exit') self.clipboard_clear() self.clipboard_append('trashed') time.sleep(0.1) self.update_idletasks() self.update() time.sleep(0.1) print('hopefully updated clipboard?') self.real_exit() def real_exit(self): exit() if __name__ == '__main__': root = tki.Tk() app = App(root) app.pack() # this bodgery points the window 'X' button to my quit routine root.protocol("WM_DELETE_WINDOW", app.quit ) root.mainloop() The set-clip function writes a time string to the clipboard which can be pasted to a text file with ctrl-V. However nothing I have yet tried gets the quit function to write 'trashed' to it. You can see my peppering of delays, updates, other function calls, to try to give it time to happen. The actual behaviour together with the system is more subtle and puzzling. In Kubuntu, there is a clipboard icon on the panel that apparently shows the present and historical contents of the clipboard. If I run my MRE, then on first press of 'set_clip', ctrl-V into a text field pastes a time string t0, and I see t0 in the Kubuntu app. On the next press of set_clip, ctrl-V pastes a new time string tn, however the Kubuntu app shows no change in history, still just the t0 entry. Subsequent presses yield the same behaviour. If I now exit the program, ctrl-V will now paste t0 again, not tn or 'trashed'. I found this Q/A, Tk only copies to clipboard if "paste" is used before program exits, which suggests the use of pyperclip. I won't repeat the whole MRE for that, just the key clipboard interaction part. # original using just the tkinter clipboard self.clipboard_clear() self.clipboard_append(f'{time.time()}') # replaced with pyperclip calls self.clipboard_clear() text = f'{time.time()}' pyperclip.copy(text) pyperclip.paste() self.update() This has had two effects Now both set_clip() and quit() behave the same way, I do sometimes see 'trashed' after program exit Although each button press changes the Kubuntu app history, the value that gets crtl-V'd into a text field only changes with every other button press??? So pyperclip is not a solution like this, though it has had an effect. The contradictory behaviour between what gets ctrl-V'd and what Kubuntu thinks is on the clipboard hints at a deeper compatibility problem to me. I would rather solve this in tkinter, rather than rewrite for another GUI. However, if another GUI is better integrated with the Ubuntu base, then maybe I've got to use that. I would also happily switch to Mint (still Ubuntu under the hood), if that's more compatible at this clipboard level. Continue reading...