Advertisement
here2share

# Tk_ani_mosaic.py

Oct 20th, 2016
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. # Tk_ani_mosaic.py
  2.  
  3. import Tkinter as tk
  4. import random
  5.  
  6. r = tk.Tk()
  7.  
  8. c = tk.Canvas(r, width=200, height=210)
  9. c.pack(fill=tk.BOTH, expand=tk.YES)
  10.  
  11. # draw 400 rectangles
  12. objs = []
  13. for i in range(20):
  14.     for j in range(20):
  15.         obj = c.create_rectangle(i*10, j*10, i*10+10, j*10+10,
  16. fill='#888888', outline='#888888')
  17.         objs.append(obj)
  18.  
  19. def run():
  20.     # loop indefinitely and randomly change the canvas item colours
  21.     while True:
  22.         obj = random.choice(objs)
  23.         red = random.randint(0,255)
  24.         green = random.randint(0,255)
  25.         blue = random.randint(0,255)
  26.         colour = '#%02x%02x%02x' %(red, green, blue)
  27.         c.itemconfigure(obj, fill=colour, outline=colour)
  28.         r.update()
  29.  
  30. tk.Button(r, text='Run', command=run).pack()
  31.  
  32. r.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement