Python tkinter-突出显示多种颜色

问题描述 投票:0回答:1

这就是我想要发生的事情:用户根据他们想要的颜色选择一个按钮。然后他们点击他们想用该颜色突出显示的单词,依此类推。 这就是:用户选择一种颜色,所有前面的单词都变为颜色,即使我想要它以前的颜色。 以下是突出显示的代码。变量Search包含文本

    def _on_click(self, event):    
                if "highlight" in tags:
                    #Unhighlights
                    ArticleTextBox.tag_remove("highlight", "insert wordstart", "insert wordend")
                    wordclicked=ArticleTextBox.get("insert wordstart", "insert wordend")
                    SearchLEN=len(Search)
                    for x in range(0,SearchLEN):
                        if Search[x]==wordclicked:
                            #global Search    # Needed to modify global copy of globvar
                            Search.remove(wordclicked)

                else:
                    #highlights
                    ArticleTextBox.tag_add("highlight", "insert wordstart", "insert wordend")
                    wordclicked=ArticleTextBox.get("insert wordstart", "insert wordend")
                    #global Search    # Needed to modify global copy of globvar
                    Search.append(wordclicked)
                    #print(Search)

这是选择颜色的代码

#Colour picker
    def sel(self):
       selection = "You selected the option " + str(var.get())
       colournumber=(var.get())
       if colournumber==2:
           ArticleTextBox.tag_config('highlight', background='yellow', foreground='black')
       elif colournumber==3:
           #print("Sorry this is not working at the moment- Please go back to name selection before you click the button")
           ArticleTextBox.tag_config('highlight', background='blue', foreground='black')
       else:
           ArticleTextBox.tag_config('highlight', background='yellow', foreground='black')

与往常一样,任何问题都将被感激地收到并回答。圣诞快乐

python tkinter
1个回答
1
投票

如果您希望每个单词都具有唯一的颜色,则必须为每个单词指定一个唯一的标记。或者,如果您有固定数量的颜色,则每种颜色需要一个标签(例如:'highlight-blue','highlight-yellow'等)。

© www.soinside.com 2019 - 2024. All rights reserved.