22

I'm trying, ultimately, to create "oddly-shaped windows" with Python using the Tkinter module. But for now I will settle for being able to make the background transparent while keeping child widgets fully-visible.

I'm aware this is done with wxPython and some other modules, but I'm inquiring as to the limits of Tkinter.

Can Tkinter create a clear Canvas or Frame? Can it pack UI elements without a canvas or frame? Can individual UI elements be transparent?

Can it pass mouse-click locations back to the system for processing any windows below it in the Z stack?

Paul Johnson
  • 321
  • 1
  • 2
  • 4

2 Answers2

33

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.x
root = Tk()
root.wait_visibility(root)
root.wm_attributes('-alpha',0.3)
root.mainloop()

Not sure if this works on Windows.

bhaskarc
  • 9,269
  • 10
  • 65
  • 86
  • Interesting. Though your example works, it seems to be incompatible with `root.overrideredirect(True)`; if I add that before the `wait_visibility` call, it causes the window blink (it appears fully opaque, and immediately gets transparent). – Clément Nov 06 '15 at 13:40
  • 3
    The 'window blink' phenomena happens for me irrespective of `overrideredirect` call. I guess it is the result of `wait_visibility` which waits till the window becomes fully visible to next allow for adding the `alpha` attribute thus causing the blink. – bhaskarc Nov 06 '15 at 18:05
  • 1
    @tao this is great. Is there any way to make the window frame opaque but the window contents transparent? – crypdick Jun 01 '17 at 17:40
  • 2
    @crypdick AFAIK, this is not possible in Tkinter. wm_attributes('-alpha',0.3) is only available on the top level window, so transparency can only be added on the top level. – bhaskarc Jun 02 '17 at 03:47
  • The proposed solution : root.wm_attributes('-alpha',0.3) raises error on my Linux distibution (WindRiver): root.wm_attributes("-alpha", 0.2) File "/opt/python27-x86_64/usr/lib/python2.7/lib-tk/Tkinter.py", line 1553, in wm_attributes return self.tk.call(args) _tkinter.TclError: wrong # args: should be "wm attributes window" Any suggestion, pls? – user929298 Feb 16 '18 at 09:26
  • @user929298 this seems unusual. Can you post a minimal but full working code of an example that would throw this error. Also it would be good to know the Tkinter version – bhaskarc Feb 16 '18 at 14:07
  • Thank you from coming back! I will create a new question is will post a link here -- https://stackoverflow.com/questions/48843580/python-2-7-tkinker-linux-windriver-how-to-make-trasparent-window – user929298 Feb 17 '18 at 16:41
  • First method works on Windows 7 with Python 3.7.2. Do you know if there some way to do this with an individual `Frame`? – martineau Jan 19 '19 at 00:21
  • 1
    @martineau: Tkinter does not allow transparency on individual widgets like the `Frame` – bhaskarc Jan 28 '19 at 18:55
  • 1
    @bhaskarc Is there a way for just the root to be transparent and not the items within it? – Delrius Euphoria Sep 02 '20 at 10:27
  • @CoolCloud Did you able to do that? transparent background? – Meric Ozcan Dec 02 '20 at 14:51
  • @MericOzcan Nope, there is no individual control over transparency. – Delrius Euphoria Dec 02 '20 at 19:17
  • @CoolCloud what you suggest than? buy a macbook? – Meric Ozcan Dec 02 '20 at 19:38
  • @MericOzcan I dont think it is OS problem, I think tkinter does not support it, though you have `root.attributes('-transparentcolor','#f0f0f0')`, just thought about this now, so not sure if its perfect, but yes. – Delrius Euphoria Dec 02 '20 at 20:53
  • Ubuntu does not have transparentcolor attribute. @CoolCloud – Meric Ozcan Dec 02 '20 at 21:06
  • @mericozcan Maybe. Not sure. – Delrius Euphoria Dec 03 '20 at 11:34
7

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!

Mitch McMabers
  • 3,634
  • 28
  • 27
  • 2
    I can confirm on Ubuntu 19:10 (default window mananger) you still need the `root.wait_visibility()` for it to work but as you say you don't need the root argument. – Daniel Worthington-Bodart May 04 '20 at 06:36
  • @DanielWorthington-Bodart Thanks for checking and letting everyone know! Answer updated. If you see this, it would be helpful to know your Python version, TCL/TK version, and TkInter (python module) version. – Mitch McMabers May 05 '20 at 23:19
  • Check these answers: https://stackoverflow.com/questions/12605003/non-standard-windows-with-tkinter – Izalion Dec 03 '20 at 09:36
  • @Izalion Hmm, that link has nothing at all to do with alpha transparency (which this question was about). But still, that's a nice link for those who want to make borderless windows. Although the people at your link say that borderless windows only work on Mac and Windows. Anyway, thanks for sharing some additional, useful knowledge :-) – Mitch McMabers Jan 21 '21 at 03:38