透明背景在Tkinter窗口里

问题描述 投票:9回答:5

有没有办法使用Tkinter在Python 3.x中创建“加载屏幕”?我的意思是像Adobe Photoshop的加载屏幕,具有透明度等等。我设法摆脱框架边框已经使用:

root.overrideredirect(1)

但如果我这样做:

root.image = PhotoImage(file=pyloc+'\startup.gif')
label = Label(image=root.image)
label.pack()

图像显示正常,但灰色窗口背景而不是透明度。

有没有办法为窗口添加透明度,但仍能正确显示图像?

python tkinter transparency
5个回答
5
投票

没有跨平台的方法可以让tkinter中的背景透明化。


24
投票

这是可能的,但它依赖于操作系统。这适用于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()

5
投票

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

Screenshot

(在macOS Sierra 10.12.21上测试)


1
投票

你可以这样做:window.attributes("-transparentcolor", "somecolor")


0
投票

这很简单:使用root.attributes()

在你的情况下,它就像root.attributes("-alpha", 0.5),其中0.5是你想要的透明度,0是完全透明的,1是不透明的。

© www.soinside.com 2019 - 2024. All rights reserved.