Issue #5262: Improved fix.
[python.git] / Doc / library / ttk.rst
blob1795771caa704b84bc01f7f9c52c929ea6b8b73e
1 :mod:`ttk` --- Tk themed widgets
2 ================================
4 .. module:: ttk
5    :synopsis: Tk themed widget set
6 .. sectionauthor:: Guilherme Polo <ggpolo@gmail.com>
9 .. index:: single: ttk
11 The :mod:`ttk` module provides access to the Tk themed widget set, which has
12 been introduced in Tk 8.5. If Python is not compiled against Tk 8.5 code may
13 still use this module as long as Tile is installed. However, some features
14 provided by the new Tk, like anti-aliased font rendering under X11, window
15 transparency (on X11 you will need a composition window manager) will be
16 missing.
18 The basic idea of :mod:`ttk` is to separate, to the extent possible, the code
19 implementing a widget's behavior from the code implementing its appearance.
22 .. seealso::
24    `Tk Widget Styling Support <http://www.tcl.tk/cgi-bin/tct/tip/48>`_
25       The document which brought up theming support for Tk
28 Using Ttk
29 ---------
31 To start using Ttk, import its module::
33    import ttk
35 But code like this::
37    from Tkinter import *
39 may optionally want to use this::
41    from Tkinter import *
42    from ttk import *
44 And then several :mod:`ttk` widgets (:class:`Button`, :class:`Checkbutton`,
45 :class:`Entry`, :class:`Frame`, :class:`Label`, :class:`LabelFrame`,
46 :class:`Menubutton`, :class:`PanedWindow`, :class:`Radiobutton`, :class:`Scale`
47 and :class:`Scrollbar`) will automatically substitute for the Tk widgets.
49 This has the direct benefit of using the new widgets, giving better look & feel
50 across platforms, but be aware that they are not totally compatible. The main
51 difference is that widget options such as "fg", "bg" and others related to
52 widget styling are no longer present in Ttk widgets. Use :class:`ttk.Style` to
53 achieve the same (or better) styling.
55 .. seealso::
57    `Converting existing applications to use the Tile widgets <http://tktable.sourceforge.net/tile/doc/converting.txt>`_
58      A text which talks in Tcl terms about differences typically found when
59      converting applications to use the new widgets.
62 Ttk Widgets
63 -----------
65 Ttk comes with 17 widgets, 11 of which already exist in Tkinter:
66 :class:`Button`, :class:`Checkbutton`, :class:`Entry`, :class:`Frame`,
67 :class:`Label`, :class:`LabelFrame`, :class:`Menubutton`,
68 :class:`PanedWindow`, :class:`Radiobutton`, :class:`Scale` and
69 :class:`Scrollbar`. The 6 new widget classes are: :class:`Combobox`,
70 :class:`Notebook`, :class:`Progressbar`, :class:`Separator`,
71 :class:`Sizegrip` and :class:`Treeview`.  All of these classes are
72 subclasses of :class:`Widget`.
74 As said previously, you will notice changes in look-and-feel as well in the
75 styling code. To demonstrate the latter, a very simple example is shown below.
77 Tk code::
79    l1 = Tkinter.Label(text="Test", fg="black", bg="white")
80    l2 = Tkinter.Label(text="Test", fg="black", bg="white")
83 Corresponding Ttk code::
85    style = ttk.Style()
86    style.configure("BW.TLabel", foreground="black", background="white")
88    l1 = ttk.Label(text="Test", style="BW.TLabel")
89    l2 = ttk.Label(text="Test", style="BW.TLabel")
91 For more information about TtkStyling_ read the :class:`Style` class
92 documentation.
94 Widget
95 ------
97 :class:`ttk.Widget` defines standard options and methods supported by Tk
98 themed widgets and is not supposed to be directly instantiated.
101 Standard Options
102 ^^^^^^^^^^^^^^^^
104 All the :mod:`ttk` widgets accept the following options:
106    +-----------+--------------------------------------------------------------+
107    | Option    | Description                                                  |
108    +===========+==============================================================+
109    | class     | Specifies the window class. The class is used when querying  |
110    |           | the option database for the window's other options, to       |
111    |           | determine the default bindtags for the window, and to select |
112    |           | the widget's default layout and style. This is a read-only   |
113    |           | option which may only be specified when the window is        |
114    |           | created.                                                     |
115    +-----------+--------------------------------------------------------------+
116    | cursor    | Specifies the mouse cursor to be used for the widget. If set |
117    |           | to the empty string (the default), the cursor is inherited   |
118    |           | from the parent widget.                                      |
119    +-----------+--------------------------------------------------------------+
120    | takefocus | Determines whether the window accepts the focus during       |
121    |           | keyboard traversal. 0, 1 or an empty string is returned.     |
122    |           | If 0, the window should be skipped entirely                  |
123    |           | during keyboard traversal. If 1, the window                  |
124    |           | should receive the input focus as long as it is viewable.    |
125    |           | An empty string means that the traversal scripts make the    |
126    |           | decision about whether or not to focus on the window.        |
127    +-----------+--------------------------------------------------------------+
128    | style     | May be used to specify a custom widget style.                |
129    +-----------+--------------------------------------------------------------+
132 Scrollable Widget Options
133 ^^^^^^^^^^^^^^^^^^^^^^^^^
135 The following options are supported by widgets that are controlled by a
136 scrollbar.
138    +----------------+---------------------------------------------------------+
139    | option         | description                                             |
140    +================+=========================================================+
141    | xscrollcommand | Used to communicate with horizontal scrollbars.         |
142    |                |                                                         |
143    |                | When the view in the widget's window changes, the widget|
144    |                | will generate a Tcl command based on the scrollcommand. |
145    |                |                                                         |
146    |                | Usually this option consists of the                     |
147    |                | :meth:`Scrollbar.set` method of some scrollbar. This    |
148    |                | will cause                                              |
149    |                | the scrollbar to be updated whenever the view in the    |
150    |                | window changes.                                         |
151    +----------------+---------------------------------------------------------+
152    | yscrollcommand | Used to communicate with vertical scrollbars.           |
153    |                | For more information, see above.                        |
154    +----------------+---------------------------------------------------------+
157 Label Options
158 ^^^^^^^^^^^^^
160 The following options are supported by labels, buttons and other button-like
161 widgets.
163    +--------------+-----------------------------------------------------------+
164    | option       | description                                               |
165    +==============+===========================================================+
166    | text         | Specifies a text string to be displayed inside the widget.|
167    +--------------+-----------------------------------------------------------+
168    | textvariable | Specifies a name whose value will be used in place of the |
169    |              | text option resource.                                     |
170    +--------------+-----------------------------------------------------------+
171    | underline    | If set, specifies the index (0-based) of a character to   |
172    |              | underline in the text string. The underline character is  |
173    |              | used for mnemonic activation.                             |
174    +--------------+-----------------------------------------------------------+
175    | image        | Specifies an image to display. This is a list of 1 or more|
176    |              | elements. The first element is the default image name. The|
177    |              | rest of the list is a sequence of statespec/value pairs as|
178    |              | defined by :meth:`Style.map`, specifying different images |
179    |              | to use when the widget is in a particular state or a      |
180    |              | combination of states. All images in the list should have |
181    |              | the same size.                                            |
182    +--------------+-----------------------------------------------------------+
183    | compound     | Specifies how to display the image relative to the text,  |
184    |              | in the case both text and image options are present.      |
185    |              | Valid values are:                                         |
186    |              |                                                           |
187    |              | * text: display text only                                 |
188    |              | * image: display image only                               |
189    |              | * top, bottom, left, right: display image above, below,   |
190    |              |   left of, or right of the text, respectively.            |
191    |              | * none: the default. display the image if present,        |
192    |              |   otherwise the text.                                     |
193    +--------------+-----------------------------------------------------------+
194    | width        | If greater than zero, specifies how much space, in        |
195    |              | character widths, to allocate for the text label; if less |
196    |              | than zero, specifies a minimum width. If zero or          |
197    |              | unspecified, the natural width of the text label is used. |
198    +--------------+-----------------------------------------------------------+
201 Compatibility Options
202 ^^^^^^^^^^^^^^^^^^^^^
204    +--------+----------------------------------------------------------------+
205    | option | description                                                    |
206    +========+================================================================+
207    | state  | May be set to "normal" or "disabled" to control the "disabled" |
208    |        | state bit. This is a write-only option: setting it changes the |
209    |        | widget state, but the :meth:`Widget.state` method does not     |
210    |        | affect this option.                                            |
211    +--------+----------------------------------------------------------------+
213 Widget States
214 ^^^^^^^^^^^^^
216 The widget state is a bitmap of independent state flags.
218    +------------+-------------------------------------------------------------+
219    | flag       | description                                                 |
220    +============+=============================================================+
221    | active     | The mouse cursor is over the widget and pressing a mouse    |
222    |            | button will cause some action to occur.                     |
223    +------------+-------------------------------------------------------------+
224    | disabled   | Widget is disabled under program control.                   |
225    +------------+-------------------------------------------------------------+
226    | focus      | Widget has keyboard focus.                                  |
227    +------------+-------------------------------------------------------------+
228    | pressed    | Widget is being pressed.                                    |
229    +------------+-------------------------------------------------------------+
230    | selected   | "On", "true", or "current" for things like Checkbuttons and |
231    |            | radiobuttons.                                               |
232    +------------+-------------------------------------------------------------+
233    | background | Windows and Mac have a notion of an "active" or foreground  |
234    |            | window. The *background* state is set for widgets in a      |
235    |            | background window, and cleared for those in the foreground  |
236    |            | window.                                                     |
237    +------------+-------------------------------------------------------------+
238    | readonly   | Widget should not allow user modification.                  |
239    +------------+-------------------------------------------------------------+
240    | alternate  | A widget-specific alternate display format.                 |
241    +------------+-------------------------------------------------------------+
242    | invalid    | The widget's value is invalid.                              |
243    +------------+-------------------------------------------------------------+
245 A state specification is a sequence of state names, optionally prefixed with
246 an exclamation point indicating that the bit is off.
249 ttk.Widget
250 ^^^^^^^^^^
252 Besides the methods described below, the :class:`ttk.Widget` class supports the
253 :meth:`Tkinter.Widget.cget` and :meth:`Tkinter.Widget.configure` methods.
255 .. class:: Widget
257    .. method:: identify(x, y)
259       Returns the name of the element at position *x* *y*, or the empty string
260       if the point does not lie within any element.
262       *x* and *y* are pixel coordinates relative to the widget.
265    .. method:: instate(statespec[, callback=None[, *args[, **kw]]])
267       Test the widget's state. If a callback is not specified, returns True
268       if the widget state matches *statespec* and False otherwise. If callback
269       is specified then it is called with *args* if widget state matches
270       *statespec*.
273    .. method:: state([statespec=None])
275       Modify or read widget state. If *statespec* is specified, sets the
276       widget state accordingly and returns a new *statespec* indicating
277       which flags were changed. If *statespec* is not specified, returns
278       the currently-enabled state flags.
280    *statespec* will usually be a list or a tuple.
283 Combobox
284 --------
286 The :class:`ttk.Combobox` widget combines a text field with a pop-down list of
287 values. This widget is a subclass of :class:`Entry`.
289 Besides the methods inherited from :class:`Widget` (:meth:`Widget.cget`,
290 :meth:`Widget.configure`, :meth:`Widget.identify`, :meth:`Widget.instate`
291 and :meth:`Widget.state`) and those inherited from :class:`Entry`
292 (:meth:`Entry.bbox`, :meth:`Entry.delete`, :meth:`Entry.icursor`,
293 :meth:`Entry.index`, :meth:`Entry.inset`, :meth:`Entry.selection`,
294 :meth:`Entry.xview`), this class has some other methods, described at
295 :class:`ttk.Combobox`.
298 Options
299 ^^^^^^^
301 This widget accepts the following options:
303    +-----------------+--------------------------------------------------------+
304    | option          | description                                            |
305    +=================+========================================================+
306    | exportselection | Boolean value. If set, the widget selection is linked  |
307    |                 | to the Window Manager selection (which can be returned |
308    |                 | by invoking :meth:`Misc.selection_get`, for example).  |
309    +-----------------+--------------------------------------------------------+
310    | justify         | Specifies how the text is aligned within the widget.   |
311    |                 | One of "left", "center", or "right".                   |
312    +-----------------+--------------------------------------------------------+
313    | height          | Specifies the height of the pop-down listbox, in rows. |
314    +-----------------+--------------------------------------------------------+
315    | postcommand     | A script (possibly registered with                     |
316    |                 | :meth:`Misc.register`) that                            |
317    |                 | is called immediately before displaying the values. It |
318    |                 | may specify which values to display.                   |
319    +-----------------+--------------------------------------------------------+
320    | state           | One of "normal", "readonly", or "disabled". In the     |
321    |                 | "readonly" state, the value may not be edited directly,|
322    |                 | and the user can only select one of the values from the|
323    |                 | dropdown list. In the "normal" state, the text field is|
324    |                 | directly editable. In the "disabled" state, no         |
325    |                 | interaction is possible.                               |
326    +-----------------+--------------------------------------------------------+
327    | textvariable    | Specifies a name whose value is linked to the widget   |
328    |                 | value. Whenever the value associated with that name    |
329    |                 | changes, the widget value is updated, and vice versa.  |
330    |                 | See :class:`Tkinter.StringVar`.                        |
331    +-----------------+--------------------------------------------------------+
332    | values          | Specifies the list of values to display in the         |
333    |                 | drop-down listbox.                                     |
334    +-----------------+--------------------------------------------------------+
335    | width           | Specifies an integer value indicating the desired width|
336    |                 | of the entry window, in average-size characters of the |
337    |                 | widget's font.                                         |
338    +-----------------+--------------------------------------------------------+
341 Virtual events
342 ^^^^^^^^^^^^^^
344 The combobox widget generates a **<<ComboboxSelected>>** virtual event
345 when the user selects an element from the list of values.
348 ttk.Combobox
349 ^^^^^^^^^^^^
351 .. class:: Combobox
353    .. method:: current([newindex=None])
355       If *newindex* is specified, sets the combobox value to the element
356       position *newindex*. Otherwise, returns the index of the current value or
357       -1 if the current value is not in the values list.
360    .. method:: get()
362       Returns the current value of the combobox.
365    .. method:: set(value)
367       Sets the value of the combobox to *value*.
370 Notebook
371 --------
373 The Ttk Notebook widget manages a collection of windows and displays a single
374 one at a time. Each child window is associated with a tab, which the user
375 may select to change the currently-displayed window.
378 Options
379 ^^^^^^^
381 This widget accepts the following specific options:
383    +---------+----------------------------------------------------------------+
384    | option  | description                                                    |
385    +=========+================================================================+
386    | height  | If present and greater than zero, specifies the desired height |
387    |         | of the pane area (not including internal padding or tabs).     |
388    |         | Otherwise, the maximum height of all panes is used.            |
389    +---------+----------------------------------------------------------------+
390    | padding | Specifies the amount of extra space to add around the outside  |
391    |         | of the notebook. The padding is a list of up to four length    |
392    |         | specifications: left top right bottom. If fewer than four      |
393    |         | elements are specified, bottom defaults to top, right defaults |
394    |         | to left, and top defaults to left.                             |
395    +---------+----------------------------------------------------------------+
396    | width   | If present and greater than zero, specifies the desired width  |
397    |         | of the pane area (not including internal padding). Otherwise,  |
398    |         | the maximum width of all panes is used.                        |
399    +---------+----------------------------------------------------------------+
402 Tab Options
403 ^^^^^^^^^^^
405 There are also specific options for tabs:
407    +-----------+--------------------------------------------------------------+
408    | option    | description                                                  |
409    +===========+==============================================================+
410    | state     | Either "normal", "disabled" or "hidden". If "disabled", then |
411    |           | the tab is not selectable. If "hidden", then the tab is not  |
412    |           | shown.                                                       |
413    +-----------+--------------------------------------------------------------+
414    | sticky    | Specifies how the child window is positioned within the pane |
415    |           | area. Value is a string containing zero or more of the       |
416    |           | characters "n", "s", "e" or "w". Each letter refers to a     |
417    |           | side (north, south, east or west) that the child window will |
418    |           | stick to, as per the :meth:`grid` geometry manager.          |
419    +-----------+--------------------------------------------------------------+
420    | padding   | Specifies the amount of extra space to add between the       |
421    |           | notebook and this pane. Syntax is the same as for the option |
422    |           | padding used by this widget.                                 |
423    +-----------+--------------------------------------------------------------+
424    | text      | Specifies a text to be displayed in the tab.                 |
425    +-----------+--------------------------------------------------------------+
426    | image     | Specifies an image to display in the tab. See the option     |
427    |           | image described in :class:`Widget`.                          |
428    +-----------+--------------------------------------------------------------+
429    | compound  | Specifies how to display the image relative to the text, in  |
430    |           | the case both text and image options are present. See        |
431    |           | `Label Options`_ for legal values.                           |
432    +-----------+--------------------------------------------------------------+
433    | underline | Specifies the index (0-based) of a character to underline in |
434    |           | the text string. The underlined character is used for        |
435    |           | mnemonic activation if :meth:`Notebook.enable_traversal` is  |
436    |           | called.                                                      |
437    +-----------+--------------------------------------------------------------+
440 Tab Identifiers
441 ^^^^^^^^^^^^^^^
443 The *tab_id* present in several methods of :class:`ttk.Notebook` may take any
444 of the following forms:
446 * An integer between zero and the number of tabs.
447 * The name of a child window.
448 * A positional specification of the form "@x,y", which identifies the tab.
449 * The literal string "current", which identifies the currently-selected tab.
450 * The literal string "end", which returns the number of tabs (only valid for
451   :meth:`Notebook.index`).
454 Virtual Events
455 ^^^^^^^^^^^^^^
457 This widget generates a **<<NotebookTabChanged>>** virtual event after a new
458 tab is selected.
461 ttk.Notebook
462 ^^^^^^^^^^^^
464 .. class:: Notebook
466    .. method:: add(child, **kw)
468       Adds a new tab to the notebook.
470       If window is currently managed by the notebook but hidden, it is
471       restored to its previous position.
473       See `Tab Options`_ for the list of available options.
476    .. method:: forget(tab_id)
478       Removes the tab specified by *tab_id*, unmaps and unmanages the
479       associated window.
482    .. method:: hide(tab_id)
484       Hides the tab specified by *tab_id*.
486       The tab will not be displayed, but the associated window remains
487       managed by the notebook and its configuration remembered. Hidden tabs
488       may be restored with the :meth:`add` command.
491    .. method:: identify(x, y)
493       Returns the name of the tab element at position *x*, *y*, or the empty
494       string if none.
497    .. method:: index(tab_id)
499       Returns the numeric index of the tab specified by *tab_id*, or the total
500       number of tabs if *tab_id* is the string "end".
503    .. method:: insert(pos, child, **kw)
505       Inserts a pane at the specified position.
507       *pos* is either the string "end", an integer index, or the name of a
508       managed child. If *child* is already managed by the notebook, moves it to
509       the specified position.
511       See `Tab Options`_ for the list of available options.
514    .. method:: select([tab_id])
516       Selects the specified *tab_id*.
518       The associated child window will be displayed, and the
519       previously-selected window (if different) is unmapped. If *tab_id* is
520       omitted, returns the widget name of the currently selected pane.
523    .. method:: tab(tab_id[, option=None[, **kw]])
525       Query or modify the options of the specific *tab_id*.
527       If *kw* is not given, returns a dictionary of the tab option values. If
528       *option* is specified, returns the value of that *option*. Otherwise,
529       sets the options to the corresponding values.
532    .. method:: tabs()
534       Returns a list of windows managed by the notebook.
537    .. method:: enable_traversal()
539       Enable keyboard traversal for a toplevel window containing this notebook.
541       This will extend the bindings for the toplevel window containing the
542       notebook as follows:
544       * Control-Tab: selects the tab following the currently selected one.
545       * Shift-Control-Tab: selects the tab preceding the currently selected one.
546       * Alt-K: where K is the mnemonic (underlined) character of any tab, will
547         select that tab.
549       Multiple notebooks in a single toplevel may be enabled for traversal,
550       including nested notebooks. However, notebook traversal only works
551       properly if all panes have the notebook they are in as master.
554 Progressbar
555 -----------
557 The :class:`ttk.Progressbar` widget shows the status of a long-running
558 operation. It can operate in two modes: determinate mode shows the amount
559 completed relative to the total amount of work to be done, and indeterminate
560 mode provides an animated display to let the user know that something is
561 happening.
564 Options
565 ^^^^^^^
567 This widget accepts the following specific options:
569    +----------+---------------------------------------------------------------+
570    | option   | description                                                   |
571    +==========+===============================================================+
572    | orient   | One of "horizontal" or "vertical". Specifies the orientation  |
573    |          | of the progress bar.                                          |
574    +----------+---------------------------------------------------------------+
575    | length   | Specifies the length of the long axis of the progress bar     |
576    |          | (width if horizontal, height if vertical).                    |
577    +----------+---------------------------------------------------------------+
578    | mode     | One of "determinate" or "indeterminate".                      |
579    +----------+---------------------------------------------------------------+
580    | maximum  | A number specifying the maximum value. Defaults to 100.       |
581    +----------+---------------------------------------------------------------+
582    | value    | The current value of the progress bar. In "determinate" mode, |
583    |          | this represents the amount of work completed. In              |
584    |          | "indeterminate" mode, it is interpreted as modulo *maximum*;  |
585    |          | that is, the progress bar completes one "cycle" when its value|
586    |          | increases by *maximum*.                                       |
587    +----------+---------------------------------------------------------------+
588    | variable | A name which is linked to the option value. If specified, the |
589    |          | value of the progress bar is automatically set to the value of|
590    |          | this name whenever the latter is modified.                    |
591    +----------+---------------------------------------------------------------+
592    | phase    | Read-only option. The widget periodically increments the value|
593    |          | of this option whenever its value is greater than 0 and, in   |
594    |          | determinate mode, less than maximum. This option may be used  |
595    |          | by the current theme to provide additional animation effects. |
596    +----------+---------------------------------------------------------------+
599 ttk.Progressbar
600 ^^^^^^^^^^^^^^^
602 .. class:: Progressbar
604    .. method:: start([interval])
606       Begin autoincrement mode: schedules a recurring timer event that calls
607       :meth:`Progressbar.step` every *interval* milliseconds. If omitted,
608       *interval* defaults to 50 milliseconds.
611    .. method:: step([amount])
613       Increments the progress bar's value by *amount*.
615       *amount* defaults to 1.0 if omitted.
618    .. method:: stop()
620       Stop autoincrement mode: cancels any recurring timer event initiated by
621       :meth:`Progressbar.start` for this progress bar.
624 Separator
625 ---------
627 The :class:`ttk.Separator` widget displays a horizontal or vertical separator
628 bar.
630 It has no other methods besides the ones inherited from :class:`ttk.Widget`.
633 Options
634 ^^^^^^^
636 This widget accepts the following specific option:
638    +--------+----------------------------------------------------------------+
639    | option | description                                                    |
640    +========+================================================================+
641    | orient | One of "horizontal" or "vertical". Specifies the orientation of|
642    |        | the separator.                                                 |
643    +--------+----------------------------------------------------------------+
646 Sizegrip
647 --------
649 The :class:`ttk.Sizegrip` widget (also known as a grow box) allows the user to
650 resize the containing toplevel window by pressing and dragging the grip.
652 This widget has neither specific options nor specific methods, besides the
653 ones inherited from :class:`ttk.Widget`.
656 Platform-specific notes
657 ^^^^^^^^^^^^^^^^^^^^^^^
659 * On Mac OS X, toplevel windows automatically include a built-in size grip
660   by default. Adding a :class:`Sizegrip` is harmless, since the built-in
661   grip will just mask the widget.
664 Bugs
665 ^^^^
667 * If the containing toplevel's position was specified relative to the right
668   or bottom of the screen (e.g. ....), the :class:`Sizegrip` widget will
669   not resize the window.
670 * This widget supports only "southeast" resizing.
673 Treeview
674 --------
676 The :class:`ttk.Treeview` widget displays a hierarchical collection of items.
677 Each item has a textual label, an optional image, and an optional list of data
678 values. The data values are displayed in successive columns after the tree
679 label.
681 The order in which data values are displayed may be controlled by setting
682 the widget option ``displaycolumns``. The tree widget can also display column
683 headings. Columns may be accessed by number or symbolic names listed in the
684 widget option columns. See `Column Identifiers`_.
686 Each item is identified by an unique name. The widget will generate item IDs
687 if they are not supplied by the caller. There is a distinguished root item,
688 named ``{}``. The root item itself is not displayed; its children appear at the
689 top level of the hierarchy.
691 Each item also has a list of tags, which can be used to associate event bindings
692 with individual items and control the appearance of the item.
694 The Treeview widget supports horizontal and vertical scrolling, according to
695 the options described in `Scrollable Widget Options`_ and the methods
696 :meth:`Treeview.xview` and :meth:`Treeview.yview`.
699 Options
700 ^^^^^^^
702 This widget accepts the following specific options:
704    +----------------+--------------------------------------------------------+
705    | option         | description                                            |
706    +================+========================================================+
707    | columns        | A list of column identifiers, specifying the number of |
708    |                | columns and their names.                               |
709    +----------------+--------------------------------------------------------+
710    | displaycolumns | A list of column identifiers (either symbolic or       |
711    |                | integer indices) specifying which data columns are     |
712    |                | displayed and the order in which they appear, or the   |
713    |                | string "#all".                                         |
714    +----------------+--------------------------------------------------------+
715    | height         | Specifies the number of rows which should be visible.  |
716    |                | Note: the requested width is determined from the sum   |
717    |                | of the column widths.                                  |
718    +----------------+--------------------------------------------------------+
719    | padding        | Specifies the internal padding for the widget. The     |
720    |                | padding is a list of up to four length specifications. |
721    +----------------+--------------------------------------------------------+
722    | selectmode     | Controls how the built-in class bindings manage the    |
723    |                | selection. One of "extended", "browse" or "none".      |
724    |                | If set to "extended" (the default), multiple items may |
725    |                | be selected. If "browse", only a single item will be   |
726    |                | selected at a time. If "none", the selection will not  |
727    |                | be changed.                                            |
728    |                |                                                        |
729    |                | Note that the application code and tag bindings can set|
730    |                | the selection however they wish, regardless of the     |
731    |                | value  of this option.                                 |
732    +----------------+--------------------------------------------------------+
733    | show           | A list containing zero or more of the following values,|
734    |                | specifying which elements of the tree to display.      |
735    |                |                                                        |
736    |                | * tree: display tree labels in column #0.              |
737    |                | * headings: display the heading row.                   |
738    |                |                                                        |
739    |                | The default is "tree headings", i.e., show all         |
740    |                | elements.                                              |
741    |                |                                                        |
742    |                | **Note**: Column #0 always refers to the tree column,  |
743    |                | even if show="tree" is not specified.                  |
744    +----------------+--------------------------------------------------------+
747 Item Options
748 ^^^^^^^^^^^^
750 The following item options may be specified for items in the insert and item
751 widget commands.
753    +--------+---------------------------------------------------------------+
754    | option | description                                                   |
755    +========+===============================================================+
756    | text   | The textual label to display for the item.                    |
757    +--------+---------------------------------------------------------------+
758    | image  | A Tk Image, displayed to the left of the label.               |
759    +--------+---------------------------------------------------------------+
760    | values | The list of values associated with the item.                  |
761    |        |                                                               |
762    |        | Each item should have the same number of values as the widget |
763    |        | option columns. If there are fewer values than columns, the   |
764    |        | remaining values are assumed empty. If there are more values  |
765    |        | than columns, the extra values are ignored.                   |
766    +--------+---------------------------------------------------------------+
767    | open   | True/False value indicating whether the item's children should|
768    |        | be displayed or hidden.                                       |
769    +--------+---------------------------------------------------------------+
770    | tags   | A list of tags associated with this item.                     |
771    +--------+---------------------------------------------------------------+
774 Tag Options
775 ^^^^^^^^^^^
777 The following options may be specified on tags:
779    +------------+-----------------------------------------------------------+
780    | option     | description                                               |
781    +============+===========================================================+
782    | foreground | Specifies the text foreground color.                      |
783    +------------+-----------------------------------------------------------+
784    | background | Specifies the cell or item background color.              |
785    +------------+-----------------------------------------------------------+
786    | font       | Specifies the font to use when drawing text.              |
787    +------------+-----------------------------------------------------------+
788    | image      | Specifies the item image, in case the item's image option |
789    |            | is empty.                                                 |
790    +------------+-----------------------------------------------------------+
793 Column Identifiers
794 ^^^^^^^^^^^^^^^^^^
796 Column identifiers take any of the following forms:
798 * A symbolic name from the list of columns option.
799 * An integer n, specifying the nth data column.
800 * A string of the form #n, where n is an integer, specifying the nth display
801   column.
803 Notes:
805 * Item's option values may be displayed in a different order than the order
806   in which they are stored.
807 * Column #0 always refers to the tree column, even if show="tree" is not
808   specified.
810 A data column number is an index into an item's option values list; a display
811 column number is the column number in the tree where the values are displayed.
812 Tree labels are displayed in column #0. If option displaycolumns is not set,
813 then data column n is displayed in column #n+1. Again, **column #0 always
814 refers to the tree column**.
817 Virtual Events
818 ^^^^^^^^^^^^^^
820 The Treeview widget generates the following virtual events.
822    +--------------------+--------------------------------------------------+
823    | event              | description                                      |
824    +====================+==================================================+
825    | <<TreeviewSelect>> | Generated whenever the selection changes.        |
826    +--------------------+--------------------------------------------------+
827    | <<TreeviewOpen>>   | Generated just before settings the focus item to |
828    |                    | open=True.                                       |
829    +--------------------+--------------------------------------------------+
830    | <<TreeviewClose>>  | Generated just after setting the focus item to   |
831    |                    | open=False.                                      |
832    +--------------------+--------------------------------------------------+
834 The :meth:`Treeview.focus` and :meth:`Treeview.selection` methods can be used
835 to determine the affected item or items.
838 ttk.Treeview
839 ^^^^^^^^^^^^
841 .. class:: Treeview
843    .. method:: bbox(item[, column=None])
845       Returns the bounding box (relative to the treeview widget's window) of
846       the specified *item* in the form (x, y, width, height).
848       If *column* is specified, returns the bounding box of that cell. If the
849       *item* is not visible (i.e., if it is a descendant of a closed item or is
850       scrolled offscreen), returns an empty string.
853    .. method:: get_children([item])
855       Returns the list of children belonging to *item*.
857       If *item* is not specified, returns root children.
860    .. method:: set_children(item, *newchildren)
862       Replaces *item*'s child with *newchildren*.
864       Children present in *item* that are not present in *newchildren* are
865       detached from the tree. No items in *newchildren* may be an ancestor of
866       *item*. Note that not specifying *newchildren* results in detaching
867       *item*'s children.
870    .. method:: column(column[, option=None[, **kw]])
872       Query or modify the options for the specified *column*.
874       If *kw* is not given, returns a dict of the column option values. If
875       *option* is specified then the value for that *option* is returned.
876       Otherwise, sets the options to the corresponding values.
878       The valid options/values are:
880       * id
881          Returns the column name. This is a read-only option.
882       * anchor: One of the standard Tk anchor values.
883          Specifies how the text in this column should be aligned with respect
884          to the cell.
885       * minwidth: width
886          The minimum width of the column in pixels. The treeview widget will
887          not make the column any smaller than specified by this option when
888          the widget is resized or the user drags a column.
889       * stretch: True/False
890          Specifies whether the column's width should be adjusted when
891          the widget is resized.
892       * width: width
893          The width of the column in pixels.
895       To configure the tree column, call this with column = "#0"
897    .. method:: delete(*items)
899       Delete all specified *items* and all their descendants.
901       The root item may not be deleted.
904    .. method:: detach(*items)
906       Unlinks all of the specified *items* from the tree.
908       The items and all of their descendants are still present, and may be
909       reinserted at another point in the tree, but will not be displayed.
911       The root item may not be detached.
914    .. method:: exists(item)
916       Returns True if the specified *item* is present in the tree.
919    .. method:: focus([item=None])
921       If *item* is specified, sets the focus item to *item*. Otherwise, returns
922       the current focus item, or '' if there is none.
925    .. method:: heading(column[, option=None[, **kw]])
927       Query or modify the heading options for the specified *column*.
929       If *kw* is not given, returns a dict of the heading option values. If
930       *option* is specified then the value for that *option* is returned.
931       Otherwise, sets the options to the corresponding values.
933       The valid options/values are:
935       * text: text
936          The text to display in the column heading.
937       * image: imageName
938          Specifies an image to display to the right of the column heading.
939       * anchor: anchor
940          Specifies how the heading text should be aligned. One of the standard
941          Tk anchor values.
942       * command: callback
943          A callback to be invoked when the heading label is pressed.
945       To configure the tree column heading, call this with column = "#0".
948    .. method:: identify(component, x, y)
950       Returns a description of the specified *component* under the point given
951       by *x* and *y*, or the empty string if no such *component* is present at
952       that position.
955    .. method:: identify_row(y)
957       Returns the item ID of the item at position *y*.
960    .. method:: identify_column(x)
962       Returns the data column identifier of the cell at position *x*.
964       The tree column has ID #0.
967    .. method:: identify_region(x, y)
969       Returns one of:
971       +-----------+--------------------------------------+
972       | region    | meaning                              |
973       +===========+======================================+
974       | heading   | Tree heading area.                   |
975       +-----------+--------------------------------------+
976       | separator | Space between two columns headings.  |
977       +-----------+--------------------------------------+
978       | tree      | The tree area.                       |
979       +-----------+--------------------------------------+
980       | cell      | A data cell.                         |
981       +-----------+--------------------------------------+
983       Availability: Tk 8.6.
986    .. method:: identify_element(x, y)
988       Returns the element at position *x*, *y*.
990       Availability: Tk 8.6.
993    .. method:: index(item)
995       Returns the integer index of *item* within its parent's list of children.
998    .. method:: insert(parent, index[, iid=None[, **kw]])
1000       Creates a new item and returns the item identifier of the newly created
1001       item.
1003       *parent* is the item ID of the parent item, or the empty string to create
1004       a new top-level item. *index* is an integer, or the value "end",
1005       specifying where in the list of parent's children to insert the new item.
1006       If *index* is less than or equal to zero, the new node is inserted at
1007       the beginning; if *index* is greater than or equal to the current number
1008       of children, it is inserted at the end. If *iid* is specified, it is used
1009       as the item identifier; *iid* must not already exist in the tree.
1010       Otherwise, a new unique identifier is generated.
1012       See `Item Options`_ for the list of available points.
1015    .. method:: item(item[, option[, **kw]])
1017       Query or modify the options for the specified *item*.
1019       If no options are given, a dict with options/values for the item is
1020       returned.
1021       If *option* is specified then the value for that option is returned.
1022       Otherwise, sets the options to the corresponding values as given by *kw*.
1025    .. method:: move(item, parent, index)
1027       Moves *item* to position *index* in *parent*'s list of children.
1029       It is illegal to move an item under one of its descendants. If *index* is
1030       less than or equal to zero, *item* is moved to the beginning; if greater
1031       than or equal to the number of children, it is moved to the end. If *item*
1032       was detached it is reattached.
1035    .. method:: next(item)
1037       Returns the identifier of *item*'s next sibling, or '' if *item* is the
1038       last child of its parent.
1041    .. method:: parent(item)
1043       Returns the ID of the parent of *item*, or '' if *item* is at the top
1044       level of the hierarchy.
1047    .. method:: prev(item)
1049       Returns the identifier of *item*'s previous sibling, or '' if *item* is
1050       the first child of its parent.
1053    .. method:: reattach(item, parent, index)
1055       An alias for :meth:`Treeview.move`.
1058    .. method:: see(item)
1060       Ensure that *item* is visible.
1062       Sets all of *item*'s ancestors open option to True, and scrolls the
1063       widget if necessary so that *item* is within the visible portion of
1064       the tree.
1067    .. method:: selection([selop=None[, items=None]])
1069       If *selop* is not specified, returns selected items. Otherwise, it will
1070       act according to the following selection methods.
1073    .. method:: selection_set(items)
1075       *items* becomes the new selection.
1078    .. method:: selection_add(items)
1080       Add *items* to the selection.
1083    .. method:: selection_remove(items)
1085       Remove *items* from the selection.
1088    .. method:: selection_toggle(items)
1090       Toggle the selection state of each item in *items*.
1093    .. method:: set(item[, column=None[, value=None]])
1095       With one argument, returns a dictionary of column/value pairs for the
1096       specified *item*. With two arguments, returns the current value of the
1097       specified *column*. With three arguments, sets the value of given
1098       *column* in given *item* to the specified *value*.
1101    .. method:: tag_bind(tagname[, sequence=None[, callback=None]])
1103       Bind a callback for the given event *sequence* to the tag *tagname*.
1104       When an event is delivered to an item, the callbacks for each of the
1105       item's tags option are called.
1108    .. method:: tag_configure(tagname[, option=None[, **kw]])
1110       Query or modify the options for the specified *tagname*.
1112       If *kw* is not given, returns a dict of the option settings for
1113       *tagname*. If *option* is specified, returns the value for that *option*
1114       for the specified *tagname*. Otherwise, sets the options to the
1115       corresponding values for the given *tagname*.
1118    .. method:: tag_has(tagname[, item])
1120       If *item* is specified, returns 1 or 0 depending on whether the specified
1121       *item* has the given *tagname*. Otherwise, returns a list of all items
1122       that have the specified tag.
1124       Availability: Tk 8.6
1127    .. method:: xview(*args)
1129       Query or modify horizontal position of the treeview.
1132    .. method:: yview(*args)
1134       Query or modify vertical position of the treeview.
1137 .. _TtkStyling:
1139 Ttk Styling
1140 -----------
1142 Each widget in :mod:`ttk` is assigned a style, which specifies the set of
1143 elements making up the widget and how they are arranged, along with dynamic and
1144 default settings for element options. By default the style name is the same as
1145 the widget's class name, but it may be overridden by the widget's style
1146 option. If the class name of a widget is unknown, use the method
1147 :meth:`Misc.winfo_class` (somewidget.winfo_class()).
1149 .. seealso::
1151    `Tcl'2004 conference presentation <http://tktable.sourceforge.net/tile/tile-tcl2004.pdf>`_
1152       This document explains how the theme engine works
1155 .. class:: Style
1157    This class is used to manipulate the style database.
1160    .. method:: configure(style, query_opt=None, **kw)
1162       Query or set the default value of the specified option(s) in *style*.
1164       Each key in *kw* is an option and each value is a string identifying
1165       the value for that option.
1167       For example, to change every default button to be a flat button with some
1168       padding and a different background color do::
1170          import ttk
1171          import Tkinter
1173          root = Tkinter.Tk()
1175          ttk.Style().configure("TButton", padding=6, relief="flat",
1176             background="#ccc")
1178          btn = ttk.Button(text="Sample")
1179          btn.pack()
1181          root.mainloop()
1184    .. method:: map(style, query_opt=None, **kw)
1186       Query or sets dynamic values of the specified option(s) in *style*.
1188       Each key in *kw* is an option and each value should be a list or a
1189       tuple (usually) containing statespecs grouped in tuples, lists, or
1190       something else of your preference. A statespec is a compound of one
1191       or more states and then a value.
1193       An example::
1195          import Tkinter
1196          import ttk
1198          root = Tkinter.Tk()
1200          style = ttk.Style()
1201          style.map("C.TButton",
1202              foreground=[('pressed', 'red'), ('active', 'blue')],
1203              background=[('pressed', '!disabled', 'black'), ('active', 'white')]
1204              )
1206          colored_btn = ttk.Button(text="Test", style="C.TButton").pack()
1208          root.mainloop()
1211       Note that the order of the (states, value) sequences for an
1212       option matters.  In the previous example, if you change the
1213       order to ``[('active', 'blue'), ('pressed', 'red')]`` in the
1214       foreground option, for example, you would get a blue foreground
1215       when the widget is in the active or pressed states.
1217    .. method:: lookup(style, option[, state=None[, default=None]])
1219       Returns the value specified for *option* in *style*.
1221       If *state* is specified, it is expected to be a sequence of one or more
1222       states. If the *default* argument is set, it is used as a fallback value
1223       in case no specification for option is found.
1225       To check what font a Button uses by default, do::
1227          import ttk
1229          print ttk.Style().lookup("TButton", "font")
1232    .. method:: layout(style[, layoutspec=None])
1234       Define the widget layout for given *style*. If *layoutspec* is omitted,
1235       return the layout specification for given style.
1237       *layoutspec*, if specified, is expected to be a list or some other
1238       sequence type (excluding strings), where each item should be a tuple and
1239       the first item is the layout name and the second item should have the
1240       format described described in `Layouts`_.
1242       To understand the format, see the following example (it is not
1243       intended to do anything useful)::
1245          import ttk
1246          import Tkinter
1248          root = Tkinter.Tk()
1250          style = ttk.Style()
1251          style.layout("TMenubutton", [
1252             ("Menubutton.background", None),
1253             ("Menubutton.button", {"children":
1254                 [("Menubutton.focus", {"children":
1255                     [("Menubutton.padding", {"children":
1256                         [("Menubutton.label", {"side": "left", "expand": 1})]
1257                     })]
1258                 })]
1259             }),
1260          ])
1262          mbtn = ttk.Menubutton(text='Text')
1263          mbtn.pack()
1264          root.mainloop()
1267    .. method:: element_create(elementname, etype, *args, **kw)
1269       Create a new element in the current theme, of the given *etype* which is
1270       expected to be either "image", "from" or "vsapi". The latter is only
1271       available in Tk 8.6a for Windows XP and Vista and is not described here.
1273       If "image" is used, *args* should contain the default image name followed
1274       by statespec/value pairs (this is the imagespec), and *kw* may have the
1275       following options:
1277        * border=padding
1278           padding is a list of up to four integers, specifying the left, top,
1279           right, and bottom borders, respectively.
1281        * height=height
1282           Specifies a minimum height for the element. If less than zero, the
1283           base image's height is used as a default.
1285        * padding=padding
1286           Specifies the element's interior padding. Defaults to border's value
1287           if not specified.
1289        * sticky=spec
1290           Specifies how the image is placed within the final parcel. spec
1291           contains zero or more characters “n”, “s”, “w”, or “e”.
1293        * width=width
1294           Specifies a minimum width for the element. If less than zero, the
1295           base image's width is used as a default.
1297       If "from" is used as the value of *etype*,
1298       :meth:`element_create` will clone an existing
1299       element. *args* is expected to contain a themename, from which
1300       the element will be cloned, and optionally an element to clone from.
1301       If this element to clone from is not specified, an empty element will
1302       be used. *kw* is discarded.
1305    .. method:: element_names()
1307       Returns the list of elements defined in the current theme.
1310    .. method:: element_options(elementname)
1312       Returns the list of *elementname*'s options.
1315    .. method:: theme_create(themename[, parent=None[, settings=None]])
1317       Create a new theme.
1319       It is an error if *themename* already exists. If *parent* is specified,
1320       the new theme will inherit styles, elements and layouts from the parent
1321       theme. If *settings* are present they are expected to have the same
1322       syntax used for :meth:`theme_settings`.
1325    .. method:: theme_settings(themename, settings)
1327       Temporarily sets the current theme to *themename*, apply specified
1328       *settings* and then restore the previous theme.
1330       Each key in *settings* is a style and each value may contain the keys
1331       'configure', 'map', 'layout' and 'element create' and they are expected
1332       to have the same format as specified by the methods
1333       :meth:`Style.configure`, :meth:`Style.map`, :meth:`Style.layout` and
1334       :meth:`Style.element_create` respectively.
1336       As an example, let's change the Combobox for the default theme a bit::
1338          import ttk
1339          import Tkinter
1341          root = Tkinter.Tk()
1343          style = ttk.Style()
1344          style.theme_settings("default", {
1345             "TCombobox": {
1346                 "configure": {"padding": 5},
1347                 "map": {
1348                     "background": [("active", "green2"),
1349                                    ("!disabled", "green4")],
1350                     "fieldbackground": [("!disabled", "green3")],
1351                     "foreground": [("focus", "OliveDrab1"),
1352                                    ("!disabled", "OliveDrab2")]
1353                 }
1354             }
1355          })
1357          combo = ttk.Combobox().pack()
1359          root.mainloop()
1362    .. method:: theme_names()
1364       Returns a list of all known themes.
1367    .. method:: theme_use([themename])
1369       If *themename* is not given, returns the theme in use.  Otherwise, sets
1370       the current theme to *themename*, refreshes all widgets and emits a
1371       <<ThemeChanged>> event.
1374 Layouts
1375 ^^^^^^^
1377 A layout can be just None, if it takes no options, or a dict of
1378 options specifying how to arrange the element. The layout mechanism
1379 uses a simplified version of the pack geometry manager: given an
1380 initial cavity, each element is allocated a parcel. Valid
1381 options/values are:
1383  * side: whichside
1384     Specifies which side of the cavity to place the element; one of
1385     top, right, bottom or left. If omitted, the element occupies the
1386     entire cavity.
1388  * sticky: nswe
1389     Specifies where the element is placed inside its allocated parcel.
1391  * unit: 0 or 1
1392     If set to 1, causes the element and all of its descendants to be treated as
1393     a single element for the purposes of :meth:`Widget.identify` et al. It's
1394     used for things like scrollbar thumbs with grips.
1396  * children: [sublayout... ]
1397     Specifies a list of elements to place inside the element. Each
1398     element is a tuple (or other sequence type) where the first item is
1399     the layout name, and the other is a `Layout`_.
1401 .. _Layout: `Layouts`_