Create a Frame with a transparent background with tkinter.
Windows10 Python3.7
from tkinter import ttk
import tkinter
root=tkinter.Tk()
root.wm_attributes("-transparentcolor", "snow")
#root.attributes("-alpha",0.5)
ttk.Style().configure("TP.TFrame", background="snow")
f=ttk.Frame(master=root,style="TP.TFrame",width="400",height="300")
f.pack()
label=ttk.Label(master=root,text="Don't get thinner ...",foreground="red",background="snow")
label.place(x=150,y=150)
root.mainloop()
Specify the color you want to be transparent to transparent color
in root.wm_attributes
. This time set to snow
.
From this, the background of f
and label
whose background
is snow
becomes transparent.
The background of Frame and Label is transparent and the wallpaper behind is visible. I can see some snow on the Label, but this doesn't help (it feels bad)
The root.wm_attributes ("-transparentcolor "," snow ")
in the above code
You can certainly change the transparency with [0,1] with the value of -alpha by doing something like root.attributes ("-alpha ", 0.5)
.
But,
Note that the transparency is applied to the entire object such as Label in root in this way.
[Python] [TkInter] Generate transparent Frame ↑ It seems that it can be reproduced with the contents of this article under Mac environment (unconfirmed because there is no environment at hand).
I checked if it could be reproduced on Linux, but I couldn't solve it after all ... If you execute the above sample code as it is under the ubuntu 18.04.4 environment at hand,
Traceback (most recent call last):
File "hoge.py", line 5, in <module>
root.wm_attributes("-transparentcolor", "snow")
File "/usr/lib/python3.6/tkinter/__init__.py", line 1788, in wm_attributes
return self.tk.call(args)
_tkinter.TclError: bad attribute "-transparentcolor": must be -alpha, -topmost, -zoomed, -fullscreen, or -type
It became.
Recommended Posts