windows 配置具有透明背景、ttk 框架背景颜色的 tkinter/ttk 小部件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4011406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configure tkinter/ttk widgets with transparent backgrounds, ttk frame background colors?
提问by Malcolm
Is it possible to configure tkinter or ttk widgets (Label
, Entry
, Text
) with a transparent background so that they can be placed in containers with custom background colors or on top of canvas graphics or images?
是否有可能配置TKinter或者TTK窗口小部件(Label
,Entry
,Text
)具有透明背景,以便它们可以被放置在与自定义的背景颜色或在画布上图形或图像的顶部容器?
I'm also looking for a way to change the background color of a ttk Frame widget?
我也在寻找一种方法来更改 ttk Frame 小部件的背景颜色?
Do I need to use the new ttk Style objects to accomplish the above? (I'm new to tkinter/ttk and still trying to get my head around the proper way to do things).
我是否需要使用新的 ttk 样式对象来完成上述操作?(我是 tkinter/ttk 的新手,仍然试图让我的头脑围绕正确的做事方式)。
回答by Donal Fellows
Excepton OSX, what you want can't be done because all those widgets are drawn on real windows which are rectangular (unless you use some low-level tricks to chop holes out of the window concerned; not an approach I recommend even though I've got code – for Tcl/Tk, not Python/Tkinter admittedly – which does it). OSX is the exception because there widgets (mostly) aren't drawn on real windows, but rather use a lighter-weight system (because that's how OSX works).
除了在 OSX 上,你想要的东西无法完成,因为所有这些小部件都是在矩形的真实窗口上绘制的(除非你使用一些低级技巧从相关窗口中切出洞;这不是我推荐的方法,即使我有代码 - 为 Tcl/Tk,而不是 Python/Tkinter 诚然 - 这样做)。OSX 是个例外,因为小部件(大部分)不是在真实窗口上绘制的,而是使用更轻量级的系统(因为这就是 OSX 的工作方式)。
You cantune the widgets to have less of a border than normal, which is OK if you're using a style that keeps them fundamentally square. Or if you're using a canvas for your “interesting” surface, you can build button-like things on the canvas which may well be better for what you're actually doing.
您可以调整小部件以使其边框比正常情况少,如果您使用的样式使它们基本保持方形,则可以。或者,如果您在“有趣”的表面上使用画布,您可以在画布上构建类似按钮的东西,这可能更适合您的实际操作。
To change the background color of a Ttk frame, you'll have to apply a style to it. The best instructions on style creation I've seen so far are in the tkdocs style tutorial, which is where Igo when I want to look up how to do these things. (Yes, the Ttk documentation ought to cover this itself, but it doesn't yet.)
要更改 Ttk 框架的背景颜色,您必须对其应用样式。到目前为止,我看到的关于样式创建的最佳说明是tkdocs 样式教程,当我想查找如何做这些事情时,我会去那里。(是的,Ttk 文档本身应该涵盖这一点,但目前还没有。)