Python Tkinter 窗口中的透明背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19080499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transparent background in a Tkinter window
提问by forumfresser
Is there a way to create a "Loading Screen" in Python 3.x using Tkinter? I mean like the loading screen for Adobe Photoshop, with transparency and so on. I managed to get rid of the frame border already using:
有没有办法使用 Tkinter 在 Python 3.x 中创建“加载屏幕”?我的意思是像 Adobe Photoshop 的加载屏幕,具有透明度等等。我设法摆脱了已经使用的框架边框:
root.overrideredirect(1)
But if I do this:
但如果我这样做:
root.image = PhotoImage(file=pyloc+'\startup.gif')
label = Label(image=root.image)
label.pack()
the image displays fine, but with the grey window background instead of transparency.
图像显示正常,但窗口背景为灰色而不是透明度。
Is there a way of adding transparency to a window, but still displaying the image correctly?
有没有办法为窗口添加透明度,但仍能正确显示图像?
采纳答案by Bryan Oakley
There is no cross-platform way to make just the background transparent in tkinter.
没有跨平台的方法可以在 tkinter 中使背景透明。
回答by vinit_ivar
It's simple: use root.attributes()
很简单:使用 root.attributes()
In your case, it'd be something like root.attributes("-alpha", 0.5)
where 0.5 is the transparency you want, 0 being fully transparent to 1 being opaque.
在您的情况下,它类似于root.attributes("-alpha", 0.5)
0.5 是您想要的透明度,0 是完全透明的,1 是不透明的。
回答by dln385
It is possible, but it's OS-dependent. This will work in Windows:
这是可能的,但它依赖于操作系统。这将在 Windows 中工作:
import Tkinter as tk # Python 2
import tkinter as tk # Python 3
root = tk.Tk()
# The image must be stored to Tk or it will be garbage collected.
root.image = tk.PhotoImage(file='startup.gif')
label = tk.Label(root, image=root.image, bg='white')
root.overrideredirect(True)
root.geometry("+250+250")
root.lift()
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")
label.pack()
label.mainloop()
回答by Josselin
Here is a solution for macOS:
这是macOS的解决方案:
import tkinter as tk
root = tk.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)
# Turn off the window shadow
root.wm_attributes("-transparent", True)
# Set the root window background color to a transparent color
root.config(bg='systemTransparent')
root.geometry("+300+300")
# Store the PhotoImage to prevent early garbage collection
root.image = tk.PhotoImage(file="photoshop-icon.gif")
# Display the image on a label
label = tk.Label(root, image=root.image)
# Set the label background color to a transparent color
label.config(bg='systemTransparent')
label.pack()
root.mainloop()
(tested on macOS Sierra 10.12.21)
(在 macOS Sierra 10.12.21 上测试)
回答by Jakub Bláha
You can do this:window.attributes("-transparentcolor", "somecolor")
你可以这样做:window.attributes("-transparentcolor", "somecolor")
回答by Ryan Gurnick
For just doing a single image you can do the following.
对于仅制作单个图像,您可以执行以下操作。
label = Label(root)
label.config(image='image.gif')
label.config(bg='systemTransparent')
this appears to allow the gif and the alpha channel to shine, on macOS specifically.
这似乎允许 gif 和 alpha 通道在 macOS 上发光。