I’m using the line below to paste text in a Tkinter Text widget. However, I’d like to have the ability to alter the text prior to it being pasted. I’m specifically trying to remove anything that would cause a new line to be made (e.g., return, ‘\n’). So how would I get the copied text as a string, then how would I set the copied text with a new string.
Line :
tktextwid.event_generate('<<Paste>>')
You don’t need to use
event_generateif you’re going to pre-process the data. You simply need to grab the clipboard contents, manipulate the data, then insert it into the widget. To fully mimic paste you also need to delete the selection, if any.Here’s a quick example, barely tested:
When you run this code and click on the “do it!” button, it will replace the selected text with what was on the clipboard after converting all newlines to the literal sequence
\n