2

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,031
  • 4
  • 20
  • 34
  • Have you tried `root.attributes('-transparentcolor', root['bg'])`? – Nae Feb 17 '18 at 08:48
  • 2
    No that just causes the error: Traceback (most recent call last): File "./bg.py", line 24, in root.wm_attributes('-transparentcolor', root['bg']) File "/usr/lib/python3.6/tkinter/__init__.py", line 1782, in wm_attributes return self.tk.call(args) _tkinter.TclError: bad attribute "-transparentcolor": must be -alpha, -topmost, -zoomed, -fullscreen, or -type – Drew Feb 17 '18 at 18:09
  • Did able to solve this problem @Drew ? – Meric Ozcan Dec 02 '20 at 14:55

3 Answers3

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
  • 291
  • 3
  • 11
  • 1
    I am aware of that for alpha on the entire window, I however, just want to have the bg transparent – Drew Feb 17 '18 at 06:42
  • Not sure what you mean. By "overlay" do you mean text? Using the above code, if I add a Text widget, I can see the text in it while the background remains transparent. – hikerjobs Feb 17 '18 at 07:01
  • I improved the question. I do not know about text, but I am trying to draw other objects, with a 100% transparent background. – Drew Feb 17 '18 at 07:17
1

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
0

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()
danilo
  • 5,162
  • 6
  • 31
  • 40