如何设置tkinter框架的透明度[英] how to set the transparency of the tkinter frame

本文是小编为大家收集整理的关于如何设置tkinter框架的透明度的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

python 2.7 版

代码;

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");

如何调整可见框架的透明度?

如果我添加代码

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

我得到 error : AttributeError: Frame instance has no attribute 'attributes'

还有代码

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

我得到 error : tkinter.TclError: invalid color name "#988C8900"

我该怎么办?

推荐答案

你只是不能在 tkinter 中设置框架的透明度.但是你可以设置整个窗口的透明度root.attributes('-alpha', 0.5)

如果您使用的是 Windows,您也可以执行 root.attributes("-transparentcolor", "red"),但同样会应用于整个窗口.

否则,您可以使用 Canvas 覆盖带有透明部分的 PNG 图像.

本文地址:https://www.itbaoku.cn/post/2091136.html

问题描述

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?

推荐答案

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.