问题描述
我在Tkinter中放入了一个部分透明的PNG图像,我得到的就是这个
I put in a partially transparent PNG image in Tkinter and all I get is this
如何使右侧的黑色三角形清晰可见? (就像应该的那样)
How do I make the dark triangle on the right clear? (like it's supposed to be)
这是Windows 7(顺便说一句)上的python 2.6.
This is python 2.6 on Windows 7, btw.
推荐答案
以下是一个示例(PNG文件example.png在不同位置具有很多透明性):
Here's an example (the PNG file example.png has lots of transparency in different places):
from Tkinter import Tk, Frame, Canvas
import ImageTk
t = Tk()
t.title("Transparency")
frame = Frame(t)
frame.pack()
canvas = Canvas(frame, bg="black", width=500, height=500)
canvas.pack()
photoimage = ImageTk.PhotoImage(file="example.png")
canvas.create_image(150, 150, image=photoimage)
t.mainloop()
您需要确保图像已存储为RGBA –具有Alpha通道的RGB.您可以使用自己选择的图形程序或PIL( Python Imaging Library ):
You need to make sure the image has been stored as RGBA – RGB with an alpha channel. You can check for that using a graphics program of your choice, or using PIL (Python Imaging Library):
import Image
im = Image.open("button.png")
print im.mode
这应该打印"RGBA".如果没有,则必须确保Alpha通道与图像一起保存.您必须查阅图形程序手册以了解操作方法.
This should print "RGBA". If not, you'll have to make sure the alpha channel is saved with the image. You'll have to consult your graphics program manual for how to do that.
这篇关于如何使Tkinter支持PNG透明性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!