Create a frame with transparent background with tkinter [Python]

Create a Frame with a transparent background with tkinter.

environment

Windows10 Python3.7

Sample code

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.

Execution result

コメント 2020-03-24 220613.png 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)

-alpha option

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, コメント 2020-03-24 220522.png Note that the transparency is applied to the entire object such as Label in root in this way.

reference

[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

Create a frame with transparent background with tkinter [Python]
Create a directory with python
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a python GUI using tkinter
Create a virtual environment with Python!
[Python] Create a file & folder path specification screen with tkinter
Create a Python function decorator with Class
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Create a GUI app with Python's Tkinter
Create a word frequency counter with Python 3.4
Difference in behavior of transparent Frame made with tkinter in pyinstaller [Python]
Create a Python module
Create a GUI executable file created with tkinter
[Python / Tkinter] A class that creates a scrollable Frame
Create a LINE BOT with Minette for Python
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a python3 build environment with Sublime Text3
[Python] I made a Youtube Downloader with Tkinter.
Create a matrix with PythonGUI (tkinter combo box)
Create a color bar with Python + Qt (PySide)
Steps to create a Twitter bot with python
Create a decision tree from 0 with Python (1. Overview)
Create a new page in confluence with Python
Create a color-specified widget with Python + Qt (PySide)
Create a Photoshop format file (.psd) with python
Create a Python environment
Create a Python console application easily with Click
Create a native GUI app with Py2app and Tkinter
Try to dynamically create a Checkbutton with Python's Tkinter
[Python] Create a ValueObject with a complete constructor using dataclasses
Why not create a stylish table easily with Python?
Create a python development environment with vagrant + ansible + fabric
Fill the background with a single color with OpenCV2 + Python
Create a Layer for AWS Lambda Python with Docker
[python] Create a date array with arbitrary increments with np.arange
[Python] How to create a 2D histogram with Matplotlib
Create a fake Minecraft server in Python with Quarry
Create a company name extractor with python using JCLdic
Create a 2d CAD file ".dxf" with python [ezdxf]
Programming with Python and Tkinter
Create a Wox plugin (Python)
Create a function in Python
Create a dictionary in Python
Create 3d gif with python3
Create a homepage with django
Create Image Viewer with Tkinter
Create a python numpy array
Make a fortune with Python
Run Label with tkinter [Python]
Create a heatmap with pyqtgraph
I tried scraping food recall information with Python to create a pandas data frame
I made a simple typing game with tkinter in Python
Create a list in Python with all followers on twitter
Create a Mastodon bot with a function to automatically reply with Python
Create a child account for connect with Stripe in Python