【发布时间】:2026-02-02 17:45:01
【问题描述】:
我正在尝试在 Tkinter 中创建一个半透明 (RGBA) 矩形。我使用来自this answer 的代码 sn-p 来制作它,但我无法理解数组在这里的作用。他们最初想要制作多个矩形,所以我理解他们为什么会在这方面使用它,但是当我尝试将数组更改为一个变量时,据我所知,它会做同样的事情,它会停止显示矩形颜色。
这是(稍作修改的)代码,其中包含用于输出的数组:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
images = [] # to hold the newly created image
def create_rectangle(x1, y1, x2, y2, **kwargs): # x & y's dimensions
alpha = int(kwargs.pop('alpha') * 255)
fill = kwargs.pop('fill')
fill = root.winfo_rgb(fill) + (alpha,) # Returns a tuple of the color
image = Image.new('RGBA', (x2-x1, y2-y1), fill)
images.append(ImageTk.PhotoImage(image))
canvas.create_image(x1, y1, image=images[-1], anchor='nw')
canvas.create_rectangle(x1, y1, x2, y2, **kwargs)
canvas = Canvas(width=300, height=300)
canvas.pack()
create_rectangle(50, 50, 250, 150, fill='green', alpha=.5)
root.mainloop()
用它的输出:
同样的代码在image=之后有一个变量而不是images[-1]:
def create_rectangle(x1, y1, x2, y2, **kwargs): # x & y's dimensions
alpha = int(kwargs.pop('alpha') * 255)
fill = kwargs.pop('fill')
fill = root.winfo_rgb(fill) + (alpha,) # Returns a tuple of the color
image = Image.new('RGBA', (x2-x1, y2-y1), fill)
test_var = ImageTk.PhotoImage(image)
canvas.create_image(x1, y1, image=test_var, anchor='nw')
canvas.create_rectangle(x1, y1, x2, y2, **kwargs)
哪些输出:
如果我打印两者的type(),我会得到相同的结果(<class 'PIL.ImageTk.PhotoImage'>),定义相同的变量并附加它(images.append(test_var))会给我正确的结果。只有在canvas.create_image中使用变量时才会出现bug。
有人知道发生了什么吗?
【问题讨论】:
标签: python tkinter canvas python-imaging-library tkinter-canvas