I assume you want something like this?
This is the closest I could get for now. It creates three text boxes and uses the anchor
attribute to keep them somewhat in the right place. It doesn't work so good for really wide or narrow letters though. It's not perfect, but it may be a start.
import Tkinter as tk
root = tk.Tk()
c = tk.Canvas(root)
c.pack(expand=1, fill=tk.BOTH)
words = '''I am writing a program that involves displaying some text in a create_text() box on a Tkinter canvas, within a loop. Each word is displayed, then replaced by the next. Sort of like flash cards. I need to color one letter of each word, close to the middle of the word, so that when the user is reading the words their eyes focus on the middle of the word. So if len(i)=1, color i[0], if len(i)>= 2 and <= 5, color i[1], and so on. It needs to be done using the Canvas, and using canvas.create_text(text = i[focus_index],fill = 'red') The result should print like this exaMple (but obviously "m" would be colored red, not be uppercase)'''
words = words.split()
def new_word(i):
if i == len(words):
i = 0
word = words[i]
middle = (len(word)+1)//2
c.itemconfigure(t1, text=word[:middle-1]+' ')
c.itemconfigure(t2, text=word[middle-1:middle])
c.itemconfigure(t3, text=word[middle:])
root.after(100, lambda: new_word(i+1))
t1 = c.create_text(200,100,text='', anchor='e', font=("Courier", 25))
t2 = c.create_text(200,100,text='', anchor='e', font=("Courier", 25), fill='red')
t3 = c.create_text(200,100,text='', anchor='w', font=("Courier", 25))
new_word(0)
root.geometry('400x200+200+200')
root.mainloop()
Ok, using the link from Bryan Oakley's comment, I improved the code some more so it works with any font, not only monospace ones. The code keeps the center of the colored letter at the same place and places the front and back of the word at the correct distance around it.
import Tkinter as tk
import tkFont
root = tk.Tk()
c = tk.Canvas(root)
c.pack(expand=1, fill=tk.BOTH)
fn = "Helvetica"
fs = 24
font = tkFont.Font(family=fn, size=fs)
words = '''I am writing a program that involves displaying some text in a create_text() box on a Tkinter canvas, within a loop. Each word is displayed, then replaced by the next. Sort of like flash cards. I need to color one letter of each word, close to the middle of the word, so that when the user is reading the words their eyes focus on the middle of the word. So if len(i)=1, color i[0], if len(i)>= 2 and <= 5, color i[1], and so on. It needs to be done using the Canvas, and using canvas.create_text(text = i[focus_index],fill = 'red') The result should print like this exaMple (but obviously "m" would be colored red, not be uppercase)'''
words = words.split()
def new_word(i):
if i == len(words):
i = 0
word = words[i]
middle = (len(word)+1)//2
front = word[:middle-1]
letter = word[middle-1:middle]
back = word[middle:]
c.itemconfigure(t1, text=front)
c.itemconfigure(t2, text=letter)
c.itemconfigure(t3, text=back)
c.coords(t1, 200-font.measure(letter)/2, 100)
c.coords(t3, 200+font.measure(letter)/2, 100)
root.after(100, lambda: new_word(i+1))
t1 = c.create_text(200,100,text='', anchor='e', font=font)
t2 = c.create_text(200,100,text='', anchor='c', font=font, fill='red')
t3 = c.create_text(200,100,text='', anchor='w', font=font)
new_word(0)
root.geometry('400x200+200+200')
root.mainloop()