4

I am currently trying to write a program which adds coloured text to both sides of selected text in Tkinter.

What I have managed to do so far is add text on both sides of selected text, here is the function I use:

def addTerm(self):
    self.txt.insert(tk.SEL_FIRST,'\\term{')
    self.txt.insert(tk.SEL_LAST,'}' )

So if I have a WORD and I select it, after calling this function it becomes \term{WORD}. I am wondering if there is a way to change the color of the text I am adding, so that when I use the function for the selected text it adds ' \term{ ' and '}' which are, for example, red, but it doesn't change the colour of the text between them.

rainbowmiha
  • 105
  • 1
  • 10

3 Answers3

3

Add tag for surrounding text with:

tk.SEL_FIRST + '-6c', tk.SEL_FIRST  # for \term{
tk.SEL_LAST, tk.SEL_LAST + '+1c'    # for }

Set color by using Text.tag_config(tag_name, background=...)


In the following example, I used term as a tag name:

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk


class MyFrame(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.txt = tk.Text(self)
        self.txt.pack()
        self.txt.insert(0.0, 'hello\nworld')
        self.btn = tk.Button(self, text='add_term', command=self.add_term)
        self.btn.pack()
        self.txt.tag_config('term', background='red')

    def add_term(self):
        self.txt.insert(tk.SEL_FIRST,'\\term{')
        self.txt.insert(tk.SEL_LAST,'}' )
        self.txt.tag_add('term', tk.SEL_FIRST + '-6c', tk.SEL_FIRST)
        self.txt.tag_add('term', tk.SEL_LAST, tk.SEL_LAST + '+1c')

root = tk.Tk()
f = MyFrame(root)
f.pack()
root.mainloop()

UPDATE

Instead of adding tag afterward, you can specify tag name when you call insert:

def add_term(self):
    self.txt.insert(tk.SEL_FIRST, '\\term{', 'term')
    self.txt.insert(tk.SEL_LAST, '}', 'term')
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • @BryanOakley, Thank you for the information. I updated the answer accordingly. Really nice to know it. – falsetru Jul 31 '15 at 13:03
1

When you insert the text, you can give it the name of a tag or tags that gets applied to the text when it is inserted:

def addTerm(self):
    self.txt.insert(tk.SEL_FIRST,'\\term{',("markup",))
    self.txt.insert(tk.SEL_LAST,'}', ("markup",))

You then need to configure the tag to have the desired attributes. You can do this when you first create the text widget:

self.txt.tag_configure("markup", foreground="gray")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

It's kinda answered here : How to change the color of certain words in the tkinter text widget?

You need to add a tag in the init function :

self.txt.tag_configure("COLOR", foreground="red")

And you can color it like this :

self.text.tag_add("COLOR", 1.0 , "sel.first")  
self.text.tag_add("COLOR", "sel.last", "end")  

Exemple, using the code provided in the linked post : enter image description here

Community
  • 1
  • 1
lucasg
  • 10,734
  • 4
  • 35
  • 57