如何使 tkinter 标签背景透明?
- 2025-04-10 09:45:00
- admin 原创
- 15
问题描述:
我有一个窗口,以标签作为框架。我这样做是因为我想在背景中使用图像。但现在我遇到了我使用过的其他标签的问题。我用来实际标记东西的其他标签没有透明背景。有没有办法让这些标签的背景透明?
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()
解决方案 1:
我认为它可以有帮助,所有黑色都会变得透明
root.wm_attributes('-transparentcolor','black')
解决方案 2:
Tk 不支持透明背景。
解决方案 3:
如果您正在处理图像并在其上添加文本,我认为最方便的方法是利用Canvas
小部件。
tkinter Canvas
小部件具有和.create_image(x, y, image=image, options)
方法.create_text(x, y, text="Some text", options)
。
解决方案 4:
使用这个:
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 行更改它
祝你好运 :)
相关推荐
热门文章
项目管理软件有哪些?
热门标签
曾咪二维码
扫码咨询,免费领取项目管理大礼包!
云禅道AD