Tkinter Text selection_get() Error Tkinter Text selection_get() Error tkinter tkinter

Tkinter Text selection_get() Error


That error is simply telling you that nothing is selected. It's not an error per se, just it's way of saying "there's nothing to get". That may be true, or you may have something selected but it isn't being exported to "the selection". If you have the exportselection option on the widget set to true, anything you select should be copied to the selection. If it's not, there's not enough code in your question to answer why.

However, to answer the question of "how do I get the text that is selected in the widget": The text that is selected in a text widget has the tag 'sel'. You can get this text with textwidget.get('sel.first', 'sel.last')

Using the get method with the tags is more correct than using selection_get since it's possible to have nothing selected in the widget yet still have selection_get return something (eg: return whatever other widget has exported to the selection)


I got the error when selecting empty space in text box. To avoid the error, I have usedchecking with re.search for text in the selection.

word = text.selection_get()if re.search(r'\w+', word):       some_action()