Create a new element in the current theme, of the given
etype
which is expected to be either “image”, “from” or “vsapi”. The latter is only available in Tk 8.6 on Windows.
If “image” is used,
args
should contain the default image name followed by statespec/value pairs (this is the imagespec), and
kw
may have the following options:
-
border=padding
-
padding is a list of up to four integers, specifying the left, top, right, and bottom borders, respectively.
-
height=height
-
Specifies a minimum height for the element. If less than zero, the base image’s height is used as a default.
-
padding=padding
-
Specifies the element’s interior padding. Defaults to border’s value if not specified.
-
sticky=spec
-
Specifies how the image is placed within the final parcel. spec contains zero or more characters “n”, “s”, “w”, or “e”.
-
width=width
-
Specifies a minimum width for the element. If less than zero, the base image’s width is used as a default.
范例:
img1 = tkinter.PhotoImage(master=root, file='button.png')
img1 = tkinter.PhotoImage(master=root, file='button-pressed.png')
img1 = tkinter.PhotoImage(master=root, file='button-active.png')
style = ttk.Style(root)
style.element_create('Button.button', 'image',
img1, ('pressed', img2), ('active', img3),
border=(2, 4), sticky='we')
If “from” is used as the value of
etype
,
element_create()
will clone an existing element.
args
is expected to contain a themename, from which the element will be cloned, and optionally an element to clone from. If this element to clone from is not specified, an empty element will be used.
kw
is discarded.
范例:
style = ttk.Style(root)
style.element_create('plain.background', 'from', 'default')
If “vsapi” is used as the value of
etype
,
element_create()
will create a new element in the current theme whose visual appearance is drawn using the Microsoft Visual Styles API which is responsible for the themed styles on Windows XP and Vista.
args
is expected to contain the Visual Styles class and part as given in the Microsoft documentation followed by an optional sequence of tuples of ttk states and the corresponding Visual Styles API state value.
kw
may have the following options:
-
padding=padding
-
Specify the element’s interior padding.
padding
is a list of up to four integers specifying the left, top, right and bottom padding quantities respectively. If fewer than four elements are specified, bottom defaults to top, right defaults to left, and top defaults to left. In other words, a list of three numbers specify the left, vertical, and right padding; a list of two numbers specify the horizontal and the vertical padding; a single number specifies the same padding all the way around the widget. This option may not be mixed with any other options.
-
margins=padding
-
Specifies the elements exterior padding.
padding
is a list of up to four integers specifying the left, top, right and bottom padding quantities respectively. This option may not be mixed with any other options.
-
width=width
-
Specifies the width for the element. If this option is set then the Visual Styles API will not be queried for the recommended size or the part. If this option is set then
height
should also be set. The
width
and
height
options cannot be mixed with the
padding
or
margins
选项。
-
height=height
-
Specifies the height of the element. See the comments for
width
.
范例:
style = ttk.Style(root)
style.element_create('pin', 'vsapi', 'EXPLORERBAR', 3, [
('pressed', '!selected', 3),
('active', '!selected', 2),
('pressed', 'selected', 6),
('active', 'selected', 5),
('selected', 4),
('', 1)])
style.layout('Explorer.Pin',
[('Explorer.Pin.pin', {'sticky': 'news'})])
pin = ttk.Checkbutton(style='Explorer.Pin')
pin.pack(expand=True, fill='both')
Changed in version 3.13:
Added support of the “vsapi” element factory.