【问题标题】:How to make a tkinter canvas polygon transparent?如何使 tkinter 画布多边形透明?
【发布时间】:2020-09-18 20:49:03
【问题描述】:

有没有更好的方法来使多边形的填充透明,而不是使用点画? 举个例子:

import tkinter as tk


class GUI:
    def __init__(self, master, x, y):
        self.master = master
        self.canvas = tk.Canvas(master, width=x, height=y)
        self.canvas.pack()
        self.canvas.create_polygon(10, 10, 10, 20, 200, 300, 250, 150, 10, 10,
        outline="green", fill="blue")
        self.canvas.create_polygon(100, 10, 10, 40, 50, 300, 250, 400, 100, 10,
        outline="green", fill="red", stipple="gray50")


x, y = 500, 500
root = tk.Tk()
gui = GUI(root, x, y)
root.mainloop()

我想让红色多边形的透明度变得逼真,就像任何给定 alpha 参数的软件一样。

【问题讨论】:

  • this的回答。它仅适用于矩形,不适用于多边形。所以我会尝试将其更改为使用多边形。 PS:您的问题不是重复的(以防有人会尝试按原样标记)。
  • 是的,我想要多边形的相同结果。非常感谢

标签: python canvas tkinter polygon transparent


【解决方案1】:

我的解决方案受到this answer 的启发,但针对的是矩形,而不是多边形。

不幸的是,Tkinter 不支持 RGBA,所以不可能只传递填充参数fill="#ff000055"。相反,我们可以使用 PIL 创建一个包含矩形并具有 RGBA 通道的图像。

这是一个例子:

from tkinter import *
from PIL import Image, ImageDraw, ImageTk


def create_polygon(*args, **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", (max(args[::2]), max(args[1::2])))
            ImageDraw.Draw(image).polygon(args, 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(*args, **kwargs)

images = []  # to hold the newly created image(s)        

root = Tk()

canvas = Canvas(width=260, height=310)
canvas.pack()

create_polygon(10, 10, 10, 20, 200, 300, 250, 150, 10, 10, fill="blue", alpha=0.5)
create_polygon(150, 100, 200, 120, 240, 180, 210, 200, 150, 150, 100, 200, fill="blue", alpha=0.2)

root.mainloop()

【讨论】:

    【解决方案2】:

    如果有人在寻找 Tkinter 的透明度和轮廓选项,我在这里找到了它们:
    https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/create_polygon.html

    这些代码行将生成一个带有轮廓的透明多边形。

    root = tk.Tk()
    root.state('zoomed')
    canvas = tk.Canvas(root)
    canvas.pack(fill = "both", expand = True)
    #points = polygon_points
    canvas.create_polygon(points, outline='red', fill='', width = 10)
    

    【讨论】:

      猜你喜欢
      • 2019-07-05
      • 1970-01-01
      • 2019-03-31
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      相关资源
      最近更新 更多