ttk,如何将标题定位在 Labelframe 内而不是边框上?
我正在尝试根据我公司的 CI 创建一个自己的 ttk 主题。我以 Sun Valley 主题 作为起点,并更换了图形、字体和颜色。
但是我被困在标签框架上。我试图将标签定位在框架内,有点像标题。即顶部边缘和标签之间应该有一些边距,并且内容(子窗口小部件)有适当的顶部填充。
现在:
+-- Label ------
| ...
所需:
+---------------
| Label
| ...
我尝试设置 padding
选项:
- 布局内
- TLabelframe 本身的
- 在 TLabelframe.Label 上的
,但标签没有移动像素。如何实现这一目标?
一般来说,我对 ttk:style layout
、ttk:style element
和 ttk:style configure
中哪些标识符和选项是合法的感到非常困惑,因为文档是模糊的并且分散在整个网络上,并且没有任何错误消息。有什么有用的提示吗?
编辑:自发布以来我发现的内容:
- Labelframe 标签完全是一个单独的小部件,具有类
TLabelframe.Label
。 - 可以覆盖其布局并在顶部添加间隔符,从而将文本向下移动。
- 但是,标签小部件在框架线上以 v 方向为中心。如果它的高度增加,它“向上”的推力和向下的推力一样多。我发现没有办法改变实际框架的对齐方式。
- 可以将
Labelframe
完全替换为具有所需布局的自定义Frame
子类。但这意味着在许多地方更改“客户端”代码。 :-/
I am trying to create an own ttk Theme based on my company's CI. I took the Sun Valley theme as starting point and swapped out graphics, fonts and colors.
However I am stuck on the Label frame. I am trying to position the Label within the frame, kind of like a heading. I.e. there should be some margin between top edge and label, and appropriate top-padding for the content (child widgets).
Now:
+-- Label ------
| ...
Desired:
+---------------
| Label
| ...
I tried to set the padding
option:
- within the Layout
- on TLabelframe itself
- on TLabelframe.Label
but the label did not move a pixel. How to achieve this?
Generally I am very confused about what identifiers and options are legal within ttk:style layout
, ttk:style element
and ttk:style configure
, because documentation is hazy and scattered all over the 'net, and there are no error messages whatsoever. Any helpful tips?
Edit: What I found out since posting:
- The Labelframe label is a separate widget altogether, with the class
TLabelframe.Label
. - It is possible to override its layout and add a spacer on top, shifting the text down.
- However, the label widget is v-centered on the frame line. If its height increases, it pushes "upward" as much as downward. I found no way to alter the alignment w.r.t. to the actual frame.
- It might be possible to replace
Labelframe
altogether with a customFrame
subclass with the desired layout. But that means changing the "client" code in many places. :-/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可以通过更改布局定义来完成,以便文本元素由 Labelframe 布局保存,并且 Layoutframe.Label 不再绘制文本元素。添加一点填充可确保所包含的小部件使标签保持清晰。
示例代码:
This can be done by changing the layout definitions so that the text element is held by the Labelframe layout and the Layoutframe.Label no longer draws the text element. Adding a bit of padding ensures the contained widgets leave the label clear.
Example code:
将 ttk.Labelframe 文本放置在浮雕图形的下方、上方或上方相对容易。此示例使用 text 属性,但也可以使用
labelwidget
。为了使浮雕可见,
Labelframe.Label
的背景颜色必须设置为“”。It is relatively easy to place
ttk.Labelframe
text below, on or above the relief graphic. This example uses the text attribute butlabelwidget
can also be used.In order for the relief to be visible the background color of
Labelframe.Label
must be set to "".查看Labelframe 小部件的源代码,我发现:
-labeloutside
配置选项。 (对于默认的NW
锚点)我现在使用 labeloutside 选项来制作“类似选项卡”的标题。
有了合适的图像,这几乎就是我的目标,只是标题不会延伸到整个框架宽度。 Tkinter 元素对图像使用类似“9-patch”的细分策略,因此您可以使用
element create
的-border
参数创建可拉伸框架。结果大约是这样的:
Looking through the source of the Labelframe widget, I found that:
-labeloutside
config option. (for defaultNW
anchor)I now used the labeloutside option to make a "tab-like" heading.
With suitable images, this is nearly what I was aiming for, only that the header does not stretch across the full frame width. Tkinter elements use a "9-patch"-like subdivision strategy for images, so you can make stretchable frames using the
-border
argument forelement create
.Result is approximately this: