here2share

# tk_inward_cache_demo.py

Sep 21st, 2025 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. # tk_inward_cache_demo.py
  2.  
  3. import tkinter as tk
  4.  
  5. WW, HH = 600, 600
  6. UNIT = 10
  7.  
  8. root = tk.Tk()
  9. root.geometry('+0+0')
  10. canvas = tk.Canvas(root, width=WW, height=HH)
  11. canvas.pack()
  12.  
  13. for y in range(0, HH, UNIT):
  14.     for x in range(0, WW, UNIT):
  15.         canvas.create_rectangle(x, y, x + UNIT, y + UNIT, outline='gray')
  16.  
  17. cache = []
  18. def plot_unit(x, y):
  19.     canvas.create_rectangle(x * UNIT, y * UNIT, (x + 1) * UNIT, (y + 1) * UNIT, fill='red', outline='')
  20.     cache.append((x, y))
  21.     root.update()
  22.  
  23. def test():
  24.     bb = (WW + 1) // UNIT
  25.     i = 0
  26.     j = bb
  27.     while j:
  28.         for x in range(i, j):
  29.             plot_unit(x, j - 1)
  30.             plot_unit(x, bb - j)
  31.         i += 1
  32.         j -= 1
  33.         for y in range(i, j):
  34.             plot_unit(j, y)
  35.             plot_unit(bb - j - 1, y)
  36.  
  37. test()
Advertisement
Add Comment
Please, Sign In to add comment