在Python中显示没有窗口的透明PNG图像 [英] Displaying a transparent PNG image without window in Python

查看:58
本文介绍了在Python中显示没有窗口的透明PNG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

操作系统:Windows 10

OS: Windows 10

Python 3.8

我正在尝试显示没有标题栏或任何其他窗口信息指示的透明 PNG 图像(想象一下在您的屏幕上粘贴贴纸).我已经用另一种语言完成了一次,将图像放入窗口中并删除了窗口的所有元素,包括标题栏和背景,但我无法在 python 中实现它.

I am trying to display PNG images with transparency with no titlebar, or any other indication of window info (imagine sticking a sticker on your screen). I've already done this once in another language by putting an image in a window and removing all the elements of the window, including titlebar and background, but I cannot achieve it in python.

我已经尝试过 Tkinter,但似乎它不是为使用 PNG 透明度而设计的,并且它不能为父窗口和子窗口提供单独的透明度.

I have already tried Tkinter, but seems like it's not designed to work with PNG transparency and it cannot have separate transparency for parent and child windows.

现在我正在尝试 wxPython.我的想法是,可以让父窗口透明,子窗口(保存图像)不透明,但无法弄清楚这是否可能.

Now I am trying wxPython. My idea is that it might be possible to make the parent window transparent and the child window (which holds the image) opaque, but cannot figure out if that's even possible.

这是我尝试的简化版本:

Here is the simplified version of my attempt:

app = wx.App()

path = 'Images/02.png'

bitmap = wx.Bitmap(path, wx.BITMAP_TYPE_PNG)  # Create bitmap

frame = wx.Frame(None,-1,'Transparent Window',size=(1000,1000)) # Create frame (top level window)
frame.SetTransparent(100)                 # Change transparency of the frame 
panel = wx.Panel(frame, -1)               # Create panel (to hold the image)
wx.StaticBitmap(panel, -1, bitmap)        # Put the image in the panel
panel.SetTransparent(255)                 # Change transparency of the panel (the image)

frame.Show()
app.MainLoop()

但不幸的是.SetTransparent()"仅适用于顶级窗口,因此我无法将面板的透明度设置为其他任何内容.

But unfortunately ".SetTransparent()" only works on top level window, so I can't set the transparency of the panel to anything else.

但基本上,问题是,是否可以使用 Python 单独显示具有透明度的图像?我不是在寻找实现这一目标的任何特定方法,我提供的方法只是我所知道的唯一方法.所以,如果可以,请帮助我:)

But basically, the question is, is it possible to display image with transparency on its own with Python? I am not looking for any specific method of achieving this, the one I provided was just the only one I know. So please help me if you can :)

尝试使用 wx.lib.agw.advancedsplash:

Edit 1: Tried to use wx.lib.agw.advancedsplash:

import wx
import wx.lib.agw.advancedsplash as AS

app = wx.App()

frame = wx.Frame(None, -1, "AdvancedSplash Test")

imagePath = "Images/05.png"
bitmap = wx.Bitmap(imagePath)

splash = AS.AdvancedSplash(frame, bitmap=bitmap)

app.MainLoop()

但它仍然填充透明区域

推荐答案

试试这个:

import tkinter as tk


root = tk.Tk()
root.overrideredirect(True)
root.config(bg="blue", bd=0, highlightthickness=0)
root.attributes("-transparentcolor", "blue")

canvas = tk.Canvas(root, bg="blue", bd=0, highlightthickness=0)
canvas.pack()

tk_img = tk.PhotoImage(file="cards.png")
canvas.create_image(0, 0, image=tk_img, anchor="nw")

root.mainloop()

它会在屏幕上创建一个像这样的图像:

It creates an image on the screen like this:

它将窗口的透明颜色更改为 "blue"(因为我使用的图片中没有任何蓝色的东西)并将背景颜色设置为 "蓝色.这使窗口透明.为了显示图像,我使用了 tk.Canvas 但它可能适用于 tk.Label(尚未测试).

It changes the transparent colour of the window to "blue" (because there isn't any thing blue in the picture that I am using) and sets the background colour to "blue". That makes the window transparent. To show the image I used a tk.Canvas but it might work with tk.Label (haven't tested it).

请注意,这很可能仅适用于 Windows.

Please note that this will most likely work only on Windows.

这篇关于在Python中显示没有窗口的透明PNG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆