Tkinter Python透明背景与(不透明)可见形状?

Posted

技术标签:

【中文标题】Tkinter Python透明背景与(不透明)可见形状?【英文标题】:Tkinter Python transparent background with (not transparent ) visible shape? 【发布时间】:2021-03-11 02:00:57 【问题描述】:

我想在该窗口内创建一个不可见的透明背景,并带有可见的形状。我写了以下代码,但是你可以看到对象也是透明的,我该如何解决这个问题?

#!/usr/bin/env python3

from tkinter import *

window = Tk()
window.wait_visibility(window)
window.wm_attributes('-alpha',0.1)


def drag(event):
    event.widget.place(x=event.x_root, y=event.y_root,anchor=CENTER)

card = Canvas(window, width=74, height=97, bg='blue')
card.place(x=300, y=600,anchor=CENTER)
card.bind("<B1-Motion>", drag)

another_card = Canvas(window, width=74, height=97, bg='red')
another_card.place(x=600, y=600,anchor=CENTER)
another_card.bind("<B1-Motion>", drag)

window.mainloop()

换句话说,我不希望我的对象是透明的。我只希望屏幕是透明的。将来我可能会在窗口中添加图片矩形等。但我希望所有这些都以透明背景可见。有什么帮助吗?

我的操作系统:Ubuntu

注意:对于所有面临同样问题的人,我将我的库更改为 WXPython。它有透明背景、透明窗口、不同形状的窗口等。但如果你们能找到解决方案,我将感谢社区。 在 UBUNTU 中没有找到这个问题的答案。 Windows 和 Mac 都有,BR

【问题讨论】:

这些会有帮助吗? ***.com/questions/19080499/… 和 ***.com/questions/18394597/… 我正在开发 Ubuntu。这些问题没有显示 ubuntu 操作系统解决方案? 这对您的小工具有效吗? ***.com/a/18430628/14719340 我根据链接中的答案编辑了问题。我做错了什么吗?因为它不起作用对象仍然透明?可能缺少一些东西? @a121 抱歉,我在 Windows 上工作,因此无法验证上一个链接的合法性。我知道,我无法在 Windows 上执行此操作。 【参考方案1】:

我有一个解决方法,它可以在不确定 ubuntu 的 Windows 中使用透明画布

from tkinter import *

window = Tk()
window.lift()
window.wm_attributes("-topmost", True)
window.wm_attributes("-transparentcolor", 'gray')
window.wait_visibility()

canvas = Canvas(window, width=500, height=500)
canvas.pack()
canvas.config(cursor='tcross')
canvas.create_rectangle(0, 0, 500, 500, fill='gray', outline='gray')

def drag(event):
    event.widget.place(x=event.x_root, y=event.y_root,anchor=CENTER)

card = Canvas(window, width=74, height=97, bg='blue')
card.place(x=0, y=0,anchor=CENTER)
card.bind("<B1-Motion>", drag)

another_card = Canvas(window, width=74, height=97, bg='red')
another_card.place(x=600, y=600,anchor=CENTER)
another_card.bind("<B1-Motion>", drag)
window.mainloop()

参考资料: https://***.com/a/42880450/10849457

输出:

【讨论】:

TY,但为了清楚起见,我想说ubuntu没有“透明色”。我知道你的方法【参考方案2】:

你能改变 another_card.place(x=100, y=100,anchor=CENTER)

稍后调用这个 window.wm_attributes("-transparentcolor", 'gray')

from tkinter import *

window = Tk()
window.lift()
window.wm_attributes("-topmost", True)
window.wait_visibility()

canvas = Canvas(window, width=500, height=500)
canvas.pack()
canvas.config(cursor='tcross')
canvas.create_rectangle(0, 0, 500, 500, fill='gray', outline='gray')

def drag(event):
    event.widget.place(x=event.x_root, y=event.y_root,anchor=CENTER)

card = Canvas(window, width=74, height=97, bg='blue')
card.place(x=0, y=0,anchor=CENTER)
card.bind("<B1-Motion>", drag)

another_card = Canvas(window, width=74, height=97, bg='red')
another_card.place(x=100, y=100,anchor=CENTER)
another_card.bind("<B1-Motion>", drag)
window.wm_attributes("-transparentcolor", 'gray')
window.mainloop()

【讨论】:

你能测试我的代码吗(***.com/a/65145544/10849457)inubuntu 如果你有请 伙计们,但我甚至在你回答之前就在 ubuntu 中测试了 transparentcolor。这个问题是特定于操作系统的。 Mac 和 Windows 可能有它。但是在ubuntu中找不到。 tkinter.TclError: bad attribute "-transparentcolor": must be -alpha, -topmost, -zoomed, -fullscreen, or -type

以上是关于Tkinter Python透明背景与(不透明)可见形状?的主要内容,如果未能解决你的问题,请参考以下文章

python 的 tkinter库的使用中,如何使button的背景色为透明

Tkinter:模糊的透明背景帧

如何使 tkinter 标签背景透明?

使用透明背景、ttk 框架背景颜色配置 tkinter/ttk 小部件?

python使用tkinter实现透明窗体的开篇之作-基础框架介绍

Tkinter:将具有透明背景的图像添加到按钮