Python tkinter-突出显示多种颜色

时间:2017-12-20 13:53:52

标签: python tkinter

这就是我想要发生的事情:用户根据他们想要的颜色选择一个按钮。然后他们点击他们想用该颜色突出显示的单词等等 会发生这种情况:用户选择一种颜色,所有之前的单词都会变为颜色,即使我想要它之前的颜色。
以下是突出显示的代码。 变量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')

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

1 个答案:

答案 0 :(得分:1)

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