R watt
R watt

Reputation: 77

Python tkinter- Highlighting multiple colours

This is what I want to happen: The user selects a button based on what colour they would like. Then they click on the word they would like to highlight in that colour and so on.
This is what happens: The user selects a colour and all of the previous words get changed to the colour even though I want the colour that it was previously.
Below is the code that highlights. The variable Search has the text in

    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)

And this is the code for selecting the colour

#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')

As always, any questions will be gratefully received and answered. Happy xmas

Upvotes: 0

Views: 264

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

If you want each word to have a unique color, you'll have to give each word a unique tag. Or, if you have a fixed number of colors, you need to have one tag for each color (eg: 'highlight-blue', 'highlight-yellow', etc).

Upvotes: 1

Related Questions