我假設你想要類似this?
這是我現在可以得到的最接近的。它創建三個文本框並使用anchor
屬性將它們保持在正確的位置。儘管如此,它對於非常寬或窄的字母來說效果不佳。這並不完美,但可能是一個開始。
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()
好,利用Bryan Oakley's comment的鏈接,我提高了一些,以便它可以與任何字體,不僅那些等寬的代碼。代碼將彩色字母的中心保留在相同的位置,並將單詞的正面和背面放置在正確的距離。
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()
你就必須創建自己的方法來做到這一點,因爲create_text只需要一個單一的顏色作爲參數,我建議你看一下的方式來對測量文本的大小然後編寫一個方法,將字符串分解並用各自的顏色分別創建各自的顏色 – 2015-03-31 13:56:56
如果這只是爲了告訴用戶在哪裏盯着,爲什麼不只是在文本中放置一個彩色點? – 2015-03-31 13:59:16
所以它是一個速讀器程序,模仿Spritz部件。快速閱讀策略包括將注意力集中在單詞的中間,並將整個單詞用外圍信息處理到大腦中。我需要用這個「焦點」來表達這個單詞,它是一個字母中間的字母,其顏色與其他文字不同。我避免使用單詞突出顯示,因爲Tkinter中有一個突出顯示()方法可以執行其他操作 – zachcapp 2015-03-31 14:56:36