Advertisement
here2share

# Tk_mesmerize2.py

Mar 28th, 2022
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. # Tk_mesmerize2.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
  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.  
  73. #Mainloop
  74. while 1:
  75.     rs(screen)
  76.     for i,j in screen:
  77.         if mouse_pos:
  78.             x,y = mouse_pos.pop(0)
  79.             for x2,y2 in  sides(x,y):
  80.                 try:
  81.                     t = xy[x2,y2]
  82.                     current[t], swap = swap, current[t]
  83.                 except:
  84.                     0
  85.                    
  86.         t = xy[i,j]
  87.        
  88.         top = max([current[xy[x,y]] for x,y in sides(i,j)])
  89.  
  90.         if top > current[t]+60:
  91.             current[t] = (current[t]+57)%max_rgb
  92.         else:
  93.             current[t] = (current[t]+ri(20,30))%max_rgb
  94.        
  95.         rgb[t] = rainbow[current[t]%max_rgb]
  96.    
  97.     draw()
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement