Select only the texts not the whole line in tkinter

459 Views Asked by At

I'm trying to select only the entered words in a ScrolledText but the whole line is getting selected. Select all code:

# I'm using ScrolledText for the input field
self.textBox = ScrolledText.ScrolledText(master=self.topFrame, wrap="word", bg='beige', padx=20, pady=5)

# Binding Shortcuts
rootone.bind("<Control-a>", self.selectAllOperation)

# Function Defination
def selectAllOperation(self, event=None):
        self.textBox.tag_add('sel', '1.0', 'end')

This is whats happening, enter image description here

This is what I want to do, enter image description here

Note that in the second picture only end of the words are selected but in the first picture the whole line is getting selected. Is it possible in tkinter to implement this feature?

I'm using python 3.6

1

There are 1 best solutions below

5
On

Question: Select only the texts not the whole line

Instead of selecting the whole text from 1.0 to end, you have to do it line by line from y.0 to y.end.

enter image description here


  1. Get the number of lines:

        lines = len(self.text.get("1.0", "end").split('\n'))
    
  2. Loop all lines, select from y.0 to y.end:

        for idx in range(1, lines):
            self.text.tag_add('sel', '{}.0'.format(idx), '{}.end'.format(idx))
    

Tested with Python: 3.5