Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter Text selection_get() Error

I am currently working on a Tkinter app, which uses as a main widget a TextWidget.

When I try to get the current selection, an error is raised, but I don't get why...

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Users\Lina\Documents\Programmation\VPE project.py", line 502, in rechercher
    texte=code_text.selection_get()
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 626, in selection_get
    return self.tk.call(('selection', 'get') + self._options(kw))
TclError: PRIMARY selection doesn't exist or form "STRING" not defined

Thanks.

EDIT: I know why it wasn't working, i binded to Ctrl-F, but it is already binded in the TextWidgets (by default, it does exactly the same thing as LeftArrow). Now the problem is, how do I get rid of that?

like image 831
linaa Avatar asked Aug 17 '11 09:08

linaa


1 Answers

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)

like image 183
Bryan Oakley Avatar answered Oct 27 '22 19:10

Bryan Oakley