How to set the transparency of the tkinter frame?
Asked Answered
U

2

6

Python version 2.7

Code:

from Tkinter import *

root = Tk();
root.geometry ('{}x{}'.format(w,h));
left_frame = Frame(root, width = w*0.8, height=400, bg='#988C89');
right_frame = Frame(root, bg='#988C89',  width = w*0.8, height=400 );


left_frame.grid_propagate(0);
right_frame.grid_propagate(0);

root.grid_rowconfigure(1, weight=1);
root.grid_columnconfigure(0, weight=1); 

visible = Frame (root,  width = w*0.8, height=400);
visible.grid(row=0, column=0, sticky="new");

How do I adjust the transparency of a visible frame?

If I add the code

 visible.attributes("transparentcolor","red")

I get error : AttributeError: Frame instance has no attribute 'attributes'

and with code

visible.configure(bg='#988C8900');

I get error : tkinter.TclError: invalid color name "#988C8900"

What should I do?

Undercast answered 14/3, 2018 at 6:54 Comment(0)
S
8

You just cannot set the transparency of the frame in tkinter. But you can set the transparency of the whole window with root.attributes('-alpha', 0.5)

You can also do root.attributes("-transparentcolor", "red") if you are using Windows, but again it will be applied to the whole window.

Otherwise, you can overlay PNG images with transparent parts using a Canvas.

Slipstream answered 14/3, 2018 at 8:41 Comment(0)
S
1

Not sure when it was introduced, but at least since tk 8.5 (which was the tk version with python 2.7), you can set the background of a Frame or LabelFrame to an empty string,

Frame(root, background='')

and it will be transparent, as in it will show the bottom widgets, including a background image if there is one, unlike root.attributes making the whole window actually transparent.

Here's an example showing that the frame actually shows the background image (created using a Label) when the background is set to an empty string. The frame is just the same height as the labels.

sample image showing that the frame is actually transparent

Unfortunately, this empty string background is supported only by Frame and LabelFrame.

Strauss answered 31/5, 2023 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.