首页 > 解决方案 > 是否可以在 tkinter 中有一个清晰/透明的标签?

问题描述

我的 tkinter 窗口中有一个图像背景,事实证明,当您创建标签或框架时,默认不清晰,它是灰色的(它看起来很清晰,因为默认背景是相同的灰色)。

我最初对框架有同样的问题,但我将背景放在框架的前面,所以它们被隐藏了。但是,我不能对标签执行此操作,因为它们包含我想查看的文本。

那么,是否有可能使灰色不显示/将标签设置为“清除”(尝试 bg="clear" 和 bg="none")?

标签: pythontkinterlabel

解决方案


No, it is not possible to have a clear background in the tkinter label widget. However, there are ways to get the same output. For simple colour backgrounds, you can match the colour in the background of the label. For picture backgrounds like this example, you can use a canvas but the picture has to be within the same canvas.

Here is how you can use Canvas:

import tkinter as tk

win = tk.Tk()
win.config(bg="red")

canvas = tk.Canvas(win, width=200, height=20)
canvas.create_rectangle(0,0,30,30, fill="orange")
canvas.pack()

canvas.create_text(10,10, text="This isn't ideal but it works", anchor="nw")

win.mainloop()

Example Window

As you can see, the text doesn't have any background to it and any items beneath it are visible. However, this only applies to items in the canvas. As shown, the window background is red but the canvas background is the default grey - the canvas itself has a background colour but only to widgets outside of the canvas.


推荐阅读