简体   繁体   中英

Implementing Tkinter button with transparency via PNG file

I am trying to implement a button that has transparency using PIL and reading a PNG with transparency. I've followed all the tips I can find, but the button still shows up without transparency.

Screenshot of button in GIMP

Screenshot of Python output

Here is the code:

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

Putting a window onto the canvas overwrites the background image. If I create_image in the canvas, I can correctly put the image of the button on the canvas (with transparency), but not an actual Button widget. My next step is to learn how to have the image react to mouseclicks so I can emulate some basic Button functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM