here2share

# tk_no_rnd_import.py

Sep 9th, 2025
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. # tk_no_rnd_import.py
  2. import tkinter as tk
  3.  
  4. width, height = 400, 400
  5. grid_size = 40
  6. cell_width = width // grid_size
  7. cell_height = height // grid_size
  8. colors = ["red", "green", "blue", "yellow", "purple", "orange"]
  9. L = len(colors)
  10.  
  11. colors_cache = [['grey' for _ in range(grid_size)] for _ in range(grid_size)]
  12.  
  13. root = tk.Tk()
  14. root.title("# tk_rand_vs_perm.py")
  15. canvas = tk.Canvas(root, width=width, height=height)
  16. canvas.pack()
  17.  
  18. def draw_grid(i,j):
  19.     x0 = i * cell_width
  20.     y0 = j * cell_height
  21.     x1 = x0 + cell_width
  22.     y1 = y0 + cell_height
  23.     canvas.create_rectangle(x0, y0, x1, y1, fill=colors_cache[i][j], outline="")
  24.  
  25. def permute_colors():
  26.     canvas.delete("all")
  27.     flat = sum(colors_cache, [])
  28.     flat = flat[2:] + flat[:2]  # Rotate colors
  29.     for i in range(grid_size):
  30.         flat = flat[i:] + flat[:i]  # Rotate colors
  31.         for j in range(grid_size):
  32.             colors_cache[i][j] = flat[i * grid_size + j]
  33.             draw_grid(i,j)
  34.  
  35. def set_colors():
  36.     canvas.delete("all")
  37.     n = 0
  38.     for i in range(grid_size):
  39.         for j in range(grid_size):
  40.             colors_cache[i][j] = colors[n % L]
  41.             draw_grid(i,j)
  42.             n += 1
  43.  
  44. set_colors()
  45.  
  46. while 1:
  47.     permute_colors()
  48.     root.update()
Advertisement
Add Comment
Please, Sign In to add comment