Advertisement
here2share

# Tk_color_substr.py

Oct 27th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. # Tk_color_substr.py
  2.  
  3. import Tkinter as tk
  4.  
  5. root = tk.Tk()
  6.  
  7. c = tk.Canvas(root)
  8. c.pack(expand=1, fill=tk.BOTH)
  9.  
  10. words = '''This is a demo program that involves displaying some text in a box via its Tkinter canvas, within a loop. Each word is displayed then replaced by the next sort of like flash cards. One letter of each word, close to the middle of the word, so that when a person is reading the words their eyes focus on the middle of each word'''
  11. words = words.split()
  12.  
  13. def new_word(i):
  14.     if i == len(words):
  15.         i = 0
  16.  
  17.     word = words[i]
  18.     if not word[-1].isalnum():
  19.         word = word[:-1]
  20.     middle = (len(word)+1)//2
  21.     c.itemconfigure(t1, text=word[:middle-1]+' ')
  22.     c.itemconfigure(t2, text=word[middle-1:middle])
  23.     c.itemconfigure(t3, text=word[middle:])
  24.  
  25.     root.after(200, lambda: new_word(i+1))
  26.  
  27. tt = ("Courier", 40, 'bold')
  28. t1 = c.create_text(200,100,text='', anchor='e', font=tt, fill='darkgray')
  29. t2 = c.create_text(200,100,text='', anchor='e', font=tt, fill='darkorange')
  30. t3 = c.create_text(200,100,text='', anchor='w', font=tt, fill='darkgray')
  31. new_word(0)
  32.  
  33. root.geometry('400x200+200+200')
  34. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement