【问题标题】:Tkinter - Button Image Transparent BackgroundTkinter - 透明背景的按钮图像
【发布时间】:2017-11-04 00:34:20
【问题描述】:

我已经想出了如何为一个按钮设置一个图像,它位于标签的顶部(我想我可能会用冗长的方式来做这件事,因为我无法在我的 Mac 上安装 PIL原因)。无论如何,它在一定程度上可以正常工作 - 我遇到的问题是它在两边都添加了空白,然后图像本身没有显示其透明背景。

我使用的代码如下:

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

【问题讨论】:

  • 您确定您使用的是 python 2.7,因为您将小写的 tkinter 导入为 python 3。

标签: python python-2.7 tkinter label transparency


【解决方案1】:

据我了解,tkinter 原生支持 GIF 等图像的透明度。

我对你的代码做了一点修改,但它确实对我有用。也许您设置代码的方式存在问题。您的标签中还有一个按钮。我认为你不需要两者兼得。您可以在需要的位置创建按钮。

仅供参考,我创建了一个标签和一个包装在不同侧面的按钮,黑色背景以显示图像的透明度。

这是我用来测试gif 的代码,我拥有它具有透明度。为了以防万一,我在 python 3.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

这是结果。

更新:

在进行更多挖掘后,我发现了适用于 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()

【讨论】:

  • 那一定是mac的东西。我得再研究一下。
  • 啊,这可以解释,我正在使用这张图片image.prntscr.com/image/d036fce797e94291bd38040bd7eb4bd0.gif
  • 啊。如果您的 gif 实际上是透明的,那将会有所帮助 XD
  • 这很有趣。好吧,当我回到家时,我可能必须安装一个虚拟 hackintosh 来玩,所以我有一些东西可以测试 mac 问题。如果我找到解决方案,我会更新我的答案。
  • @ReblochonMasque 不,我还没有找到 MAC 的解决方案。我知道 MAC 有很多与 Tkinter GUI 相关的问题。我认为在 mac 上花费的时间比我在 tkinter 上工作的时间更多的 MAC 人可能会提供帮助,但我还没有找到解决方案。
猜你喜欢
  • 1970-01-01
  • 2020-12-09
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多