简体   繁体   English

Python 3 Tkinter | 在文本小部件中的':'之间更改单词的颜色/样式

[英]Python 3 Tkinter | Change color/style for words in between ':' in Text Widget

Let's say we have this text: 假设我们有这段文字:

text = "You don't need to try to find hidden meaning:\nin this text :sun_with_face:!\nthis text : does not have any :meaning :D"

Now notice the :sun_with_face: in the text above? 现在注意上面的文本中的:sun_with_face:吗? That's what I am trying to get somehow highlighted by changing the boldness and the color of the Text Widget to that particular section. 这就是我想通过将文本小部件的粗体和颜色更改为该特定部分来以某种方式突出显示的内容。

Issue: 问题:

  • How to filter the text for :these_words: ? 如何为:these_words:过滤文本? You may notice that those are emoji.demojized() emoji so an optional solution would be to highlight all words that are in the emoji.UNICODE_EMOJI list but I feel that it will tax on the resources and time a lot (since the list contains over 1000 items) 您可能会注意到这些是emoji.demojized()表情符号,因此可选的解决方案是突出显示emoji.UNICODE_EMOJI列表中的所有单词,但我认为这会emoji.UNICODE_EMOJI大量资源和时间(因为该列表包含1000个项目)

  • How to highlight the wanted stuff. 如何突出显示想要的东西。 Even though we might somehow solve the first issue, these demojized emoji are in the middle of the text... 即使我们可能会以某种方式解决第一个问题,但这些经过个性化处理的表情符号仍位于文本的中间...

Edit 编辑

Just to clarify: I know how to highlight the text from this question . 只是要澄清一下:我知道如何突出显示该问题的内容 I just need to know the location of the word to high light it, which is the tricky part. 我只需要知道单词的位置以突出显示它,这是棘手的部分。 Also the :sun_with_face:. 还有:sun_with_face:。 It can be anything as long as its open and closed with ':' and in-between there are no spaces ( 只要它以':'打开和关闭,并且中间没有空格( ) and newlines ( \\n ). )和换行符( \\n )。

I think what you're asking is how to apply a highlight to text that begins with a colon, has some sort span of text, and ends with a colon. 我认为您要问的是如何对以冒号开头,具有一定范围的文本并以冒号结尾的文本应用突出显示。 You can find text that matches that pattern with the text widget search method, and you can apply the highlighting by using the tag feature of text widgets. 您可以使用文本小部件search方法找到与该模式匹配的文本,并可以通过使用文本小部件的标记功能来应用突出显示。

Since you are wanting to search against a pattern, you need to use a regular expression search. 由于您要针对模式进行搜索,因此需要使用正则表达式搜索。 You do this by setting regexp to True when you call the search method. 您可以通过在调用search方法时将regexp设置为True来实现。 Because the pattern can be any length, you also need to pass in a variable so that tkinter can return how many characters it matched. 因为模式可以是任意长度,所以您还需要传入一个变量,以便tkinter可以返回匹配的字符数。 You can use this information to apply the highlighting. 您可以使用此信息来应用突出显示。

Note: when using regular expression searches, the expression must follow the Tcl regular expression syntax , not the python one. 注意:使用正则表达式搜索时,表达式必须遵循Tcl正则表达式语法 ,而不是python。 There are some subtle differences. 有一些细微的差异。

Here's an example that finds all matches of a colon followed immediately by the shortest group of non-whitespace characters, followed by a colon. 这是一个示例,该示例查找冒号的所有匹配项,紧接着是最短的一组非空白字符,然后是冒号。 It then adds the tag "highlight" to each match: 然后,将标签“ highlight”添加到每个匹配项:

countVar = tk.IntVar()
start_index = "1.0"
while start_index:
    index = text_widget.search(r':\S+?:', start_index, stopindex="end",
                               count=countVar, regexp=True)
    if index:
        # ie: a match was found
        end_index = "{} + {} chars".format(index, countVar.get())
        text_widget.tag_add("highlight", index, end_index)
        start_index = end_index
    else:
        start_index = None

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

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