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.
来源:https://stackoverflow.com/questions/28443818/padding-specified-in-style-ignored-by-ttk-frame