1

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

stovfl
  • 14,998
  • 7
  • 24
  • 51
Soumyajit
  • 329
  • 1
  • 5
  • 16

1 Answers1

2

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

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • Used this, numLines = len(self.textBox.get("1.0", "end").split("\n")) for i in range(1, numLines): self.textBox.tag_add('sel', '{}.0'.format(i), '{}.end'.format(i)) Not working – Soumyajit Feb 22 '19 at 17:33
  • Not working in the sense, there's no change in the output. The result is the same as before selecting the whole line. `print(numLines)` gives `actual_line + 1`, i.e. if there are 6 lines then the output is `7` and yes, I'm using wrap="word" as mentioned in the question. – Soumyajit Feb 22 '19 at 20:04
  • 1
    Please verify this single line selection: `self.textBox.tag_add('sel', '1.0', '1.end')`. I want to know *whole line* or *OK*? – stovfl Feb 22 '19 at 20:08
  • I've created menu items for select all operation, from there it's selecting only the end of texts. But I was using keyboard shortcut (`Ctrl+a`, which I bind with the function `selectAllOperation`). From keyboard shortcut it's selecting the whole line. Do you have any fix for this keybord shortcut? `rootone.bind("", self.selectAllOperation) rootone.bind("", self.selectAllOperation)` – Soumyajit Feb 22 '19 at 20:49
  • I'm using `.bind(""...` as well. So couldn't confirm this behavior. Doublecheck if you use `.bind(""...` for another binding? – stovfl Feb 22 '19 at 20:56