Swati Verma
Swati Verma

Reputation: 13

Enable text copy from canvas in tcl

I want to enable copying of the text placed on canvas. Is it possible to do so? I have placed text using:

.c.canvasName create text 100 90 $var -font {Courier -12} ...

where var contains a tcl tk matrix.

Upvotes: 1

Views: 486

Answers (1)

Peter Lewerin
Peter Lewerin

Reputation: 13272

As Donal wrote, ctext.tcl gives many helpful hints on how to manage text items in canvases.

Note that it doesn't demonstrate copying text to the clipboard. Use the following code for a rudimentary clipboard copy function:

$c bind text <<Copy>> "textCopy $c"

...

proc textCopy {w} {
    clipboard clear
    clipboard append [selection get]
    selection clear
}

clipboard clear empties the Tk clipboard, and clipboard append copies new text to it. On Windows you can then paste this text using the normal Ctrl+V.

selection get copies text from the current selection and throws an error if no text is selected. Use

    catch {clipboard append [selection get]}

to suppress such errors.

selection clear unselects the selection.

Upvotes: 1

Related Questions