Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_ani_mosaic.py
- import Tkinter as tk
- import random
- r = tk.Tk()
- c = tk.Canvas(r, width=200, height=210)
- c.pack(fill=tk.BOTH, expand=tk.YES)
- # draw 400 rectangles
- objs = []
- for i in range(20):
- for j in range(20):
- obj = c.create_rectangle(i*10, j*10, i*10+10, j*10+10,
- fill='#888888', outline='#888888')
- objs.append(obj)
- def run():
- # loop indefinitely and randomly change the canvas item colours
- while True:
- obj = random.choice(objs)
- red = random.randint(0,255)
- green = random.randint(0,255)
- blue = random.randint(0,255)
- colour = '#%02x%02x%02x' %(red, green, blue)
- c.itemconfigure(obj, fill=colour, outline=colour)
- r.update()
- tk.Button(r, text='Run', command=run).pack()
- r.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement