Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_polygon_opacity.py
- from Tkinter import *
- from PIL import Image, ImageDraw, ImageTk
- def create_polygon(zzz, **kwargs):
- if "alpha" in kwargs:
- if "fill" in kwargs:
- # Get and process the input data
- fill = root.winfo_rgb(kwargs.pop("fill")) + (int(kwargs.pop("alpha") * 255),)
- outline = kwargs.pop("outline") if "outline" in kwargs else None
- # We need to find a rectangle the polygon is inscribed in
- # (max(args[::2]), max(args[1::2])) are x and y of the bottom right point of this rectangle
- # and they also are the width and height of it respectively (the image will be inserted into
- # (0, 0) coords for simplicity)
- image = Image.new("RGBA", (400, 400))
- ImageDraw.Draw(image).polygon(zzz, fill=fill, outline=outline)
- images.append(ImageTk.PhotoImage(image)) # prevent the Image from being garbage-collected
- return canvas.create_image(0, 0, image=images[-1], anchor="nw") # insert the Image to the 0, 0 coords
- raise ValueError("fill color must be specified!")
- return canvas.create_polygon(zzz, **kwargs)
- images = [] # to hold the newly created image(s)
- def star_5pt(x,y,s):
- 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
- plot_xy = [(a,b),(f,b),(e,a),(g,b),(h,b),(j,c),(k,d),(e,m),(n,d),(p,c)]
- nova=[]
- for xy in plot_xy:
- xy = (xy[0]*0.002)*s+x,(xy[1]*0.002)*s+y
- nova.append(xy)
- return tuple(nova)
- star = star_5pt(30,60,200)
- root = Tk()
- canvas = Canvas(width=260, height=310)
- canvas.pack()
- pyTk = [(98, 112), (120, 112), (86, 176), (110, 176), (74, 268), (160, 176), (132, 176), (186, 112), (158, 112), (238, 32), (128, 32)]
- create_polygon(pyTk, fill="yellow", alpha=0.9, outline="black")
- create_polygon(star, fill="blue", alpha=0.5)
- root.mainloop()
Add Comment
Please, Sign In to add comment