Tkinter:将具有透明背景的图像添加到按钮

Tkinter: Adding Image with transparent background to Button

希望你们一切都好。我有一个问题希望你能帮忙解决。

我正在尝试在 tkinter 中构建棋盘游戏。这将有不同形状的瓷砖被放置在背景图像顶部的正方形中。我已设法使用 tk.PhotoImage 和 tk.Label 添加背景,并使用 ImageTk.PhotoImage.

正确调整了图块图像的大小

但是,当我将图块放在板上时,所有透明度都消失了,取而代之的是单调的灰色。

最小代码:

from PIL import Image,ImageTk
import tkinter as tk

def tile_push():
    pass

# Create background image
root = tk.Tk()
root.geometry("390x500") # Size of background board
background_image = tk.PhotoImage(file="Gameboard.png")
background_label = tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

# Create button
im = Image.open("Chick.png").resize((100,100))
image_player_1 = ImageTk.PhotoImage(im)
b = tk.Button(root, image=image_player_1, command=tile_push, borderwidth=0, highlightthickness=0)
b.place(x=140, y=258, width=115, height=115)
tk.mainloop()

SO 上的类似问题显示了如何将背景设置为黑色,但我需要背景是透明的。

任何帮助将不胜感激!

感谢acw的支持,我现在已经成功了。这就是我所做的:

from PIL import Image,ImageTk
import tkinter as tk

def tile_push(*args, **kwargs):
    print("It's alive!")

# Create background image
root = tk.Tk()
root.geometry("390x500") # Size of background board
canvas = tk.Canvas(root, width=390, height=500)
canvas.place(x=0,y=0)
background_image = tk.PhotoImage(file="Gameboard.png")
canvas.create_image((0,0), anchor="nw", image=background_image)

# Create button
im = Image.open("Chick.png").resize((115,115))
imTk =  ImageTk.PhotoImage(im)
chick = canvas.create_image((140,258), anchor="nw", image=imTk)
canvas.tag_bind(chick, '<Button-1>', tile_push)
tk.mainloop()