Python Tkinter-按钮图像透明背景
我已经研究出了如何在标签上放置按钮的图像(我想我可能会用冗长的方式,因为我因为某种原因无法在我的Mac上安装PIL)。无论如何,它在一定程度上是正常工作的——我遇到的问题是,它在两侧添加了空白,然后图像本身没有显示其透明背景 我使用的代码如下:Python Tkinter-按钮图像透明背景,python,python-2.7,tkinter,label,transparency,Python,Python 2.7,Tkinter,Label,Transparency,我已经研究出了如何在标签上放置按钮的图像(我想我可能会用冗长的方式,因为我因为某种原因无法在我的Mac上安装PIL)。无论如何,它在一定程度上是正常工作的——我遇到的问题是,它在两侧添加了空白,然后图像本身没有显示其透明背景 我使用的代码如下: from tkinter import * #from PIL import Image root = Tk() #Removes the title bar and places over mac top bar root.tk.call(":
from tkinter import *
#from PIL import Image
root = Tk()
#Removes the title bar and places over mac top bar
root.tk.call("::tk::unsupported::MacWindowStyle", "style", root._w, "plain", "none")
# Makes the app full screen
#root.wm_attributes('-fullscreen', 1)
root.geometry('{}x{}'.format(480, 320))
#root.attributes('-topmost', True)
def quitApp():
# mlabel = Label (root, text = 'Close').pack()
root.destroy()
background_img = PhotoImage(file="images/bg.gif")
scanBtn_img = PhotoImage(file="images/scanBtn.gif")
background = Label(root,
compound = CENTER,
quitButton = Button(image=scanBtn_img, command = quitApp).pack(),
image = background_img).pack(side="right")
background.image = background_img # keep a reference!
root.mainloop()
据我所知,tkinter本机支持GIF等图像的透明度 我把你的代码切碎了一点,但它确实对我有用。也许你的代码设置有问题。标签上还有一个按钮。我认为你不需要两者兼得。您可以在需要的地方创建按钮 仅供参考,我创建了一个标签和一个按钮,用黑色背景包装在不同的侧面,以显示图像的透明度 下面是我用来测试具有透明度的
gif
的代码。为了以防万一,我在Python3.6和2.7上都进行了测试
from tkinter import *
root = Tk()
def quitApp():
root.destroy()
background_img = PhotoImage(file="Colors/sa.gif")
scanBtn_img = PhotoImage(file="Colors/sa.gif")
background = Label(root,bg='black', image = background_img).pack(side = RIGHT)
quitButton = Button(bg='black', image=scanBtn_img, command = quitApp).pack(side = LEFT)
backgroundimage = background_img # keep a reference!
root.mainloop()
更新:我在评论中使用了gif you链接
结果如下
更新:
在进一步挖掘之后,我找到了适用于Mac OS的方法:
我现在没有Mac电脑可供测试,因此请让我知道这是否适用于您:
from tkinter import *
root = Tk()
# Hide the root window drag bar and close button
root.overrideredirect(True)
# Make the root window always on top
root.wm_attributes("-topmost", True)
# Make the window content area transparent
root.wm_attributes("-transparent", True)
# Set the root window background color to a transparent color
root.config(bg='systemTransparent')
def quitApp():
root.destroy()
background_img = PhotoImage(file="Colors/1.gif")
scanBtn_img = PhotoImage(file="Colors/1.gif")
background = Label(root,bg='black', image = background_img)
background.pack(side = RIGHT)
background.config(bg='systemTransparent')
quitButton = Button(bg='black', image=scanBtn_img, command = quitApp)
quitButton.pack(side = LEFT)
quitButton.config(bg='systemTransparent')
backgroundimage = background_img # keep a reference!
root.mainloop()
您确定您使用的是Python2.7吗?因为您正在将tkinter以小写形式导入为Python3。这一定是mac的事情。我得再仔细研究一下。啊,这也许可以解释,我用的是这个图像啊。如果你的
gif
是透明的,那会很有帮助,这很有趣。嗯,当我回到家时,我可能不得不安装一个虚拟的hackintosh来玩,这样我就有了测试mac问题的工具。如果我找到了解决方案,我将更新我的答案。@ReblochonMasque不,我还没有找到MAC的解决方案。我知道MAC有很多与Tkinter GUI相关的问题。我认为一个在MAC上花的时间比我在tkinter上花的时间多的MAC用户可能会有所帮助,但我还没有找到解决方案。