[Tkinter Series 14/45] ttk component library and principles

28. ttk: theme widget

Starting with Tk 8.5, the ttk module becomes available. This module replaces most (but not all) of the original Tkinter machinery. Using this module you get the following advantages:

  • Platform-specific appearance. In versions prior to Tk 8.5, one of the most common complaints about Tk applications was that they did not conform to the style of each platform.

    The ttk module allows you to write your application in a generic way, but your application can look like a Windows application under Windows, a MacOS application under MacOS, etc., without having to worry about Make any changes to the program.

    Each possible different appearance is represented by a named ttk Theme. For example, themes give you the look and feel of the original Tkinter widgets described in the previous section.

  • Simplification and generalization of state-specific widget behavior. In the basic Tkinter world, there are many widget options for specifying how a widget should look or behave based on various conditions.

    For example, the tk.Button widget has several different options for controlling the foreground (text) color.

    • activeforegroundApply color options when the cursor is over the button.

    • disabledforegroundThis color is used when the widget is disabled.

    • foreground The widget will have a color when other conditions do not apply.

    The ttk module collapses many of these special cases into a simple two-part system:

    • Each widget has a number of different states, each of which can be turned on or off independently of the others. Examples of states are: disabled, active, and focus.

    • You can set up a style map that specifies that certain options will be set to certain values based on certain states or certain combinations of widget states.

To use ttk, you need to know these things.

28.1 Importttk

There are several ways to import the ttk module.

  • If you want all widgets and other functionality of Tkinter and ttk to be in the global namespace, use an import of the following form:

    from Tkinter import *
    from ttk import *

    It is important to perform these two imports in this order so that all widget types in ttk replace equivalent widgets in Tkinter. For example, all your Button widgets will come from ttk rather than Tkinter.

  • In more complex applications where you use multiple imported modules, you can greatly improve the readability of your code if you practice safe namespace hygiene: import all modules using the ” ” syntax. This just requires a little more typing, but it has the great advantage that you can look at references to something and know where it comes from. import modulename

    We recommend this import method:

    import ttk

    So, after this import, ttk.Label is a Label widget constructor and ttk.Button is a Button >, etc.

    If you need to reference an item in the Tkinter module, it can be used as ttk.Tkinter. For example, the anchor code for “Northeast” is ttk.Tkinter.NE.

    You can import Tkinter individually via:

    import Tkinter as tk

    After importing in this way, the code of “Northeast” is tk.NE.

28.2. ttkWidgetset

The ttk module contains different versions of most of the standard Tkinter widgets and some new widgets. These widgets replace the widgets of the same name in Tkinter:

  • Item 29, ” ttk.Button “.

  • Item 30, ” ttk.Checkbutton “.

  • Clause 32, ” ttk.Entry “.

  • Item 33, ” ttk.Frame “.

  • Item 34, “ttk.Label”.

  • Item 35, “ttk.LabelFrame”.

  • Item 36, ” ttk.Menubutton “.

  • Item 38, ” ttk.PanedWindow “.

  • Item 40, ” ttk.Radiobutton “.

  • Item 41, ” ttk.Scale “.

  • Item 42, ” ttk.Scrollbar “.

These widgets are new and specific to ttk:

  • Item 31, ” ttk.Combobox “.

  • Item 37, ” ttk.Notebook “.

  • Item 39, ” ttk.Progressbar “.

  • Item 43, ” ttk.Separator “.

29. ttk.Button

This widget is the ttk version of Section 7 “Widgets”. Create a ttk widget: Button.Button

 w = ttk.Button(parent, option=value, ...)

The following are the options for the ttk widget .Button. Compare them to the Tkinter version discussed in Section 7, “Widgets”. Button

Table 35. ttk.ButtonOptions

class_ Small Component class name. This can be specified when the widget is created, but cannot be changed later. See Section 27, “Standardized Appearance” for a description of widget classes.
command Function called when the button is pressed.
compound

If both the image and text options are provided, the compound option specifies the position of the image relative to the text. The value can be tk.TOP (image above text), tk.BOTTOM (image below text), tk.LEFT (image below text) image to the left) or tk.RIGHT (image to the right of the text).

When you provide the image and text options without specifying the compound option, the image will be displayed but the text will not be displayed.

cursor The cursor that appears when the mouse is over a button; see Section 5.8, “Cursors”.
image The image that appears on the button; see Section 5.9 “Images”.
style The style to use when rendering this button; see Section 49, “Using and Customizing ttk Styles”.
takefocus By default, a ttk is included in the focus traversal;.ButtonSee Section 53, “Focus: Routing Keyboard Input”. To remove a widget from focus traversal, use takefocus=False.
text The text to display on the button, as a string.
textvariable Variable that controls the text displayed on the button; see Section 52, “Controlling Variables: Values Behind Widgets”.
underline If this option has a non-negative valuen , an underline will appear below the character at positionn.
width If the label is text, this option specifies the absolute width of the text area on the button in number of characters; the actual width is this number multiplied by the average width of characters in the current font. For image tags, this option is ignored. This option can also be configured per style.

The ttk constructor does not support these options for the Tkinter Button widget: .Button

Table 36. Tkinter Button options not found in ttkTkinter .Button

activebackground Use Stylemaps to control the background option; see Section 50.2, “TTK Stylemaps: Dynamic Appearance Changes”.
activeforeground Use style maps to control foreground options.
anchor

Configure this option using styles; see Section 49, “Using and Customizing ttk Styles”. Use this option to specify the position of text width when this option allocates additional horizontal space.

For example, if you specify the options width=20 and a style (east) on a button that displays text and an image, the image will be at the right end of the twenty character space and the text will be exactly at its left. compound=tk.RIGHTanchor=tk.E

This option is ignored when the button displays an image but no text.

backgroundor bg backgroundUse style configuration options. bg does not support abbreviations.
bitmap Not supported.
borderwidthor bd borderwidth uses style configuration options. bd does not support abbreviations.
cursor The cursor that appears when the mouse is over a check button; see Section 5.8, “Cursors”.
default Not supported; see Section 50.2, “TTK Style Maps: Dynamic Appearance Changes”.
disabledforeground Use stylemaps as the foreground option; see Section 50.2, “ttk Stylemaps: Dynamic Appearance Changes”.
font Configure this option using styles.
foreground or fg Configure this option using styles.
height Not supported.
highlightbackground To control the color of the focus highlight when the button has no focus, use a style map to control the option highlightcolor; see Section 50.2, “TTK Style Maps: Dynamic Appearance Changes”.
highlightcolor You can specify the default focus highlight color by setting this option in the style. You can also control focus highlight color using style maps.
highlightthickness Configure this option using styles. This option may not be available for all themes.
justify If text contains newline ( '\
'
) characters, the text will occupy multiple lines on the button. The justify option controls how each line is positioned horizontally. Configure this option using a style; the value can be tk.LEFT, tk.CENTER, or for left-aligned, centered, or right-aligned lines. tk.RIGHT
overrelief Use style graphs to control relief options; see Section 50.2, “ttk style graphs: Dynamic appearance changes”.
padx Not supported.
pady Not supported.
relief Configure this option using a style; see Section 49, “Using and Customizing ttk Styles”.
repeatdelay Not supported.
repeatinterval Not supported.
state There is no option with this name in ttk. The state mechanism has been generalized; see Section 50.2, “TTK Style Maps: Dynamic Appearance Changes”.
wraplength If you use a style that sets this option to a certain size, the text will be cut into pieces that do not exceed that size.

Methods on ttk include all methods described in Section 46, “Methods common to all ttk widgets” .Button, plus:

.invoke()

Call the button’s command callback and return the content returned by the function. Has no effect if the button is disabled or has no callback.

The ttk widget does not support the .flash() method. Tkinter.Button.Button

29. ttk.Button (anzeljg.github.io)