透明背景在Tkinter窗口里

时间:2013-09-29 16:37:40

标签: python tkinter transparency

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

root.overrideredirect(1)

但如果我这样做:

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

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

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

8 个答案:

答案 0 :(得分: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()

答案 1 :(得分:5)

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

答案 2 :(得分: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上测试)

答案 3 :(得分:1)

您可以执行此操作:window.attributes("-transparentcolor", "somecolor")

答案 4 :(得分:0)

很简单:使用root.attributes()

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

答案 5 :(得分:0)

对于仅制作单个图像,您可以执行以下操作。

label = Label(root)
label.config(image='image.gif')
label.config(bg='systemTransparent')

这似乎允许gif和alpha通道特别在macOS上发光。

答案 6 :(得分:0)

Linux 方式 - 安装 pqiv

“Linux 方式”似乎正在安装另一个软件包:

$ sudo apt install pqiv

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  pqiv
0 upgraded, 1 newly installed, 0 to remove and 11 not upgraded.
Need to get 34.6 kB of archives.
After this operation, 136 kB of additional disk space will be used.
Get:1 http://ca.archive.ubuntu.com/ubuntu xenial/universe amd64 pqiv amd64 2.2-1 [34.6 kB]
Fetched 34.6 kB in 0s (96.0 kB/s)
Selecting previously unselected package pqiv.
(Reading database ... 442035 files and directories currently installed.)
Preparing to unpack .../archives/pqiv_2.2-1_amd64.deb ...
Unpacking pqiv (2.2-1) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for mime-support (3.59ubuntu1) ...
Setting up pqiv (2.2-1) ...

但由于我必须为我的应用程序安装 xdotool 和其他软件包,还有什么是另外一个呢?另外它会让码头工人高兴:)

好消息是它只有 136KB,并且会自动将启动画面放置在活动监视器的中心,而不是主监视器的中心或 X11 屏幕的中心(这在三个不同的监视器系统上看起来很有趣)决议)。


调用 pqiv

从命令行(您可以使用 os.popen()subprocess.Popen() 在 Python 中轻松复制),您只需键入:

pqiv -c -c -i m.png

这是我的 png 图像的样子:

mserve transparent splash screen.gif


结束 pqiv

在终端加载完成后,我必须发送 Control + C。在 Python 中,您必须 grep 来自 ps 的输出和 kill 作业。我想在 Linux 中相当直接,但对于我们的 Windows 朋友来说可能很陌生。

感谢超级用户 answer

答案 7 :(得分:0)

跨平台解决方案:

from sys import platform
import tkinter as tk
root = tk.Tk()

if platform == "linux" or platform == "linux2":
    root.overrideredirect(True)
    root.wait_visibility(root)
    root.wm_attributes("-alpha", 0.5)
elif platform == "darwin":
    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()
elif platform == "win32":
    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()

root.mainloop()
相关问题