here2share

# Tk_polygon_opacity.py

Feb 13th, 2021 (edited)
1,383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. # Tk_polygon_opacity.py
  2.  
  3. from Tkinter import *
  4. from PIL import Image, ImageDraw, ImageTk
  5.  
  6. def create_polygon(zzz, **kwargs):
  7.     if "alpha" in kwargs:        
  8.         if "fill" in kwargs:
  9.             # Get and process the input data
  10.             fill = root.winfo_rgb(kwargs.pop("fill")) + (int(kwargs.pop("alpha") * 255),)
  11.             outline = kwargs.pop("outline") if "outline" in kwargs else None
  12.  
  13.             # We need to find a rectangle the polygon is inscribed in
  14.             # (max(args[::2]), max(args[1::2])) are x and y of the bottom right point of this rectangle
  15.             # and they also are the width and height of it respectively (the image will be inserted into
  16.             # (0, 0) coords for simplicity)
  17.             image = Image.new("RGBA", (400, 400))
  18.             ImageDraw.Draw(image).polygon(zzz, fill=fill, outline=outline)
  19.  
  20.             images.append(ImageTk.PhotoImage(image))  # prevent the Image from being garbage-collected
  21.             return canvas.create_image(0, 0, image=images[-1], anchor="nw")  # insert the Image to the 0, 0 coords
  22.         raise ValueError("fill color must be specified!")
  23.     return canvas.create_polygon(zzz, **kwargs)
  24.  
  25. images = []  # to hold the newly created image(s)
  26.  
  27. def star_5pt(x,y,s):
  28.     a,b,c,d,e,f,g,h,j,k,m,n,p = 0,180,294,476,250,192,308,500,340,402,357,96,157
  29.     plot_xy = [(a,b),(f,b),(e,a),(g,b),(h,b),(j,c),(k,d),(e,m),(n,d),(p,c)]
  30.     nova=[]
  31.     for xy in plot_xy:
  32.         xy = (xy[0]*0.002)*s+x,(xy[1]*0.002)*s+y
  33.         nova.append(xy)
  34.     return tuple(nova)
  35.  
  36. star = star_5pt(30,60,200)
  37.  
  38. root = Tk()
  39.  
  40. canvas = Canvas(width=260, height=310)
  41. canvas.pack()
  42.  
  43.  
  44. pyTk = [(98, 112), (120, 112), (86, 176), (110, 176), (74, 268), (160, 176), (132, 176), (186, 112), (158, 112), (238, 32), (128, 32)]
  45.  
  46. create_polygon(pyTk, fill="yellow", alpha=0.9, outline="black")
  47. create_polygon(star, fill="blue", alpha=0.5)
  48.  
  49. root.mainloop()
Add Comment
Please, Sign In to add comment