只选文本,不选整行

2024-04-26 02:40:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

事情就是这样, enter image description here

这就是我想做的, enter image description here

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

我使用的是python3.6


Tags: the代码文本selfmasterfieldforinput
1条回答
网友
1楼 · 发布于 2024-04-26 02:40:09

Question: Select only the texts not the whole line

与其选择从1.0end的整个文本,不如从y.0y.end逐行选择。你知道吗

enter image description here


  1. 获取行数:

        lines = len(self.text.get("1.0", "end").split('\n'))
    
  2. 循环所有行,selecty.0y.end

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

用Python:3.5测试

相关问题 更多 >