06-06-2020, 07:14 +0200
AuthorPost
SIYB  12-09-2007, 13:23   | [TK] Copy highlighted text to clipboard Jabber  Homepage
Member since 06/2007
8 Posts
It took me some time to figure out how to get highlighted text from a text widget, I figured that this code might save some time. This program draws a text widget (in which you are able to write in) and a menu that will be spawned by using mouse3 (Button-3).
--> Highlight some typed text, "rightclick", copy and paste.

                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
# copy highlighted text
proc copyhl {} {
    set start [lindex [split [.textbox tag ranges sel]] 0]
    set stop [lindex [split [.textbox tag ranges sel]] 1]
    if {$start == "" || $stop == ""} { return };# check if there is something to copy

    clipboard clear
    clipboard append [.textbox get $start $stop]
}

pack [text .textbox]

# small menu featuring copy&paste functionality
menu .menu1 -tearoff 0
.menu1 add command -label "Copy" -command {
    copyhl
}
.menu1 add command -label "Paste" -command {
    .textbox insert end [clipboard get]
}
bind .textbox <Button-3> { tk_popup .menu1 %X %Y }

Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen | Time: 152.3 msec, CPU time: 54.1 msec, 30 Database queries in 113.3 msec | 3.3 kB (12.6 kB) | Timezone: +0200