问题描述
我已经研究出如何在按钮上放置图像,即将其放置在标签的顶部(我想我可能会花很长的时间这样做,因为某些时候我无法在Mac上安装PIL原因).无论如何,它在一定程度上应该可以正常工作-我的问题是它在两边都添加了空白,然后图像本身没有显示其透明背景.
I have worked out how to have an image for a button, thats positioned on top of a label (I think I maybe doing it the long-winded way because I can't install PIL on my Mac for some reason). Anyhow, it works as it should to a certain degree - the problem I have is it's adding white space either side, and then the image itself is not displaying its transparent background.
我正在使用的代码如下:
The code I am using is as follows:
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等图像的透明度.
From what I understand tkinter natively supports transparency on images like GIF.
我对您的代码做了一些修改,但是对我来说确实有用.也许您如何设置代码有问题.您的标签上也有一个按钮.我认为您不需要同时拥有两者.您可以在所需的位置创建按钮.
I chopped up your code a little but it does work for me. Maybe there is a problem with how you have set up your code. Your label also has a button in it. I don't think you need to have both. You can just created the button where you want it.
仅供参考,我创建了一个标签和一个按钮,按钮和按钮分别包装在黑色背景的不同面上,以显示图像的透明度.
Just for reference I created a Label and a Button packed on different sides with a black background to show the transparency of the image.
这是我用来测试具有透明性的gif
的代码.为了以防万一,我在python 3.6和2.7上都对此进行了测试.
Here is the code I used to test a gif
I have that has transparency. I tested this on both python 3.6 and 2.7 just in case.
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
Update: I used the gif you link in the comment
这是结果.
更新:
进行更多的挖掘之后,我发现了适用于Mac OS的方法:
After doing some more digging I found what might work for Mac OS:
我现在没有Mac可以测试,所以请告诉我这是否适合您:
I don't have a Mac to test on right now so let me know if this works for you:
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()
这篇关于Tkinter-按钮图像透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!