python - 样式中指定的填充被 Ttk Frame 忽略

标签 python tkinter tk-toolkit ttk

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

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()

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

为什么忽略为样式“TFrame”指定的填充?另一个属性(“背景”)已正确应用于框架,填充和背景属性均已使用 TButton 样式正确应用于按钮。

最佳答案

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

>>> from tkinter import *
>>> from tkinter.ttk import *
>>> style = Style()
>>> style.theme_use('default') # select the Unix theme
>>> 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'})],
        'sticky': 'nswe'})],
    'sticky': 'nswe'})],
'sticky': 'nswe'})]

在此布局中,我们有一个“边框”元素,它将使用 borderwidthrelief 配置选项。用于处理聚焦环显示的 focus 元素。还有一个 padding 元素,用于包含配置的填充量。

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

关于python - 样式中指定的填充被 Ttk Frame 忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28443818/

相关文章:

python - 使用选择器的订阅不适用于 python - stomp.py

python - 想要程序循环的说明 (Python)

user-interface - 在 tcl tk gui 中隐藏和显示框架

python - Tkinter.PhotoImage 不支持 png 图片

python - PhotoImage 中的透明度错误

python - 使用 numpy 或 cython 进行高效的成对 DTW 计算

python - 如何更改Ubuntu 14.04中虚拟环境使用的python版本?

python - Tkinter 多个选项卡 : (Multithreading or Multiprocessing)

python - 使用 tkinter 输入一个变量,被调用

Python 3.3 tkinter.filedialog.askopenfile() 留下无响应的 Tk 窗口