在 Ttk Frame 忽略的样式中指定的填充 [英] Padding specified in style ignored by Ttk Frame

查看:22
本文介绍了在 Ttk Frame 忽略的样式中指定的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码按预期工作,在绿色背景框架(也有很多填充)中显示一个红色背景按钮(有很多填充).请注意,在 Style 语句和 ttk.Frame 初始化中都指定了 Frame padding.

导入 ttk导入 Tkinter根 = Tkinter.Tk()ttk.Style().configure("TFrame", background="#0c0", padding=60)ttk.Style().configure("TButton", background="#c00", padding=20)frame = ttk.Frame(root, padding=60)框架.pack()btn = ttk.Button(frame, text="Button")btn.pack()root.mainloop()

然而,现在从 Frame 初始化中删除 'padding=60' 行(即 frame = ttk.Frame(root)),只将其保留在 Style() 中.configure(...) 语句,并且弹出的窗口在 Frame 中没有填充(因此您也无法分辨背景颜色是什么).相反,只有红色背景按钮占据了整个窗口.尽管有 Style 语句,但帧填充已恢复为默认值 0.

为什么为样式TFrame"指定的填充被忽略?另一个属性(背景")已正确应用于框架,并且使用 TButton 样式将填充和背景属性正确应用于按钮.

解决方案

TFrame 的布局不包含填充元素,因此没有样式元素可以使用样式中配置的填充值.您可以通过检查布局来看到这一点:

<预><代码>>>>从 tkinter 导入 *>>>从 tkinter.ttk 导入 *>>>风格 = 风格()>>>style.theme_use('default') # 选择Unix主题>>>style.layout("TFrame")[('Frame.border', {'sticky': 'nswe'})]

看看像 TButton 一样使用填充的东西:

<预><代码>>>>style.layout("TButton")[('Button.border', {'border': '1', 'children':[('Button.focus', {'children':[('Button.padding', {'children':[('Button.label', {'sticky': 'nswe'})],'粘性':'nswe'})],'粘性':'nswe'})],'粘性':'nswe'})]

在这个布局中,我们有一个border"元素,它将使用 borderwidthrelief 配置选项.一个 focus 元素来处理显示焦点环.还有一个 padding 元素以包含配置的填充量.

TFrame 样式不使用任何填充元素.但是,Frame 小部件确实使用小部件填充值.当放置包含的小部件时,小部件本身使用它来定义内部填充.这不在样式范围内,而是作为小部件特定选项.

The following code works as expected, presenting a red background button (with lots of padding) in a green background frame (also with lots of padding). Note that Frame padding is specified both in the Style statement and the ttk.Frame initialization.

import ttk
import Tkinter
root = Tkinter.Tk()
ttk.Style().configure("TFrame", background="#0c0", padding=60)
ttk.Style().configure("TButton", background="#c00", padding=20)
frame = ttk.Frame(root, padding=60)
frame.pack()
btn = ttk.Button(frame, text="Button")
btn.pack()
root.mainloop()

Now, however, remove the 'padding=60' line from the Frame initialization (i.e., frame = ttk.Frame(root)), leaving it only in the Style().configure(...) statement, and the window that pops up has no padding in the Frame (and therefore you can't tell what the background color is either). Instead there's just the red background button taking up the whole window. The frame padding has reverted to the default of 0 in spite of the Style statement.

Why is the padding specified for style "TFrame" ignored? The other attribute ("background") is correctly applied to the Frame, and both the padding and background attributes are correctly applied to the Button using the TButton style.

解决方案

The layout for the TFrame does not include a padding element so there is no style element to make use of the configured padding value in your style. You can see this by inspecting the layout:

>>> from tkinter import *
>>> from tkinter.ttk import *
>>> style = Style()
>>> style.theme_use('default') # select the Unix theme
>>> style.layout("TFrame")
[('Frame.border', {'sticky': 'nswe'})]

Looking at something that does use padding like a TButton:

>>> style.layout("TButton")
[('Button.border', {'border': '1', 'children':
    [('Button.focus', {'children': 
        [('Button.padding', {'children':
            [('Button.label', {'sticky': 'nswe'})],
        'sticky': 'nswe'})],
    'sticky': 'nswe'})],
'sticky': 'nswe'})]

In this layout we have a "border" element that will make use of borderwidth and relief configuration options. A focus element to deal with showing the focus ring. And also a padding element to include a configured amount of padding.

The TFrame style does not use any padding element. However, the Frame widget does use a widget padding value. This is used by the widget itself to define an internal padding when placing contained widgets. This is not covered by the style but as a widget specific option.

这篇关于在 Ttk Frame 忽略的样式中指定的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆