1

我试图只选择 ScrolledText 中输入的单词,但整行都被选中了。选择所有代码:

# 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')

这是正在发生的事情, 在此处输入图像描述

这是我想做的, 在此处输入图像描述

请注意,在第二张图片中,只选择了单词的结尾,但在第一张图片中,整行都被选中了。tkinter 是否可以实现此功能?

我正在使用 python 3.6

4

1 回答 1

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

于 2019-02-19T18:33:23.890 回答