枕头图像不透明

问题描述 投票:0回答:1

我环顾四周,找到了很多答案,但似乎都没有用。我可以使用枕头在Tkinter窗口中获取图片,它是透明背景上的白色圆圈。该图像是png,我用Image.mode发现它是我所知的正确类型(RGBA)。

显示在其位置上的是一个与图像大小相同的白色正方形。

我正在使用python3,应该提到我已使用pip删除了PIL并安装了Pillow。

我尝试使用的圆圈是https://i.imgur.com/I6l0qQY.png

我的代码看起来像这样

from tkinter import *
from PIL import ImageTk, Image

win = Tk()

win.title("Transparency")
win.config(bg='#636363')

load = Image.open("on.png")
render = ImageTk.PhotoImage(file="on.png")

img = Label(win, image=render)
img.image = render

img.place(x=0, y=0)

win.mainloop()

除了图像是透明的以外,一切正常。

python python-3.x tkinter python-imaging-library
1个回答
0
投票

透明度是有效的,只是因为您没有为Label小部件指定背景色,所以您看不到它的效果。

尝试一下:

from tkinter import *
from PIL import ImageTk, Image

win = Tk()

win.title("Transparency")
win.config(bg='#636363')

load = Image.open("on.png")
render = ImageTk.PhotoImage(file="on.png")

img = Label(win, image=render, bg='red')  # Added background.
img.image = render

img.place(x=0, y=0)

win.mainloop()

结果截图:

screenshot of result

© www.soinside.com 2019 - 2024. All rights reserved.