Error selecting Tkinter Text_get ()

I am currently working on a Tkinter application that uses TextWidget as its main widget.

When I try to get the current selection, an error occurs, but I do not understand 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 doesn’t work, I am bound to Ctrl-F, but it is already bound in TextWidgets (by default it does the same as LeftArrow). Now the problem is, how can I get rid of this?

+4
source share
1 answer

This error just tells you that nothing is selected. This is not a mistake in itself, it’s just a way of saying “nothing will work out there”. This may be true, or you may have something selected, but it is not exported to "selection". If the exportselection parameter in the widgets is set to true, everything you select should be copied to the selection. If this is not the case, your question does not have enough code to answer.

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

Using the get method with tags is more appropriate than using selection_get , since it is possible that nothing is selected in the widgets, but selection_get still has something to return (for example: return all other widgets exported to the selection)

+4
source

All Articles