On this page describing the Tkinter text widget, it's stated that 'The selection is a special tag named SEL (or “sel”) that corresponds to the current selection. You can use the constants SEL_FIRST and SEL_LAST to refer to the selection. If there’s no selection, Tkinter raises a TclError exception.'
My question: is there a more efficient way to tell if there is a selection in a Text widget besides fooling with exceptions, like the below code?
seltext = None
try:
seltext = txt.get(SEL_FIRST, SEL_LAST)
except TclError:
pass
if seltext:
# do something with text
tag_nextrange() . Use this method to change the order of tags in the tag stack (see Section 24.5, “ Text widget tags”, above, for an explanation of the tag stack). If you pass two arguments, the tag with name tagName is moved to a position just above the tag with name aboveThis .
An Entry widget in Tkinter is nothing but an input widget that accepts single-line user input in a text field. To return the data entered in an Entry widget, we have to use the get() method. It returns the data of the entry widget which further can be printed on the console.
The Entry widget is used to accept single-line text strings from a user.
You can ask the widget for the range of text that the "sel" tag encompases. If there is no selection the range will be of zero length:
if txt.tag_ranges("sel"):
print "there is a selection"
else:
print "there is no selection"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With