Is there a way to create transparent windows with Tkinter? Is there a way to create transparent windows with Tkinter? tkinter tkinter

Is there a way to create transparent windows with Tkinter?


The option root.attributes('-alpha', 0.1) can be used to make a transparent window

from Tkinter import *root = Tk()root.attributes('-alpha', 0.3)root.mainloop()

However in this case even the widgets on the root will inherit the transparency.

Update for Linux (Tested on Ubuntu)

The above code does not work on Linux machines. Here's an update that works on Linux.

from tkinter import Tk # or(from Tkinter import Tk) on Python 2.xroot = Tk()root.wait_visibility(root)root.wm_attributes('-alpha',0.3)root.mainloop()

Not sure if this works on Windows.


Summary as of late 2019:

As of TCL/TK version 8.6, the alpha, fullscreen and topmost window attributes work on ALL platforms (Windows, Mac and Linux):

https://www.tcl.tk/man/tcl8.6/TkCmd/wm.htm#M9

Previous versions of the manual noted that there PREVIOUSLY WERE platform differences (only some platforms supported those 3 attributes). But as long as you use the latest TCL/TK, you're guaranteed that all of those attributes will work on ALL platforms!

There are still platform quirks on LINUX, since each window attribute feature relies on the operating system's underlying window manager (on Mac and Windows they're always capable, but on Linux there are tons of different window managers/compositors, and NOT all support transparent windows, etc). It says that in case transparency is not supported, the alpha property will stay at 1.0 if you try to read it again later. However the page also notes that on Linux (X11), the attributes are updated asynchronously which means that you can't trust the value you read (if you change alpha and then immediately try to read it, you'll still read the old value, so you can't use that method to check if alpha was successfully changed on Linux).

As for the other answers saying that you first need to use root.wait_visibility(root) on Linux to make sure the window is visible on screen before setting the alpha attribute... I don't know, since I don't have a Linux machine to check. But I heavily doubt that it's needed anymore, since the official manual says that alpha is supported and says nothing about that command being necessary. Either way, it doesn't hurt to add the wait_visibility trick too... It is simply a command that runs a very brief event loop that waits until the actual window has appeared on the user's screen. So it's probably still a good idea to add that command before all of your attribute-setting. Especially since it's proven to help the alpha work on Linux on old TCL/TK versions! PS: You don't need the (root) argument, and should type root.wait_visibility() instead, which means "wait for self (root in this case since we called the command on the root object)".

Update:

Daniel in the comments below has let me know that root.wait_visibility() is still necessary on Ubuntu 19.10. Although he didn't specify his Python, TCL/TK or TkInter versions so maybe they're outdated on his system. Either way, sounds like it's a safer bet to always include that command for backwards compatibility!