gpt4 book ai didi

python - 在 tkinter 中仅选择文本而不是整行

转载 作者:行者123 更新时间:2023-12-01 01:12:03 25 4
gpt4 key购买 nike

我试图仅选择 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')

这就是正在发生的事情, enter image description here

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

请注意,在第二张图片中,仅选择了单词的末尾,但在第一张图片中,整行都被选中。tkinter 可以实现这个功能吗?

我使用的是 python 3.6

最佳答案

Question: Select only the texts not the whole line

您不必选择从 1.0end 的整个文本,而是必须从 y.0 逐行执行此操作>y.end

enter image description here

<小时/>
  1. 获取行数:

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

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

使用 Python 测试:3.5

关于python - 在 tkinter 中仅选择文本而不是整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54759023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com