配置 tkinter/ttk 小部件透明背景、ttk 框架背景颜色?
是否可以使用透明背景配置 tkinter 或 ttk 小部件(Label
、Entry
、Text
),以便将它们放置在容器中自定义背景颜色或画布图形或图像之上?
我也在寻找一种方法来更改 ttk Frame 小部件的背景颜色?
我是否需要使用新的 ttk Style 对象来完成上述任务? (我是 tkinter/ttk 的新手,仍在尝试以正确的方式做事)。
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?
I'm also looking for a way to change the background color of a ttk Frame widget?
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了在 OSX 上,你想要的都无法完成,因为所有这些小部件都是在矩形的真实窗口上绘制的(除非你使用一些低级技巧在相关窗口上挖洞;而不是这是我推荐的方法,尽管我已经有了 Tcl/Tk 的代码,诚然不是 Python/Tkinter 的代码,但它确实做到了)。 OSX 是个例外,因为窗口小部件(大部分)不是在真实窗口上绘制的,而是使用更轻量级的系统(因为这就是 OSX 的工作原理)。
您可以将小部件调整为比正常情况更少的边框,如果您使用的样式使它们基本上保持方形,那么这是可以的。或者,如果您使用画布作为“有趣”的表面,则可以在画布上构建类似按钮的东西,这可能更适合您实际正在做的事情。
要更改 Ttk 框架的背景颜色,您必须对其应用样式。到目前为止,我见过的有关样式创建的最佳说明位于 tkdocs 样式教程,当我想要查找如何做这些事情时,我会去那里。 (是的,Ttk 文档本身应该涵盖这一点,但目前还没有。)
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.
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.)