I'd like to be able to get a string out of a highlighted portion of text, how do I go about this?
3 Answers
For a Tkinter.Text
or Tkinter.Entry
widget, you can use:
content = text.selection_get()
Interactive example of getting selected text both from an Entry and from a Text widgets in the Python console:
>>> import Tkinter
>>> w = Tkinter.Tk()
>>> e = Tkinter.Entry(w)
>>> e.pack()
>>> t = Tkinter.Text(w)
>>> t.pack()
#(scribble at the widgets in the created window, and select text in the Entry)
>>> e.selection_get()
'1234'
#(select text)
>>> t.selection_get()
'1234'
>>>
According to Bryan Oakley's comment:
selection_get
is a generic widget method available to all widgets. It does not always get the selected string from a Text widget. It might, but it's not guaranteed. What it does is get what's called the "X" selection (from tk's X11 roots). If you setexportselection
to False for the Text widget,selection_get
will fail to work. So your suggest will work in the normal case, but not in all cases.
-
The Text widget does not have a `selection_get` method. – Bryan Oakley Apr 25 '14 at 12:36
-
@BryanOakley: Which version are you using? I am using Tkinter that ships with Python 2.7, and my text widgets definitely have a `selection_get`. (I just tried again in a console section just to be sure and it worked). – jsbueno Apr 25 '14 at 19:02
-
3I should have been more precise: the text widget itself does not have a `selection_get` method. `selection_get` is a generic widget method available to all widgets. It does _not_ get the selected string from a text widget. It might, but it's not guaranteed. What it does is get what's called the "X" selection (from tk's X11 roots). If you set `exportselection` to False for the text widget, `selection_get` will fail to work. So your suggest will work in the normal case, but not in all cases. – Bryan Oakley Apr 25 '14 at 20:13
The Text widget has a special tag named 'sel', accessible via Tkinter.SEL and testable via text_widget.tag_ranges(Tkinter.SEL), which allows you to retrieve the "selected" text. Here is a simple example:
if textWidget.tag_ranges(Tkinter.SEL):
print('SELECTED Text is %r' % textWidget.get(Tkinter.SEL_FIRST, Tkinter.SEL_LAST))
else:
print('NO Selected Text')
If you want a slightly more advanced solution, you can also try:
ranges = textWidget.tag_ranges(Tkinter.SEL)
if ranges:
print('SELECTED Text is %r' % textWidget.get(*ranges))
else:
print('NO Selected Text')

- 5,350
- 1
- 24
- 32
-
3Plus1 for checking if there is any selected text to begin with. Otherwise SEL_FIRST/LAST will error :-) – double_j Apr 06 '16 at 17:14
use the get
method. If you want the selected text use the indices SEL_FIRST
and SEL_LAST
.
[edit]] one of the comments speculated this didn't work with Tkinter because there was no attribute "sel". "sel" isn't an attribute, it's a tag. Tags are a remarkably powerful feature of the text widget.
Here's an example:
import Tkinter as tk
import tkFont
class App:
def __init__(self):
root=tk.Tk()
self.text = tk.Text(root)
self.text.pack()
self.button = tk.Button(root, text="Get Selection", command=self.OnButton)
self.button.pack()
root.mainloop()
def OnButton(self):
print "selected text: '%s'" % self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
app=App()
For this demo to work, type some text in the text box then press the button.

- 20,617
- 19
- 137
- 193

- 370,779
- 53
- 539
- 685
-
I see no such "sel" attribute in Text or Entry widgets -- maybe you are refering to methods avaliable from TCL, but that are wrapped under other name in Python? – jsbueno Nov 01 '10 at 22:37
-
@jsbueno: `sel` isn't an attribute. It's a text tag that represents the selection, and it is available under Tkinter. – Bryan Oakley Nov 02 '10 at 10:41
-
There's no need to use the underlying string defined by Tk. `Tkinter` has a variable defined for accessing it - `SEL_FIRST` and `SEL_LAST`. – ArtOfWarfare May 17 '15 at 13:52
-
@ArtOfWarfare: I don't like your edit very much. While you say there's no reason to use the underlying string because there's a variable, I say there's no need to use a variable for something that has a constant string representation. – Bryan Oakley May 17 '15 at 17:01
-
1@BryanOakley The issue with using the strings from `Tk/Tcl` is that those may change in future versions. The string literal would work on some versions and not others. If you instead use the variable defined by `Tkinter` (each version of which is tightly bound to a specific version of `Tk/Tcl`), you're guaranteed that it will always work. Further, you shouldn't be mixing your `Python` code with `Tcl` anymore than necessary. If you don't use as much `Python` as possible, why draw the line where you have? Why not just write everything in `Tcl`? – ArtOfWarfare May 17 '15 at 20:23
-
3@ArtOfWarfare: those strings are decades old and tcl takes backwards compatibility very seriously. There is no chance they will ever change. The advantage to using the strings is that the knowledge of using ".first", etc. can be transferred to all tags, not just the selection. Using the strings isn't "mixing python code with tcl" -- the strings are the documented interface. – Bryan Oakley May 17 '15 at 21:02