3

So I wanted to make a text editor and I've gotten stuck on this part, I want it to change the color of a certain word thats typed in in real time, for example: Lets say I type print I want it to automatically change the foreground color from the default black color to lets say blue. I Don't really know if you have to use tag_configure to do this but if so can someone please help me work it, thanks.

Code:

from tkinter import *

root = Tk()

text = Text(root)
text.grid(row=0)

def Event(event):
    text.tag_configure("print", foreground="blue")

#This is a KeyBind to trigger the Function: Event
root.bind("<Return>", Event)

root.mainloop()
Nimantha
  • 6,405
  • 6
  • 28
  • 69
MoziakBeats
  • 31
  • 1
  • 5
  • 3
    Possible duplicate of [How to change the color of certain words in the tkinter text widget?](https://stackoverflow.com/questions/14786507/how-to-change-the-color-of-certain-words-in-the-tkinter-text-widget) – m13op22 May 02 '19 at 17:06
  • 1
    First of all, `Event` is already a python tkinter class. – Jacob May 02 '19 at 17:59
  • `tag_configure` is only to configure tag but you have to assign tag to word in text to change its color. – furas May 02 '19 at 21:32

2 Answers2

2

If you want to change the color of text as you're typing it in, you need to do a few things:

  1. define a tag with the desired coloration. You're already got:

    text.tag_configure("print", foreground="blue")

which works. You probably want to be able to trigger other events which change the color to apply for the newly typed text, or your editor will be quite boring. You can have a few buttons with a different color for each, etc. You'll probably want multiple tags, because if you change the config of the "print" tag, everything that is already tagged with that tag name will change to match the new config.

  1. then you have to apply the tag to the text as you're typing it in. There are probably a few ways to do this, but you can process each character as it's typed by setting up a binding for "<Key>" events. Each time the event is triggered, because a new char has been typed, go back and apply the tag "print" to that character.

So, something along the lines of:

root.bind("<Key>", on_key)

along with:

def on_key(event=None):
    text.tag_add("print", "INSERT - 1c", "INSERT")

That should roughly give you what you want, but as it's set up, it still won't be too interesting. You can have multiple tags, each configured to look visually different (different foreground and background colors, etc), and apply different tags to what you're typing in, as you see fit.

INSERT is a special Text widget mark which represents where the insertion point/cursor is in the widget. As you type, INSERT keeps moving to the right, always just after what you've typed. So, "INSERT - 1c" is the previous location and points to the character that was just typed.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
GaryMBloom
  • 5,350
  • 1
  • 24
  • 32
0

tag_configure is only to configure tag but you have to assign tag to word in text to change its color.

You can use text.tag_add(tag_name, text_start, text_end) to add tag to selected text.

Problem can be how to find text_startand text_end for word or line.

Tkinter has special values like end, insert, wordstart, etc.

I differnt tags to change color for last line and for last word. It shows different situations.

I use -2c to skip last Return but in other system it can need only -1c. Without -2c it will use color when you put new chars in new line.

If you put space after last word in line the it doesn't find last word.

If you change order of tag_configure then it may not work.

import tkinter as tk

# --- functions ---

def on_return(event):
    # -2c (-2chars) to skip `Return`

    # red color for last line
    text.tag_add('red_fg', 'end-2c linestart', 'end-2c')

    # blue color for last word
    text.tag_add('blue_fg', 'insert-2c wordstart', 'end-2c')


# --- main ---

root = tk.Tk()

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

# tag's order can be important
text.tag_configure("red_fg", foreground="red")
text.tag_configure("blue_fg", foreground="blue")

root.bind("<Return>", on_return)

root.mainloop()

effbot.org: Text

Tcl/Tk: Text indices

TkDocs: Text -> Text Positions and Indices

furas
  • 134,197
  • 12
  • 106
  • 148