[英]How to make background Transparent in TKinter by converting Label widget into Canvas widget?
[英]How to make a tkinter Label background 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()
我认为它可以提供帮助,所有黑色都是透明的
root.wm_attributes('-transparentcolor','black')
Tk 中的透明背景不支持它。
如果您正在处理图像并将文本放入其中,最方便的方法是 - 我认为 - 使用Canvas
小部件。
.create_image(x, y, image=image, options)
tkinter Canvas
小部件的方法为.create_image(x, y, image=image, options)
和.create_text(x, y, text="Some text", options)
。
用这个 :
from tkinter import *
main=Tk()
photo=PhotoImage(file='test.png')
Label(main,image=photo,bg='grey').pack()
#your other label or button or ...
main.wm_attributes("-transparentcolor", 'grey')
main.mainloop()
这项工作如果使用bg='grey'
你可以在第 7 行更改它
祝你好运 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.