Python Tkinter标签背景透明

Cri*_*spy 1 python label tkinter colors transparent

我有一个带有标签的窗口作为我的框架.我这样做是因为我想在后台拍摄一张照片.但是现在我遇到了我用过的其他标签的问题.我用来实际标记的其他标签没有透明的背景.有没有办法让这些标签的背景透明?

import Tkinter as tk

root = tk.Tk()
root.title('background image')

image1 = Tk.PhotoImage(file='image_name.gif')

# get the image size
w = image1.width()
h = image1.height()

# make the root window the size of the image
root.geometry("%dx%d" % (w, h))

# root has no image argument, so use a label as a panel
panel1 = tk.Label(root, image=image1)
panel1.pack(side='top', fill='both', expand='yes')

# put a button/label on the image panel to test it
label1 = tk.Label(panel1, text='here i am')
label1.pack(side=Top)

button2 = tk.Button(panel1, text='button2')
button2.pack(side='top')

# start the event loop
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

Mar*_*1ni 6

Tk 中的透明背景不支持它。


小智 6

我认为它可以提供帮助,所有黑色都是透明的

root.wm_attributes('-transparentcolor','black')
Run Code Online (Sandbox Code Playgroud)

  • 如果它只使彩色小部件透明,这将是一个很好的解决方案,但它似乎使彩色小部件和所有较低的小部件透明。如果您可以使抬起的小部件透明并看到低于它的小部件,那就太好了。知道如何实现这一点吗? (4认同)
  • 使用`root.wm_attributes('-transparentcolor',root ['bg'])`使默认颜色透明。 (3认同)
  • 这是行不通的。我已经尝试过了,它不接受 `-transparentcolor` 作为第一个参数。 (2认同)