The ttk widget is based on a revised and enhanced version of TIP #48 (http://tip.tcl.tk/48) specified style engine.
FILE: path opython27\liblib-tk tk.py
The basic idea is to separate, to the extent possible, the code implementing a widget's behavior from the code implementing its appearance. Widget class bindings are primarily responsible for maintaining the widget state and invoking callbacks, and all aspects of the widgets appearance lies under themes.
You can substitute some Tkinter widgets with their corresponding ttk widgets (Button, Checkbutton, Entry, Frame, Label, LabelFrame, Menubutton, PanedWindow, Radiobutton, Scale, and Scrollbar).
However, Tkinter and ttk widgets are not completely compatible. The main difference is that Tkinter widget styling options like fg
, bg
, relief
, and others are not supported options for ttk widgets. These styling options are instead moved to ttk.Style()
.
Here's a small Tkinter code sample:
Label(text="Who", fg="white", bg="black")
Label(text="Are You ?", fg="white", bg="black")
And here's its equivalent code in ttk:
style = ttk.Style()
style.configure("BW.TLabel", foreground="white", background="black")
ttk.Label(text="Who", style="BW.TLabel")
ttk.Label(text="Are You ?", style="BW.TLabel")
ttk also provides six new widget classes which are not available in Tkinter. These are Combob
ox
, Notebo
ok
, Progressb
ar
, Separator
, Sizegrip
, and Treevi
ew
.
ttk style names are as follows:
Options available to all ttk widgets are as follows:
Options accepted by all scrollable ttk widgets are as follows:
Methods from ttk.Widget class with their description are as follows:
We will not show all ttk widget specific options here. To obtain a list of available options for a ttk widget, use the help command.
To obtain help on any ttk widget/class, import ttk into the namespace using following command:
>>>import ttk
The following commands can then be used to get information on a particular widget:
Widget Name |
Getting Help |
---|---|
| |
The following given are some ttkVirtual events and situation when they are triggered:
Virtual Event |
Triggered when |
---|---|
The user selects an element from the list of values in the Combobox widget | |
Each widget in ttk is assigned a style, which specifies the set of elements making up the widget and how they are arranged, along with dynamic and default settings for element options.
By default, the style name is the same as the widget's class name, but it may be overridden by the widget's style option. If the class name of a widget is unknown, use the method Misc.winfo_class()
(somewidget.winfo_class()
). Following given are few methods with their description of ttk styling:
Method |
Description |
---|---|
Query or sets the default value of the specified option(s) in style. Each key in | |
Create a new element in the current theme of given | |
Returns the list of elements defined in the current theme. | |
Return the list of | |
Define the widget layout for given style. If
|
A layout can be just None
, if it takes no options, or a dictionary of options specifying how to arrange the element. The layout mechanism uses a simplified version of the pack geometry manager: given an initial cavity, each element is allocated a parcel.
Valid options: Values |
Description |
---|---|
Specifies which side of the cavity to place the element; one of top, right, bottom or left. If omitted, the element occupies the entire cavity. | |
Specifies where the element is placed inside its allocated parcel. | |
Specifies a list of elements to place inside the element. Each element is a tuple (or other sequence) where the first item is the layout name, and the other is a layout. | |
Returns the value specified for option in style. If state is specified, it is expected to be a sequence of one or more states. If the default argument is set, it is used as a fallback value in case no specification for option is found. | |
Query or sets dynamic values of the specified option(s) in style. Each key in kw is an option, and each value should be a list or a tuple (usually) containing | |
Creates a new theme. It is an error if themename already exists. If parent is specified, the new theme will inherit styles, elements and layouts from the specified parent theme. If settings are present, they are expected to have the same syntax used for | |
Returns a list of all known themes. | |
Temporarily sets the current theme to | |
If |
3.15.12.34