tkinter.ttk --- Tk 主題化小工具¶
The tkinter.ttk module provides access to the Tk themed widget set,
introduced in Tk 8.5. It provides additional benefits including anti-aliased font
rendering under X11 and window transparency (requiring a composition
window manager on X11).
The basic idea for tkinter.ttk is to separate, to the extent possible,
the code implementing a widget's behavior from the code implementing its
appearance.
也參考
- Tk Widget Styling Support
A document introducing theming support for Tk
使用 Ttk¶
To start using Ttk, import its module:
from tkinter import ttk
To override the basic Tk widgets, the import should follow the Tk import:
from tkinter import *
from tkinter.ttk import *
That code causes several tkinter.ttk widgets (Button,
Checkbutton, Entry, Frame, Label,
LabelFrame, Menubutton, PanedWindow,
Radiobutton, Scale and Scrollbar) to
automatically replace the Tk widgets.
This has the direct benefit of using the new widgets which gives a better
look and feel across platforms; however, the replacement widgets are not
completely compatible. The main difference is that widget options such as
"fg", "bg" and others related to widget styling are no
longer present in Ttk widgets. Instead, use the ttk.Style class
for improved styling effects.
也參考
- Converting existing applications to use Tile widgets
A monograph (using Tcl terminology) about differences typically encountered when moving applications to use the new widgets.
Ttk Widgets¶
Ttk comes with 18 widgets, twelve of which already existed in tkinter:
Button, Checkbutton, Entry, Frame,
Label, LabelFrame, Menubutton, PanedWindow,
Radiobutton, Scale, Scrollbar, and Spinbox.
The other six are new: Combobox, Notebook,
Progressbar, Separator, Sizegrip and
Treeview. And all them are subclasses of Widget.
Using the Ttk widgets gives the application an improved look and feel. As discussed above, there are differences in how the styling is coded.
Tk code:
l1 = tkinter.Label(text="Test", fg="black", bg="white")
l2 = tkinter.Label(text="Test", fg="black", bg="white")
Ttk code:
style = ttk.Style()
style.configure("BW.TLabel", foreground="black", background="white")
l1 = ttk.Label(text="Test", style="BW.TLabel")
l2 = ttk.Label(text="Test", style="BW.TLabel")
For more information about TtkStyling, see the Style class
documentation.
Widget¶
ttk.Widget defines standard options and methods supported by Tk
themed widgets and is not supposed to be directly instantiated.
標準選項¶
All the ttk Widgets accept the following options:
選項 |
描述 |
|---|---|
class |
Specifies the window class. The class is used when querying the option database for the window's other options, to determine the default bindtags for the window, and to select the widget's default layout and style. This option is read-only, and may only be specified when the window is created. |
cursor |
Specifies the mouse cursor to be used for the widget. If set to the empty string (the default), the cursor is inherited for the parent widget. |
takefocus |
Determines whether the window accepts the focus during keyboard traversal. 0, 1 or an empty string is returned. If 0 is returned, it means that the window should be skipped entirely during keyboard traversal. If 1, it means that the window should receive the input focus as long as it is viewable. And an empty string means that the traversal scripts make the decision about whether or not to focus on the window. |
style |
May be used to specify a custom widget style. |