2015-10-24 30 views
0

我試圖實現一個使用PIL並閱讀具有透明度的PNG的透明按鈕。我已經遵循了我可以找到的所有提示,但按鈕仍然顯示不透明。 Screenshot of button in GIMPPython 2.7 - 通過PNG文件實現帶有透明度的Tkinter按鈕

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

`

回答

0

把一個窗口到畫布覆蓋背景圖像。如果我在畫布中創建了圖像,我可以正確地將按鈕的圖像放在畫布上(使用透明度),但不能使用實際的按鈕小部件。我的下一步是學習如何讓圖像對鼠標點擊作出反應,以便我可以模擬一些基本的按鈕功能。