简体   繁体   English

基于Tkinter文本小部件中的第一个字符标记整个单词

[英]Tagging Whole Word Based on the First Character in a Tkinter Text Widget

I'm trying to tag and highlight a whole word based on the first character. 我正在尝试根据第一个字符标记并突出显示整个单词。 In the example below I'm searching for hashtags "#". 在下面的示例中,我正在搜索主题标签“#”。 I want to tag and highlight the subsequent characters until the next space. 我要标记并突出显示后续字符,直到下一个空格为止。 So the characters "#hashtag" should all be highlighted blue. 因此,字符“ #hashtag”应全部突出显示为蓝色。 The code below finds the hashtags and highlights them blue but not the subsequent characters. 下面的代码查找主题标签并将其突出显示为蓝色,但不突出显示后续字符。 I assume the problem is that I'm not using "wordend" correctly in self.text_box.tag_add("hashtag{}".format(x), self.index, self.index + "wordend") but I can't figure out what I'm doing wrong. 我认为问题是我没有在self.text_box.tag_add("hashtag{}".format(x), self.index, self.index + "wordend")正确使用"wordend" self.text_box.tag_add("hashtag{}".format(x), self.index, self.index + "wordend")但我无法弄清楚我在做什么错。

According to effbot.org 根据effbot.org

“wordstart” and “wordend” moves the index to the beginning (end) of the current word. “ wordstart”和“ wordend”将索引移到当前单词的开头(结尾)。 Words are sequences of letters, digits, and underline, or single non-space characters. 单词是字母,数字和下划线或单个非空格字符的序列。 http://effbot.org/tkinterbook/text.htm http://effbot.org/tkinterbook/text.htm

Any pointers welcome. 任何指针欢迎。

Here is some sample code that replicates the problem: 这是一些复制该问题的示例代码:

import tkinter as tk
import tkinter.scrolledtext as St

    class Main(tk.Tk):

    def __init__(self):

        tk.Tk.__init__(self)
        self.text = "random text\nrandom text\n#hashtag random text\n#hashtag"

        self.text_box = St.ScrolledText(self)
        self.text_box.pack()

        self.text_box.insert("1.0", self.text)

        self.index = self.text_box.search("#", "1.0", stopindex=tk.END)
        x = 0
        while self.index != "":
            self.text_box.tag_add("hashtag{}".format(x), self.index, self.index + "wordend")
            self.text_box.tag_configure("hashtag{}".format(x), foreground="blue")
            self.index += "+1c"
            self.index = self.text_box.search("#", self.index, stopindex=tk.END)
            x += 1

main=Main()
main.mainloop()

From the canonical tcl/tk documentation: 从规范的tcl / tk文档中:

A word consists of any number of adjacent characters that are letters, digits, or underscores, or a single character that is not one of these . 单词由任意数量的相邻字符组成,这些字符可以是字母,数字或下划线, 也可以是单个字符,而不是其中之一 If the display submodifier is given, this only examines non-elided characters, otherwise all characters (elided or not) are examined. 如果指定了display子修饰符,则仅检查非省略字符,否则将检查所有字符(是否被删除)。

In your case the character after "3.0" is # which is "not one of these", so that single character is considered to be a word, and thus the "wordend" modifier stops after that single character. 在您的情况下,“ 3.0”之后的字符是# ,而不是其中的一个,因此单个字符被视为一个单词,因此“ wordend”修饰符在该单个字符之后停止。

If you want a hashtag plus the word following it to be given the tag, your second index should be of the form " line.char +1c wordend" (eg: "3.0+1c wordend") so that you look for the end of the word beginning with the first character after the hash, not beginning with the hash itself. 如果您想为标签加上标签后面的单词,则第二个索引应采用“ line.char + 1c wordend ”(例如:“ 3.0 + 1c wordend”)的形式,以便查找该单词以哈希之后的第一个字符开头,而不是以哈希本身开头。

I got it working by changing the tag.add line to 我通过更改tag.add其工作

self.text_box.tag_add("hashtag{}".format(x), self.index, (self.index + "+1c") + " wordend").

But I'm not sure why I need to add the extra character to the end index of the tag.add command? 但是我不确定为什么需要在tag.add命令的末尾索引中添加额外的字符? Can anyone explain? 谁能解释?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM