通过PNG文件实现透明度的Tkinter按钮

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

我正在尝试使用PIL实现一个具有透明度的按钮并读取具有透明度的PNG。我已经按照我能找到的所有提示,但按钮仍然显示没有透明度。

Screenshot of button in GIMP

Screenshot of Python output

这是代码:

from Tkinter import *
from PIL import Image, ImageTk

root = Tk()
root.resizable(width=FALSE, height = FALSE)

global background, redbutton, rb1


rb1 =ImageTk.PhotoImage(file="test.png")

#confirm the png file is correct format
im = Image.open("test.png")
print im.mode

bg = PhotoImage(file = "background.gif")

GameWin = Canvas(root, bd = 2, height = 600, width = 450)
GameWin.create_image(0,0, image = bg, anchor = NW)

rb = Button(GameWin, bd=0, image=rb1)

# create a window in the canvas for the button
rb_win = GameWin.create_window(10, 10, anchor=NW, window=rb)


GameWin.pack()

root.mainloop()
python python-2.7 tkinter png transparency
1个回答
0
投票

将窗口放在画布上会覆盖背景图像。如果我在画布中创建了create_image,我可以正确地将按钮的图像放在画布上(具有透明度),但不是实际的Button小部件。我的下一步是学习如何让图像对鼠标点击做出反应,这样我就可以模拟一些基本的Button功能。

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