python tkinter button 透明图片,如何使Tkinter支持PNG透明性?

I put in a partially transparent PNG image in Tkinter and all I get is this

b854dfee8cb23393c192715d7fbeb1aa.png

How do I make the dark triangle on the right clear? (like it's supposed to be)

This is python 2.6 on Windows 7, btw.

解决方案

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()

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值