5

Is there a way to get a transparent background in a Tkinter window on Linux?

The only way I see currently is:

import tkinter as tk

root = tk.Tk()

root.overrideredirect(True)
root.wait_visibility(True)
root.wm_attributes("-alpha", 0.0)

canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()
canvas.create_rectangle(50, 25, 150, 75, fill="red")

root.mainloop()

However, this is not what I want. I want to get the background to be transparent so my overlay can be something else.

I found a couple posts here, but they only cover bg transparency on Windows and Mac OS. The Linux related one is only alpha?

linux mint tkinter transparent window

transparent background in a tkinter window

Drew
  • 1,171
  • 4
  • 21
  • 36

4 Answers4

4

I'm pretty sure this is going to work.

root.wm_attributes("-alpha", 0.5)
root.wait_visibility(root)
  • Hi @SuspectAverage5. Thanks for your answer. Usually answers with an explanation are more welcomed here. Would you like to update your answer to provide short explanation? – MaxV Jan 30 '21 at 21:06
4

It worked for me on Ubuntu 20.04, Linux Mint 20.1, Cinnamon
Python 3.6

import tkinter as tk
root = tk.Tk()
#root.overrideredirect(True)
root.wait_visibility(root)
root.wm_attributes("-alpha", 0.5)
root.mainloop()

Use `root.overrideredirect(True)´ if you want that the window manager does not handle the window at all, except when you explicitly tell it to map or unmap it.

Volker Siegel
  • 3,277
  • 2
  • 24
  • 35
danilo
  • 7,680
  • 7
  • 43
  • 46
  • `root.overrideredirect(True)´ is not essential and has the confusing and unrelated effect that the window can not be closed by normal means. It is somewhat related to having a transparent window, so I leave it in, but comment it out. – Volker Siegel May 25 '23 at 18:04
1

Add this line:

root.wait_visibility(root)

before the "root.wm_attributes.." line as mentioned in this answer. It works for me on Ubuntu 16.04

hikerjobs
  • 326
  • 3
  • 12
0

I am afraid "Tkinter window on Linux" does not make sense. Linux is a "command line" OS, so no windows with linux !

To have windows on Linux, you must use a "desktop environment" (KDE, Gnome, Xfce, LXDE...)

So the question should be "Is there a way to get a transparent background in a Tkinter window on <XXX>?" XXX = the desktop environment you use

For Gnome, the best answer I found is at How to make Gtk+ window background transparent? (see "code in python + GTK3").

Iznogood1
  • 152
  • 2
  • 11
  • The desktop environment is not relevant here. All use an X11 window system. Not even the window manager is relevant, as one of the answers demonstrates, currently. – Volker Siegel May 25 '23 at 18:01