Hello I'm trying to add highlighting to the specific keywords in the text widget. For instance I would like to highlight : Hello World. where is "hello" - will be coloured in red and "World" coloured green. Any Idea ???

from Tkinter import *
import os 
import subprocess
import shutil
import tkFont
import py_compile
#py_compile.compile ("linxtl.py", "linxtl.pye")
import Tkinter as tk

import Tkinter
   
root = Tk()
class MenuTL(Frame):                 
    def __init__(self,  parent=None):      
        Frame.__init__(self, parent)      
        self.pack(expand=YES, fill=BOTH)
        self.createWidgets()  
        #self.CMENU()  
        self.master.title("LinXTL v. 0.1")
        self.master.iconname("LinXTL")
        
     #text widget 
   
    def createWidgets(self):
      
        sbar = Scrollbar(self)
        text = Text(self, relief=SUNKEN, font='bold', spacing1='1', undo='true')
        
        sbar.config(command=text.yview)                  
        text.config(yscrollcommand=sbar.set)
        sbar.pack(side=RIGHT, fill=Y)                   
        text.pack(side=LEFT, expand=YES, fill=BOTH)     
        self.text = text
if __name__ == '__main__':  
                  MenuTL().mainloop()  # if I'm run as a script

                  root.mainloop()

Recommended Answers

All 2 Replies

Member Avatar for masterofpuppets

hi,
you can check vegaseat's example from this thread http://www.daniweb.com/forums/thread191210-8.html

it's really nicely explained :)

A simple way to add color highlighted text to the Tkinter Text() widget ...
Help with Code Tags
python Syntax (Toggle Plain Text)
# multiple color text with Tkinter using widget Text() and tags
# vegaseat
 
try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
 
def color_text(edit, tag, word, fg_color='black', bg_color='white'):
    # add a space to the end of the word
    word = word + " "
    edit.insert('end', word)
    end_index = edit.index('end')
    begin_index = "%s-%sc" % (end_index, len(word) + 1)
    edit.tag_add(tag, begin_index, end_index)
    edit.tag_config(tag, foreground=fg_color, background=bg_color)
 
 
root = tk.Tk()
root.geometry("600x200")
 
edit = tk.Text(root)
edit.pack()
 
text = "Up the hill went Jack and Jill, down fell Jill and cried!"
# create a list of single words
word_list = text.split()
#print( word_list )  # test
 
# pick word to be colored
myword1 = 'Jack'
myword2 = 'Jill'
# create a list of unique tags
tags = ["tg" + str(k) for k in range(len(word_list))]
for ix, word in enumerate(word_list):
    # word[:len(myword)] for word ending with a punctuation mark
    if word[:len(myword1)] == myword1:
        color_text(edit, tags[ix], word, 'blue')
    elif word[:len(myword2)] == myword2:
        color_text(edit, tags[ix], word, 'red', 'yellow')
    else:
        color_text(edit, tags[ix], word)
 
root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.