Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_no_rnd_import.py
- import tkinter as tk
- width, height = 400, 400
- grid_size = 40
- cell_width = width // grid_size
- cell_height = height // grid_size
- colors = ["red", "green", "blue", "yellow", "purple", "orange"]
- L = len(colors)
- colors_cache = [['grey' for _ in range(grid_size)] for _ in range(grid_size)]
- root = tk.Tk()
- root.title("# tk_rand_vs_perm.py")
- canvas = tk.Canvas(root, width=width, height=height)
- canvas.pack()
- def draw_grid(i,j):
- x0 = i * cell_width
- y0 = j * cell_height
- x1 = x0 + cell_width
- y1 = y0 + cell_height
- canvas.create_rectangle(x0, y0, x1, y1, fill=colors_cache[i][j], outline="")
- def permute_colors():
- canvas.delete("all")
- flat = sum(colors_cache, [])
- flat = flat[2:] + flat[:2] # Rotate colors
- for i in range(grid_size):
- flat = flat[i:] + flat[:i] # Rotate colors
- for j in range(grid_size):
- colors_cache[i][j] = flat[i * grid_size + j]
- draw_grid(i,j)
- def set_colors():
- canvas.delete("all")
- n = 0
- for i in range(grid_size):
- for j in range(grid_size):
- colors_cache[i][j] = colors[n % L]
- draw_grid(i,j)
- n += 1
- set_colors()
- while 1:
- permute_colors()
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment