配置具有透明背景、ttk 框架背景颜色的 tkinter/ttk 小部件? [英] Configure tkinter/ttk widgets with transparent backgrounds, ttk frame background colors?
问题描述
是否可以配置具有透明背景的 tkinter 或 ttk 小部件(Label
、Entry
、Text
)以便它们可以放置在具有自定义背景颜色的容器中还是在画布图形或图像之上?
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?
我也在寻找一种方法来更改 ttk Frame 小部件的背景颜色?
I'm also looking for a way to change the background color of a ttk Frame widget?
我是否需要使用新的 ttk 样式对象来完成上述操作?(我是 tkinter/ttk 的新手,仍然试图让我的头脑围绕正确的做事方式).
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).
推荐答案
Except 在 OSX 上无法实现,因为所有这些小部件都是在矩形的真实窗口上绘制的(除非您使用一些低级技巧来从相关窗口中剔除孔洞;即使我有代码,也不是我推荐的方法 – 用于 Tcl/Tk,而不是 Python/Tkinter – 诚然是这样做的).OSX 是个例外,因为小部件(大部分)不是在真实窗口上绘制的,而是使用更轻量级的系统(因为 OSX 就是这样工作的).
Except on 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).
您可以调整小部件的边框,使其比正常情况下的边框更少,如果您使用的样式使它们基本保持方形,则可以.或者,如果您将画布用于有趣"的表面,您可以在画布上构建类似按钮的东西,这可能更适合您的实际操作.
You can tune 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.
要更改 Ttk 框架的背景颜色,您必须对其应用样式.到目前为止,我所看到的关于样式创建的最佳说明是 tkdocs 样式教程,当我想查看如何做这些事情时,这就是 我 去的地方.(是的,Ttk 文档本身应该涵盖这一点,但目前还没有.)
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 I go 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 框架背景颜色的 tkinter/ttk 小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!