Is it possible to copy text to your clipboard and preserve it after closing my window?
Asked Answered
C

2

1

I want to have a copy function in my program, but after it is copied, and the window is closed, my clipboard is wiped of all copied or cut text from my program.

Concepcionconcept answered 4/10, 2022 at 8:35 Comment(1)
Have a look at this answer - it seems like you will find a solution there: #46179450Mobster
I
0

How do I copy a string to the clipboard?

I think there you can find the answer

from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('text')
r.update() # now it stays on the clipboard after the window is closed

Izabel answered 4/10, 2022 at 8:43 Comment(1)
I'm not having any luck. I'm trying to copy from an Entry widget and used a function that used entry.event_generate("<<Copy>>"). Now I am trying to use this in its function instead, but it just closes my window without adding anything to the clipboard.Concepcionconcept
T
0

As of March 2023, on Kubuntu 20.04, I was not able to make it work with either Tkinter (as in a previous answer), or pyperclip, or pandas.

What finally did work for me was xerox:

import xerox

#produce the string you want to copy to the clipboard
my_string = "here_is_a_nontrivial_string"

#set the clipboard contents to the string
xerox.copy(my_string)

After execution, the string "here_is_a_nontrivial_string" remained in the clipboard, and I was able to paste it into e.g. a text file, using ctrl+v.

Note that I had the same results regardless of whether I saved the code in a .py file and ran it using the command python3, or whether I saved it as a bash script (adding the line #!/usr/bin/python3 at the top), making it executable, and invoking it as a command from the terminal.

Traps answered 27/3, 2023 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.