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",