【问题标题】:Implementing Tkinter button with transparency via PNG file通过PNG文件实现具有透明度的Tkinter按钮
【发布时间】:2016-01-23 23:48:04
【问题描述】:

我正在尝试使用 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】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 2011-10-20
      • 2017-10-07
      • 1970-01-01
      • 2010-09-23
      • 2018-11-06
      相关资源
      最近更新 更多