Advertisement
here2share

# Tk_dither.py

Oct 11th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. # Tk_dither.py
  2. # test program to simulate color dithering algorithms.
  3.  
  4. from Tkinter import *
  5. import random as r
  6.  
  7. # Number of squares in x and y dimensions.
  8. nx = 40
  9. ny = 40
  10.  
  11. # Size of square side in pixels.
  12. ss = 10
  13.  
  14. # Total dimensions of display.
  15. dx = nx * ss
  16. dy = ny * ss
  17.  
  18. root = Tk()
  19. root.geometry("%sx%s" % (dx, dy))
  20. c = Canvas(root, width=800, height=600)
  21. c.pack()
  22.  
  23. def make_color():
  24.     return '#' + r.choice(['ff0000', '00ff00', '0000ff','ffffff','cccccc'])
  25.  
  26.  
  27. def make_square(x, y, color):
  28.     return c.create_rectangle(x * ss, y * ss, (x+1) * ss, (y+1) * ss,
  29.                               fill=color, outline=color)
  30.  
  31. def make_squares():
  32.     squares = []
  33.     for x in range(nx):
  34.         squares.append([])
  35.         for y in range(ny):
  36.             squares[x].append(make_square(x, y, make_color()))
  37.     return squares
  38.  
  39. squares = make_squares()
  40.  
  41. def change_colors(squares):
  42.     '''Change each square color independent of other squares.'''
  43.     for x in range(len(squares)):
  44.         for y in range(len(squares[x])):
  45.             h = squares[x][y]
  46.             color = make_color()
  47.             c.itemconfig(h, fill=color, outline=color)
  48.  
  49. done = False
  50. def quit_handler(event):
  51.     global done
  52.     done = True
  53.  
  54. # Left mouse button click on canvas terminates the program.
  55. root.bind('<Button-1>', quit_handler)
  56.  
  57. while not done:
  58.     change_colors(squares)
  59.     root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement