0

I am trying to give recognized sentences in Text Tk(). I want to highlight with different colors to certain tokens in accordance their labels. I am deploying a model result to GUI. The model output text file format is like this:

# 1.0000
This B-LOC
is I-LOC
example I-LOC
of E-LOC
my O
data O
format O
. O
In O
this B-ORG
place E-ORG
, O
characters O
of O
my O
language O
is B-PNAME
applied E-PNAME
. O

And S-PNAME
help O
Me. O

Here is the code example.

if l_list[i] == "S-PNAME" or "B-PNAME" or "I-PNAME" or "E-PNAME":

    self.output.update()
    self.output.insert(END,s_list[i])
    self.output.config(foreground='red')                                   

elif l_list[i] == "S-ORG" or "B-ORG" or "I-ORG" or "E-ORG":              

    self.output.update()
    self.output.insert(END,s_list[i])
    self.output.config(foreground='pink') 

else:
    self.output.update()
    self.output.insert(END,s_list[i])

I want to color tokens with P-NAME tags with red colour, LOC tags with pink colours etc....But in my output all sentences are coloured red.

halfer
  • 19,824
  • 17
  • 99
  • 186
HMM
  • 1
  • 1
  • # 1.0000 This B-LOC is I-LOC example I-LOC of E-LOC my O data O format O . O In O this B-ORG place E-ORG , O characters O of O my O language O is B-PNAME applied E-PNAME . O And S-PNAME help O Me. O – HMM Jun 04 '19 at 03:48
  • I have formatted your text as a block, please edit it again if that is not what you intended. – halfer Jun 04 '19 at 07:41

1 Answers1

1

I assume your self.output is the text widget. Currently you are just modifying the foreground of all the text in the widget by calling self.output.config(foreground=...).

To highlight colors for different text, you need to set tag for the text inserted, and then use tag_config to configure the colors of each tag.

import tkinter as tk

root = tk.Tk()

text = tk.Text(root)
text.pack()

text.insert(tk.INSERT,"This is a red message\n","red")
text.insert(tk.INSERT,"This is a green message\n","green")
text.insert(tk.INSERT,"This is a blue message\n","blue")

text.tag_config("red", foreground="red")
text.tag_config("green", foreground="green", relief="sunken",borderwidth=2)
text.tag_config("blue", foreground="blue", underline=1)

root.mainloop()
Henry Yik
  • 22,275
  • 4
  • 18
  • 40
  • Thank you. But it doesn't work. if l_list[i] == "S-PNAME" or "B-PNAME" or "I-PNAME" or "E-PNAME": self.output.tag_configure('red',foreground='red') self.output.insert(END,s_list[i],'red') elif l_list[i] == "S-ORG" or "B-ORG" or "I-ORG" or "E-ORG": self.output.tag_configure('pink',foreground='pink') self.output.insert(END,s_list[i],'pink') – HMM Jun 04 '19 at 05:49
  • "Doesn't work" does not describe the problem. What error you received upon executing? – Henry Yik Jun 04 '19 at 05:54
  • The input test file format is like CoNLL data format. In the text output, I want this input tokens as sequence and certain tokens are highlighted. – HMM Jun 04 '19 at 05:58
  • I resulted all tokens are highlighted with red color. – HMM Jun 04 '19 at 06:11
  • You can't just dump all your text imported into `Text` widget and expect to have all keywords automatically tagged. See [this post](https://stackoverflow.com/questions/19464813/explain-tkinter-text-search-method) on how to tag keywords by searching. – Henry Yik Jun 04 '19 at 06:25