Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_dither.py
- # test program to simulate color dithering algorithms.
- from Tkinter import *
- import random as r
- # Number of squares in x and y dimensions.
- nx = 40
- ny = 40
- # Size of square side in pixels.
- ss = 10
- # Total dimensions of display.
- dx = nx * ss
- dy = ny * ss
- root = Tk()
- root.geometry("%sx%s" % (dx, dy))
- c = Canvas(root, width=800, height=600)
- c.pack()
- def make_color():
- return '#' + r.choice(['ff0000', '00ff00', '0000ff','ffffff','cccccc'])
- def make_square(x, y, color):
- return c.create_rectangle(x * ss, y * ss, (x+1) * ss, (y+1) * ss,
- fill=color, outline=color)
- def make_squares():
- squares = []
- for x in range(nx):
- squares.append([])
- for y in range(ny):
- squares[x].append(make_square(x, y, make_color()))
- return squares
- squares = make_squares()
- def change_colors(squares):
- '''Change each square color independent of other squares.'''
- for x in range(len(squares)):
- for y in range(len(squares[x])):
- h = squares[x][y]
- color = make_color()
- c.itemconfig(h, fill=color, outline=color)
- done = False
- def quit_handler(event):
- global done
- done = True
- # Left mouse button click on canvas terminates the program.
- root.bind('<Button-1>', quit_handler)
- while not done:
- change_colors(squares)
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement