here2share

# Tk_mesmerize5.py

Apr 13th, 2022 (edited)
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. # Tk_mesmerize5.py
  2.  
  3. from tkinter import *
  4. import time
  5. from PIL import Image, ImageTk
  6. from random import shuffle as rs, randint as ri, choice as rc
  7.  
  8. tx =  time.time
  9. ww = 200
  10. hh = 200
  11.  
  12. root = Tk()
  13. root.title("Tk_mesmerize")
  14. root.geometry("%dx%d+-6+-2"%(ww,hh))
  15. canvas = Canvas(width=ww, height=hh)
  16. canvas.pack()
  17.  
  18. def rgb2hex(r,g,b):
  19.     return '#%02X%02X%02X'%(r,g,b)
  20.    
  21. def motion(event):
  22.     mouse_pos.append((event.x, event.y))
  23. root.bind('<B1-Motion>', motion)
  24.  
  25. cols = ww
  26. rows = hh
  27.  
  28. rainbow=[]
  29. def z(r,g,b):
  30.     rainbow.append((r,g,b))
  31. r,g,b=255,0,0
  32. for g in range(256):
  33.     z(r,g,b)
  34. for r in range(254, -1, -1):
  35.     z(r,g,b)
  36. for b in range(256):
  37.     z(r,g,b)
  38. for g in range(254, -1, -1):
  39.     z(r,g,b)
  40. for r in range(256):
  41.     z(r,g,b)
  42. for b in range(254, -1, -1):
  43.     z(r,g,b)
  44. rainbow = rainbow[1:-1]+rainbow[::-1]
  45. max_rgb=len(rainbow)
  46.  
  47. rgb = []
  48. screen = []
  49. xy = {}
  50. for j in range(0,rows):
  51.     for i in range(0,cols):
  52.         xy[i,j] = len(rgb)
  53.         rgb += [(ri(0,9999))]
  54.         if (0 < i < cols-1) and  (0 < j < rows-1):
  55.             screen += [(i,j)]
  56. rs(rgb)
  57. current = rgb[:]
  58.  
  59. mouse_pos = []
  60. image = Image.new("RGB", (ww,hh))
  61.  
  62. def draw():
  63.     image.putdata(rgb)
  64.     photo = ImageTk.PhotoImage(image)
  65.     canvas.create_image(0,0,image=photo,anchor=NW)
  66.     canvas.update()
  67.  
  68. def sides(i,j):
  69.     return ([i-1,j],[i+1,j],[i,j-1],[i,j+1])
  70.  
  71. swap = 1
  72. L = ww*hh
  73.  
  74. #Mainloop
  75. while 1:
  76.     # rs(screen)
  77.     for i,j in screen:
  78.                    
  79.         t = xy[i,j]
  80.         v = 0
  81.         for x,y in sides(i,j):
  82.             v += current[xy[x,y]]
  83.         current[t] = (int(v/4)+50)%max_rgb
  84.        
  85.         rgb[t] = rainbow[current[t]]
  86.        
  87.     rainbow = [rainbow[-1]]+rainbow[:-1]
  88.     draw()
  89.  
Add Comment
Please, Sign In to add comment