blob: 76ecfcce497775ebf4e2c6dec977952602bb1427 [file] [log] [blame]
Guilherme Polo5f238482009-01-28 14:41:10 +00001:mod:`tkinter.ttk` --- Tk themed widgets
2========================================
3
4.. module:: tkinter.ttk
5 :synopsis: Tk themed widget set
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Guilherme Polo5f238482009-01-28 14:41:10 +00007.. sectionauthor:: Guilherme Polo <ggpolo@gmail.com>
8
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04009**Source code:** :source:`Lib/tkinter/ttk.py`
Guilherme Polo5f238482009-01-28 14:41:10 +000010
11.. index:: single: ttk
12
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040013--------------
14
Guilherme Polo5f238482009-01-28 14:41:10 +000015The :mod:`tkinter.ttk` module provides access to the Tk themed widget set,
Raymond Hettinger238018c2009-04-08 08:23:44 +000016introduced in Tk 8.5. If Python has not been compiled against Tk 8.5, this
17module can still be accessed if *Tile* has been installed. The former
18method using Tk 8.5 provides additional benefits including anti-aliased font
19rendering under X11 and window transparency (requiring a composition
20window manager on X11).
Guilherme Polo5f238482009-01-28 14:41:10 +000021
Raymond Hettinger238018c2009-04-08 08:23:44 +000022The basic idea for :mod:`tkinter.ttk` is to separate, to the extent possible,
Guilherme Polo5f238482009-01-28 14:41:10 +000023the code implementing a widget's behavior from the code implementing its
24appearance.
25
26
27.. seealso::
28
Sanyam Khurana338cd832018-01-20 05:55:37 +053029 `Tk Widget Styling Support <https://core.tcl.tk/tips/doc/trunk/tip/48.md>`_
Raymond Hettinger238018c2009-04-08 08:23:44 +000030 A document introducing theming support for Tk
Guilherme Polo5f238482009-01-28 14:41:10 +000031
32
33Using Ttk
34---------
35
Raymond Hettinger238018c2009-04-08 08:23:44 +000036To start using Ttk, import its module::
Guilherme Polo5f238482009-01-28 14:41:10 +000037
38 from tkinter import ttk
39
Raymond Hettinger238018c2009-04-08 08:23:44 +000040To override the basic Tk widgets, the import should follow the Tk import::
Guilherme Polo5f238482009-01-28 14:41:10 +000041
42 from tkinter import *
43 from tkinter.ttk import *
44
Raymond Hettinger238018c2009-04-08 08:23:44 +000045That code causes several :mod:`tkinter.ttk` widgets (:class:`Button`,
Guilherme Polo5f238482009-01-28 14:41:10 +000046:class:`Checkbutton`, :class:`Entry`, :class:`Frame`, :class:`Label`,
47:class:`LabelFrame`, :class:`Menubutton`, :class:`PanedWindow`,
Raymond Hettinger238018c2009-04-08 08:23:44 +000048:class:`Radiobutton`, :class:`Scale` and :class:`Scrollbar`) to
49automatically replace the Tk widgets.
Guilherme Polo5f238482009-01-28 14:41:10 +000050
Raymond Hettinger238018c2009-04-08 08:23:44 +000051This has the direct benefit of using the new widgets which gives a better
52look and feel across platforms; however, the replacement widgets are not
53completely compatible. The main difference is that widget options such as
54"fg", "bg" and others related to widget styling are no
55longer present in Ttk widgets. Instead, use the :class:`ttk.Style` class
56for improved styling effects.
57
Guilherme Polo5f238482009-01-28 14:41:10 +000058
59.. seealso::
60
Raymond Hettinger238018c2009-04-08 08:23:44 +000061 `Converting existing applications to use Tile widgets <http://tktable.sourceforge.net/tile/doc/converting.txt>`_
62 A monograph (using Tcl terminology) about differences typically
63 encountered when moving applications to use the new widgets.
Guilherme Polo5f238482009-01-28 14:41:10 +000064
65
66Ttk Widgets
67-----------
68
Miss Islington (bot)105fcbf2018-02-09 03:40:14 -080069Ttk comes with 18 widgets, twelve of which already existed in tkinter:
Guilherme Polo5f238482009-01-28 14:41:10 +000070:class:`Button`, :class:`Checkbutton`, :class:`Entry`, :class:`Frame`,
71:class:`Label`, :class:`LabelFrame`, :class:`Menubutton`, :class:`PanedWindow`,
Miss Islington (bot)105fcbf2018-02-09 03:40:14 -080072:class:`Radiobutton`, :class:`Scale`, :class:`Scrollbar`, and :class:`Spinbox`.
73The other six are new: :class:`Combobox`, :class:`Notebook`,
74:class:`Progressbar`, :class:`Separator`, :class:`Sizegrip` and
75:class:`Treeview`. And all them are subclasses of :class:`Widget`.
Guilherme Polo5f238482009-01-28 14:41:10 +000076
Raymond Hettinger238018c2009-04-08 08:23:44 +000077Using the Ttk widgets gives the application an improved look and feel.
78As discussed above, there are differences in how the styling is coded.
Guilherme Polo5f238482009-01-28 14:41:10 +000079
80Tk code::
81
82 l1 = tkinter.Label(text="Test", fg="black", bg="white")
83 l2 = tkinter.Label(text="Test", fg="black", bg="white")
84
85
86Ttk code::
87
88 style = ttk.Style()
89 style.configure("BW.TLabel", foreground="black", background="white")
90
91 l1 = ttk.Label(text="Test", style="BW.TLabel")
92 l2 = ttk.Label(text="Test", style="BW.TLabel")
93
Raymond Hettinger238018c2009-04-08 08:23:44 +000094For more information about TtkStyling_, see the :class:`Style` class
Guilherme Polo5f238482009-01-28 14:41:10 +000095documentation.
96
97Widget
98------
99
100:class:`ttk.Widget` defines standard options and methods supported by Tk
101themed widgets and is not supposed to be directly instantiated.
102
103
104Standard Options
105^^^^^^^^^^^^^^^^
106
107All the :mod:`ttk` Widgets accepts the following options:
108
Georg Brandl44ea77b2013-03-28 13:28:44 +0100109 .. tabularcolumns:: |l|L|
110
Guilherme Polo5f238482009-01-28 14:41:10 +0000111 +-----------+--------------------------------------------------------------+
112 | Option | Description |
113 +===========+==============================================================+
114 | class | Specifies the window class. The class is used when querying |
115 | | the option database for the window's other options, to |
116 | | determine the default bindtags for the window, and to select |
Martin Panterd21e0b52015-10-10 10:36:22 +0000117 | | the widget's default layout and style. This option is |
118 | | read-only, and may only be specified when the window is |
119 | | created. |
Guilherme Polo5f238482009-01-28 14:41:10 +0000120 +-----------+--------------------------------------------------------------+
121 | cursor | Specifies the mouse cursor to be used for the widget. If set |
122 | | to the empty string (the default), the cursor is inherited |
123 | | for the parent widget. |
124 +-----------+--------------------------------------------------------------+
125 | takefocus | Determines whether the window accepts the focus during |
Ezio Melotti1a263ad2010-03-14 09:51:37 +0000126 | | keyboard traversal. 0, 1 or an empty string is returned. |
127 | | If 0 is returned, it means that the window should be skipped |
128 | | entirely during keyboard traversal. If 1, it means that the |
129 | | window should receive the input focus as long as it is |
130 | | viewable. And an empty string means that the traversal |
131 | | scripts make the decision about whether or not to focus |
132 | | on the window. |
Guilherme Polo5f238482009-01-28 14:41:10 +0000133 +-----------+--------------------------------------------------------------+
134 | style | May be used to specify a custom widget style. |
135 +-----------+--------------------------------------------------------------+
136
137
138Scrollable Widget Options
139^^^^^^^^^^^^^^^^^^^^^^^^^
140
141The following options are supported by widgets that are controlled by a
142scrollbar.
143
Georg Brandl44ea77b2013-03-28 13:28:44 +0100144 .. tabularcolumns:: |l|L|
145
Guilherme Polo5f238482009-01-28 14:41:10 +0000146 +----------------+---------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100147 | Option | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000148 +================+=========================================================+
Georg Brandlae2dbe22009-03-13 19:04:40 +0000149 | xscrollcommand | Used to communicate with horizontal scrollbars. |
Guilherme Polo5f238482009-01-28 14:41:10 +0000150 | | |
151 | | When the view in the widget's window change, the widget |
152 | | will generate a Tcl command based on the scrollcommand. |
153 | | |
154 | | Usually this option consists of the method |
155 | | :meth:`Scrollbar.set` of some scrollbar. This will cause|
156 | | the scrollbar to be updated whenever the view in the |
157 | | window changes. |
158 +----------------+---------------------------------------------------------+
Georg Brandlae2dbe22009-03-13 19:04:40 +0000159 | yscrollcommand | Used to communicate with vertical scrollbars. |
Guilherme Polo5f238482009-01-28 14:41:10 +0000160 | | For some more information, see above. |
161 +----------------+---------------------------------------------------------+
162
163
164Label Options
165^^^^^^^^^^^^^
166
167The following options are supported by labels, buttons and other button-like
168widgets.
169
Georg Brandl44ea77b2013-03-28 13:28:44 +0100170 .. tabularcolumns:: |l|p{0.7\linewidth}|
Georg Brandl2c860042009-06-16 19:24:38 +0000171
Guilherme Polo5f238482009-01-28 14:41:10 +0000172 +--------------+-----------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100173 | Option | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000174 +==============+===========================================================+
175 | text | Specifies a text string to be displayed inside the widget.|
176 +--------------+-----------------------------------------------------------+
177 | textvariable | Specifies a name whose value will be used in place of the |
178 | | text option resource. |
179 +--------------+-----------------------------------------------------------+
180 | underline | If set, specifies the index (0-based) of a character to |
181 | | underline in the text string. The underline character is |
182 | | used for mnemonic activation. |
183 +--------------+-----------------------------------------------------------+
184 | image | Specifies an image to display. This is a list of 1 or more|
185 | | elements. The first element is the default image name. The|
186 | | rest of the list if a sequence of statespec/value pairs as|
187 | | defined by :meth:`Style.map`, specifying different images |
188 | | to use when the widget is in a particular state or a |
189 | | combination of states. All images in the list should have |
190 | | the same size. |
191 +--------------+-----------------------------------------------------------+
192 | compound | Specifies how to display the image relative to the text, |
193 | | in the case both text and images options are present. |
194 | | Valid values are: |
195 | | |
196 | | * text: display text only |
197 | | * image: display image only |
198 | | * top, bottom, left, right: display image above, below, |
199 | | left of, or right of the text, respectively. |
200 | | * none: the default. display the image if present, |
201 | | otherwise the text. |
202 +--------------+-----------------------------------------------------------+
203 | width | If greater than zero, specifies how much space, in |
204 | | character widths, to allocate for the text label, if less |
205 | | than zero, specifies a minimum width. If zero or |
206 | | unspecified, the natural width of the text label is used. |
207 +--------------+-----------------------------------------------------------+
208
209
210Compatibility Options
211^^^^^^^^^^^^^^^^^^^^^
212
Georg Brandl44ea77b2013-03-28 13:28:44 +0100213 .. tabularcolumns:: |l|L|
214
Guilherme Polo5f238482009-01-28 14:41:10 +0000215 +--------+----------------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100216 | Option | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000217 +========+================================================================+
218 | state | May be set to "normal" or "disabled" to control the "disabled" |
219 | | state bit. This is a write-only option: setting it changes the |
220 | | widget state, but the :meth:`Widget.state` method does not |
221 | | affect this option. |
222 +--------+----------------------------------------------------------------+
223
224Widget States
225^^^^^^^^^^^^^
226
227The widget state is a bitmap of independent state flags.
228
Georg Brandl44ea77b2013-03-28 13:28:44 +0100229 .. tabularcolumns:: |l|L|
230
Guilherme Polo5f238482009-01-28 14:41:10 +0000231 +------------+-------------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100232 | Flag | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000233 +============+=============================================================+
234 | active | The mouse cursor is over the widget and pressing a mouse |
235 | | button will cause some action to occur |
236 +------------+-------------------------------------------------------------+
237 | disabled | Widget is disabled under program control |
238 +------------+-------------------------------------------------------------+
239 | focus | Widget has keyboard focus |
240 +------------+-------------------------------------------------------------+
241 | pressed | Widget is being pressed |
242 +------------+-------------------------------------------------------------+
243 | selected | "On", "true", or "current" for things like Checkbuttons and |
244 | | radiobuttons |
245 +------------+-------------------------------------------------------------+
246 | background | Windows and Mac have a notion of an "active" or foreground |
247 | | window. The *background* state is set for widgets in a |
248 | | background window, and cleared for those in the foreground |
249 | | window |
250 +------------+-------------------------------------------------------------+
251 | readonly | Widget should not allow user modification |
252 +------------+-------------------------------------------------------------+
253 | alternate | A widget-specific alternate display format |
254 +------------+-------------------------------------------------------------+
255 | invalid | The widget's value is invalid |
256 +------------+-------------------------------------------------------------+
257
258A state specification is a sequence of state names, optionally prefixed with
259an exclamation point indicating that the bit is off.
260
261
262ttk.Widget
263^^^^^^^^^^
264
Benjamin Petersond23f8222009-04-05 19:13:16 +0000265Besides the methods described below, the :class:`ttk.Widget` supports the
Guilherme Polo5f238482009-01-28 14:41:10 +0000266methods :meth:`tkinter.Widget.cget` and :meth:`tkinter.Widget.configure`.
267
268.. class:: Widget
269
270 .. method:: identify(x, y)
271
272 Returns the name of the element at position *x* *y*, or the empty string
273 if the point does not lie within any element.
274
275 *x* and *y* are pixel coordinates relative to the widget.
276
277
Georg Brandl7f01a132009-09-16 15:58:14 +0000278 .. method:: instate(statespec, callback=None, *args, **kw)
Guilherme Polo5f238482009-01-28 14:41:10 +0000279
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200280 Test the widget's state. If a callback is not specified, returns ``True``
281 if the widget state matches *statespec* and ``False`` otherwise. If callback
Guilherme Polo5f238482009-01-28 14:41:10 +0000282 is specified then it is called with args if widget state matches
283 *statespec*.
284
285
Georg Brandl7f01a132009-09-16 15:58:14 +0000286 .. method:: state(statespec=None)
Guilherme Polo5f238482009-01-28 14:41:10 +0000287
288 Modify or inquire widget state. If *statespec* is specified, sets the
289 widget state according to it and return a new *statespec* indicating
290 which flags were changed. If *statespec* is not specified, returns
291 the currently-enabled state flags.
292
293 *statespec* will usually be a list or a tuple.
294
295
296Combobox
297--------
298
299The :class:`ttk.Combobox` widget combines a text field with a pop-down list of
300values. This widget is a subclass of :class:`Entry`.
301
302Besides the methods inherited from :class:`Widget`: :meth:`Widget.cget`,
303:meth:`Widget.configure`, :meth:`Widget.identify`, :meth:`Widget.instate`
304and :meth:`Widget.state`, and the following inherited from :class:`Entry`:
305:meth:`Entry.bbox`, :meth:`Entry.delete`, :meth:`Entry.icursor`,
Martin Panter96a4f072016-02-10 01:17:51 +0000306:meth:`Entry.index`, :meth:`Entry.insert`, :meth:`Entry.selection`,
Guilherme Polo5f238482009-01-28 14:41:10 +0000307:meth:`Entry.xview`, it has some other methods, described at
308:class:`ttk.Combobox`.
309
310
311Options
312^^^^^^^
313
314This widget accepts the following specific options:
315
Georg Brandl44ea77b2013-03-28 13:28:44 +0100316 .. tabularcolumns:: |l|L|
317
Guilherme Polo5f238482009-01-28 14:41:10 +0000318 +-----------------+--------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100319 | Option | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000320 +=================+========================================================+
321 | exportselection | Boolean value. If set, the widget selection is linked |
322 | | to the Window Manager selection (which can be returned |
323 | | by invoking Misc.selection_get, for example). |
324 +-----------------+--------------------------------------------------------+
325 | justify | Specifies how the text is aligned within the widget. |
326 | | One of "left", "center", or "right". |
327 +-----------------+--------------------------------------------------------+
328 | height | Specifies the height of the pop-down listbox, in rows. |
329 +-----------------+--------------------------------------------------------+
330 | postcommand | A script (possibly registered with Misc.register) that |
331 | | is called immediately before displaying the values. It |
332 | | may specify which values to display. |
333 +-----------------+--------------------------------------------------------+
334 | state | One of "normal", "readonly", or "disabled". In the |
335 | | "readonly" state, the value may not be edited directly,|
336 | | and the user can only selection of the values from the |
337 | | dropdown list. In the "normal" state, the text field is|
338 | | directly editable. In the "disabled" state, no |
339 | | interaction is possible. |
340 +-----------------+--------------------------------------------------------+
341 | textvariable | Specifies a name whose value is linked to the widget |
342 | | value. Whenever the value associated with that name |
343 | | changes, the widget value is updated, and vice versa. |
344 | | See :class:`tkinter.StringVar`. |
345 +-----------------+--------------------------------------------------------+
346 | values | Specifies the list of values to display in the |
347 | | drop-down listbox. |
348 +-----------------+--------------------------------------------------------+
349 | width | Specifies an integer value indicating the desired width|
350 | | of the entry window, in average-size characters of the |
351 | | widget's font. |
352 +-----------------+--------------------------------------------------------+
353
354
355Virtual events
356^^^^^^^^^^^^^^
357
358The combobox widgets generates a **<<ComboboxSelected>>** virtual event
359when the user selects an element from the list of values.
360
361
362ttk.Combobox
363^^^^^^^^^^^^
364
365.. class:: Combobox
366
Georg Brandl7f01a132009-09-16 15:58:14 +0000367 .. method:: current(newindex=None)
Guilherme Polo5f238482009-01-28 14:41:10 +0000368
369 If *newindex* is specified, sets the combobox value to the element
370 position *newindex*. Otherwise, returns the index of the current value or
371 -1 if the current value is not in the values list.
372
373
374 .. method:: get()
375
376 Returns the current value of the combobox.
377
378
379 .. method:: set(value)
380
381 Sets the value of the combobox to *value*.
382
383
Miss Islington (bot)105fcbf2018-02-09 03:40:14 -0800384Spinbox
385-------
386The :class:`ttk.Spinbox` widget is a :class:`ttk.Entry` enhanced with increment
387and decrement arrows. It can be used for numbers or lists of string values.
388This widget is a subclass of :class:`Entry`.
389
390Besides the methods inherited from :class:`Widget`: :meth:`Widget.cget`,
391:meth:`Widget.configure`, :meth:`Widget.identify`, :meth:`Widget.instate`
392and :meth:`Widget.state`, and the following inherited from :class:`Entry`:
393:meth:`Entry.bbox`, :meth:`Entry.delete`, :meth:`Entry.icursor`,
394:meth:`Entry.index`, :meth:`Entry.insert`, :meth:`Entry.xview`,
395it has some other methods, described at :class:`ttk.Spinbox`.
396
397Options
398^^^^^^^
399
400This widget accepts the following specific options:
401
402 .. tabularcolumns:: |l|L|
403
404+----------------------+------------------------------------------------------+
405| Option | Description |
406+======================+======================================================+
407| from | Float value. If set, this is the minimum value to |
408| | which the decrement button will decrement. Must be |
409| | spelled as ``from_`` when used as an argument, since |
410| | ``from`` is a Python keyword. |
411+----------------------+------------------------------------------------------+
412| to | Float value. If set, this is the maximum value to |
413| | which the increment button will increment. |
414+----------------------+------------------------------------------------------+
415| increment | Float value. Specifies the amount which the |
416| | increment/decrement buttons change the |
417| | value. Defaults to 1.0. |
418+----------------------+------------------------------------------------------+
419| values | Sequence of string or float values. If specified, |
420| | the increment/decrement buttons will cycle through |
421| | the items in this sequence rather than incrementing |
422| | or decrementing numbers. |
423| | |
424+----------------------+------------------------------------------------------+
425| wrap | Boolean value. If ``True``, increment and decrement |
426| | buttons will cycle from the ``to`` value to the |
427| | ``from`` value or the ``from`` value to the ``to`` |
428| | value, respectively. |
429+----------------------+------------------------------------------------------+
430| format | String value. This specifies the format of numbers |
431| | set by the increment/decrement buttons. It must be |
432| | in the form "%W.Pf", where W is the padded width of |
433| | the value, P is the precision, and '%' and 'f' are |
434| | literal. |
435+----------------------+------------------------------------------------------+
436| command | Python callable. Will be called with no arguments |
437| | whenever either of the increment or decrement buttons|
438| | are pressed. |
439| | |
440+----------------------+------------------------------------------------------+
441
442
443Virtual events
444^^^^^^^^^^^^^^
445
446The spinbox widget generates an **<<Increment>>** virtual event when the
447user presses <Up>, and a **<<Decrement>>** virtual event when the user
448presses <Down>.
449
450ttk.Spinbox
451^^^^^^^^^^^^
452
453.. class:: Spinbox
454
455 .. method:: get()
456
457 Returns the current value of the spinbox.
458
459
460 .. method:: set(value)
461
462 Sets the value of the spinbox to *value*.
463
464
Guilherme Polo5f238482009-01-28 14:41:10 +0000465Notebook
466--------
467
468Ttk Notebook widget manages a collection of windows and displays a single
469one at a time. Each child window is associated with a tab, which the user
470may select to change the currently-displayed window.
471
472
473Options
474^^^^^^^
475
476This widget accepts the following specific options:
477
Georg Brandl44ea77b2013-03-28 13:28:44 +0100478 .. tabularcolumns:: |l|L|
479
Guilherme Polo5f238482009-01-28 14:41:10 +0000480 +---------+----------------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100481 | Option | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000482 +=========+================================================================+
483 | height | If present and greater than zero, specifies the desired height |
484 | | of the pane area (not including internal padding or tabs). |
485 | | Otherwise, the maximum height of all panes is used. |
486 +---------+----------------------------------------------------------------+
487 | padding | Specifies the amount of extra space to add around the outside |
488 | | of the notebook. The padding is a list up to four length |
489 | | specifications left top right bottom. If fewer than four |
490 | | elements are specified, bottom defaults to top, right defaults |
491 | | to left, and top defaults to left. |
492 +---------+----------------------------------------------------------------+
493 | width | If present and greater than zero, specified the desired width |
494 | | of the pane area (not including internal padding). Otherwise, |
495 | | the maximum width of all panes is used. |
496 +---------+----------------------------------------------------------------+
497
498
499Tab Options
500^^^^^^^^^^^
501
502There are also specific options for tabs:
503
Georg Brandl44ea77b2013-03-28 13:28:44 +0100504 .. tabularcolumns:: |l|L|
505
Guilherme Polo5f238482009-01-28 14:41:10 +0000506 +-----------+--------------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100507 | Option | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000508 +===========+==============================================================+
509 | state | Either "normal", "disabled" or "hidden". If "disabled", then |
510 | | the tab is not selectable. If "hidden", then the tab is not |
511 | | shown. |
512 +-----------+--------------------------------------------------------------+
513 | sticky | Specifies how the child window is positioned within the pane |
514 | | area. Value is a string containing zero or more of the |
515 | | characters "n", "s", "e" or "w". Each letter refers to a |
516 | | side (north, south, east or west) that the child window will |
517 | | stick to, as per the :meth:`grid` geometry manager. |
518 +-----------+--------------------------------------------------------------+
519 | padding | Specifies the amount of extra space to add between the |
520 | | notebook and this pane. Syntax is the same as for the option |
521 | | padding used by this widget. |
522 +-----------+--------------------------------------------------------------+
523 | text | Specifies a text to be displayed in the tab. |
524 +-----------+--------------------------------------------------------------+
525 | image | Specifies an image to display in the tab. See the option |
526 | | image described in :class:`Widget`. |
527 +-----------+--------------------------------------------------------------+
528 | compound | Specifies how to display the image relative to the text, in |
529 | | the case both options text and image are present. See |
530 | | `Label Options`_ for legal values. |
531 +-----------+--------------------------------------------------------------+
532 | underline | Specifies the index (0-based) of a character to underline in |
533 | | the text string. The underlined character is used for |
534 | | mnemonic activation if :meth:`Notebook.enable_traversal` is |
535 | | called. |
536 +-----------+--------------------------------------------------------------+
537
538
539Tab Identifiers
540^^^^^^^^^^^^^^^
541
542The tab_id present in several methods of :class:`ttk.Notebook` may take any
543of the following forms:
544
545* An integer between zero and the number of tabs
546* The name of a child window
547* A positional specification of the form "@x,y", which identifies the tab
548* The literal string "current", which identifies the currently-selected tab
549* The literal string "end", which returns the number of tabs (only valid for
550 :meth:`Notebook.index`)
551
552
553Virtual Events
554^^^^^^^^^^^^^^
555
556This widget generates a **<<NotebookTabChanged>>** virtual event after a new
557tab is selected.
558
559
560ttk.Notebook
561^^^^^^^^^^^^
562
563.. class:: Notebook
564
565 .. method:: add(child, **kw)
566
567 Adds a new tab to the notebook.
568
569 If window is currently managed by the notebook but hidden, it is
570 restored to its previous position.
571
572 See `Tab Options`_ for the list of available options.
573
574
575 .. method:: forget(tab_id)
576
577 Removes the tab specified by *tab_id*, unmaps and unmanages the
578 associated window.
579
580
581 .. method:: hide(tab_id)
582
583 Hides the tab specified by *tab_id*.
584
585 The tab will not be displayed, but the associated window remains
586 managed by the notebook and its configuration remembered. Hidden tabs
Benjamin Petersond23f8222009-04-05 19:13:16 +0000587 may be restored with the :meth:`add` command.
Guilherme Polo5f238482009-01-28 14:41:10 +0000588
589
590 .. method:: identify(x, y)
591
592 Returns the name of the tab element at position *x*, *y*, or the empty
593 string if none.
594
595
596 .. method:: index(tab_id)
597
598 Returns the numeric index of the tab specified by *tab_id*, or the total
599 number of tabs if *tab_id* is the string "end".
600
601
602 .. method:: insert(pos, child, **kw)
603
604 Inserts a pane at the specified position.
605
Benjamin Petersond23f8222009-04-05 19:13:16 +0000606 *pos* is either the string "end", an integer index, or the name of a
Guilherme Polo5f238482009-01-28 14:41:10 +0000607 managed child. If *child* is already managed by the notebook, moves it to
608 the specified position.
609
610 See `Tab Options`_ for the list of available options.
611
612
Georg Brandl7f01a132009-09-16 15:58:14 +0000613 .. method:: select(tab_id=None)
Guilherme Polo5f238482009-01-28 14:41:10 +0000614
615 Selects the specified *tab_id*.
616
617 The associated child window will be displayed, and the
618 previously-selected window (if different) is unmapped. If *tab_id* is
619 omitted, returns the widget name of the currently selected pane.
620
621
Georg Brandl7f01a132009-09-16 15:58:14 +0000622 .. method:: tab(tab_id, option=None, **kw)
Guilherme Polo5f238482009-01-28 14:41:10 +0000623
624 Query or modify the options of the specific *tab_id*.
625
Benjamin Petersond23f8222009-04-05 19:13:16 +0000626 If *kw* is not given, returns a dictionary of the tab option values. If
Guilherme Polo5f238482009-01-28 14:41:10 +0000627 *option* is specified, returns the value of that *option*. Otherwise,
628 sets the options to the corresponding values.
629
630
631 .. method:: tabs()
632
633 Returns a list of windows managed by the notebook.
634
635
636 .. method:: enable_traversal()
637
638 Enable keyboard traversal for a toplevel window containing this notebook.
639
640 This will extend the bindings for the toplevel window containing the
641 notebook as follows:
642
Serhiy Storchaka0424eaf2015-09-12 17:45:25 +0300643 * :kbd:`Control-Tab`: selects the tab following the currently selected one.
644 * :kbd:`Shift-Control-Tab`: selects the tab preceding the currently selected one.
645 * :kbd:`Alt-K`: where *K* is the mnemonic (underlined) character of any tab, will
Guilherme Polo5f238482009-01-28 14:41:10 +0000646 select that tab.
647
648 Multiple notebooks in a single toplevel may be enabled for traversal,
649 including nested notebooks. However, notebook traversal only works
Benjamin Petersond23f8222009-04-05 19:13:16 +0000650 properly if all panes have the notebook they are in as master.
Guilherme Polo5f238482009-01-28 14:41:10 +0000651
652
653Progressbar
654-----------
655
656The :class:`ttk.Progressbar` widget shows the status of a long-running
Raymond Hettinger238018c2009-04-08 08:23:44 +0000657operation. It can operate in two modes: 1) the determinate mode which shows the
658amount completed relative to the total amount of work to be done and 2) the
659indeterminate mode which provides an animated display to let the user know that
660work is progressing.
Guilherme Polo5f238482009-01-28 14:41:10 +0000661
662
663Options
664^^^^^^^
665
666This widget accepts the following specific options:
667
Georg Brandl44ea77b2013-03-28 13:28:44 +0100668 .. tabularcolumns:: |l|L|
669
Guilherme Polo5f238482009-01-28 14:41:10 +0000670 +----------+---------------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100671 | Option | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000672 +==========+===============================================================+
673 | orient | One of "horizontal" or "vertical". Specifies the orientation |
674 | | of the progress bar. |
675 +----------+---------------------------------------------------------------+
676 | length | Specifies the length of the long axis of the progress bar |
677 | | (width if horizontal, height if vertical). |
678 +----------+---------------------------------------------------------------+
679 | mode | One of "determinate" or "indeterminate". |
680 +----------+---------------------------------------------------------------+
681 | maximum | A number specifying the maximum value. Defaults to 100. |
682 +----------+---------------------------------------------------------------+
683 | value | The current value of the progress bar. In "determinate" mode, |
684 | | this represents the amount of work completed. In |
Benjamin Petersond23f8222009-04-05 19:13:16 +0000685 | | "indeterminate" mode, it is interpreted as modulo *maximum*; |
Guilherme Polo5f238482009-01-28 14:41:10 +0000686 | | that is, the progress bar completes one "cycle" when its value|
Benjamin Petersond23f8222009-04-05 19:13:16 +0000687 | | increases by *maximum*. |
Guilherme Polo5f238482009-01-28 14:41:10 +0000688 +----------+---------------------------------------------------------------+
689 | variable | A name which is linked to the option value. If specified, the |
Benjamin Petersond23f8222009-04-05 19:13:16 +0000690 | | value of the progress bar is automatically set to the value of|
Guilherme Polo5f238482009-01-28 14:41:10 +0000691 | | this name whenever the latter is modified. |
692 +----------+---------------------------------------------------------------+
693 | phase | Read-only option. The widget periodically increments the value|
694 | | of this option whenever its value is greater than 0 and, in |
695 | | determinate mode, less than maximum. This option may be used |
696 | | by the current theme to provide additional animation effects. |
697 +----------+---------------------------------------------------------------+
698
699
700ttk.Progressbar
701^^^^^^^^^^^^^^^
702
703.. class:: Progressbar
704
Georg Brandl7f01a132009-09-16 15:58:14 +0000705 .. method:: start(interval=None)
Guilherme Polo5f238482009-01-28 14:41:10 +0000706
Benjamin Petersond23f8222009-04-05 19:13:16 +0000707 Begin autoincrement mode: schedules a recurring timer event that calls
Guilherme Polo5f238482009-01-28 14:41:10 +0000708 :meth:`Progressbar.step` every *interval* milliseconds. If omitted,
709 *interval* defaults to 50 milliseconds.
710
711
Georg Brandl7f01a132009-09-16 15:58:14 +0000712 .. method:: step(amount=None)
Guilherme Polo5f238482009-01-28 14:41:10 +0000713
Benjamin Petersond23f8222009-04-05 19:13:16 +0000714 Increments the progress bar's value by *amount*.
Guilherme Polo5f238482009-01-28 14:41:10 +0000715
716 *amount* defaults to 1.0 if omitted.
717
718
719 .. method:: stop()
720
721 Stop autoincrement mode: cancels any recurring timer event initiated by
Benjamin Petersond23f8222009-04-05 19:13:16 +0000722 :meth:`Progressbar.start` for this progress bar.
Guilherme Polo5f238482009-01-28 14:41:10 +0000723
724
725Separator
726---------
727
728The :class:`ttk.Separator` widget displays a horizontal or vertical separator
729bar.
730
Benjamin Petersond23f8222009-04-05 19:13:16 +0000731It has no other methods besides the ones inherited from :class:`ttk.Widget`.
Guilherme Polo5f238482009-01-28 14:41:10 +0000732
733
734Options
735^^^^^^^
736
737This widget accepts the following specific option:
738
Georg Brandl44ea77b2013-03-28 13:28:44 +0100739 .. tabularcolumns:: |l|L|
740
Guilherme Polo5f238482009-01-28 14:41:10 +0000741 +--------+----------------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100742 | Option | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000743 +========+================================================================+
744 | orient | One of "horizontal" or "vertical". Specifies the orientation of|
745 | | the separator. |
746 +--------+----------------------------------------------------------------+
747
748
749Sizegrip
750--------
751
Benjamin Petersond23f8222009-04-05 19:13:16 +0000752The :class:`ttk.Sizegrip` widget (also known as a grow box) allows the user to
Guilherme Polo5f238482009-01-28 14:41:10 +0000753resize the containing toplevel window by pressing and dragging the grip.
754
Benjamin Petersond23f8222009-04-05 19:13:16 +0000755This widget has neither specific options nor specific methods, besides the
Guilherme Polo5f238482009-01-28 14:41:10 +0000756ones inherited from :class:`ttk.Widget`.
757
758
759Platform-specific notes
760^^^^^^^^^^^^^^^^^^^^^^^
761
Benjamin Petersond23f8222009-04-05 19:13:16 +0000762* On MacOS X, toplevel windows automatically include a built-in size grip
763 by default. Adding a :class:`Sizegrip` is harmless, since the built-in
Guilherme Polo5f238482009-01-28 14:41:10 +0000764 grip will just mask the widget.
765
766
767Bugs
768^^^^
769
770* If the containing toplevel's position was specified relative to the right
Benjamin Petersond23f8222009-04-05 19:13:16 +0000771 or bottom of the screen (e.g. ....), the :class:`Sizegrip` widget will
772 not resize the window.
Guilherme Polo5f238482009-01-28 14:41:10 +0000773* This widget supports only "southeast" resizing.
774
775
776Treeview
777--------
778
779The :class:`ttk.Treeview` widget displays a hierarchical collection of items.
780Each item has a textual label, an optional image, and an optional list of data
781values. The data values are displayed in successive columns after the tree
782label.
783
784The order in which data values are displayed may be controlled by setting
Benjamin Petersond23f8222009-04-05 19:13:16 +0000785the widget option ``displaycolumns``. The tree widget can also display column
Guilherme Polo5f238482009-01-28 14:41:10 +0000786headings. Columns may be accessed by number or symbolic names listed in the
787widget option columns. See `Column Identifiers`_.
788
Martin Panter6245cb32016-04-15 02:14:19 +0000789Each item is identified by a unique name. The widget will generate item IDs
Guilherme Polo5f238482009-01-28 14:41:10 +0000790if they are not supplied by the caller. There is a distinguished root item,
Benjamin Petersond23f8222009-04-05 19:13:16 +0000791named ``{}``. The root item itself is not displayed; its children appear at the
Guilherme Polo5f238482009-01-28 14:41:10 +0000792top level of the hierarchy.
793
Benjamin Petersond23f8222009-04-05 19:13:16 +0000794Each item also has a list of tags, which can be used to associate event bindings
Guilherme Polo5f238482009-01-28 14:41:10 +0000795with individual items and control the appearance of the item.
796
797The Treeview widget supports horizontal and vertical scrolling, according to
798the options described in `Scrollable Widget Options`_ and the methods
799:meth:`Treeview.xview` and :meth:`Treeview.yview`.
800
801
802Options
803^^^^^^^
804
Benjamin Petersond23f8222009-04-05 19:13:16 +0000805This widget accepts the following specific options:
Guilherme Polo5f238482009-01-28 14:41:10 +0000806
Georg Brandl44ea77b2013-03-28 13:28:44 +0100807 .. tabularcolumns:: |l|p{0.7\linewidth}|
Georg Brandl2c860042009-06-16 19:24:38 +0000808
Guilherme Polo5f238482009-01-28 14:41:10 +0000809 +----------------+--------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100810 | Option | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000811 +================+========================================================+
812 | columns | A list of column identifiers, specifying the number of |
813 | | columns and their names. |
814 +----------------+--------------------------------------------------------+
815 | displaycolumns | A list of column identifiers (either symbolic or |
816 | | integer indices) specifying which data columns are |
817 | | displayed and the order in which they appear, or the |
818 | | string "#all". |
819 +----------------+--------------------------------------------------------+
820 | height | Specifies the number of rows which should be visible. |
821 | | Note: the requested width is determined from the sum |
822 | | of the column widths. |
823 +----------------+--------------------------------------------------------+
824 | padding | Specifies the internal padding for the widget. The |
825 | | padding is a list of up to four length specifications. |
826 +----------------+--------------------------------------------------------+
827 | selectmode | Controls how the built-in class bindings manage the |
828 | | selection. One of "extended", "browse" or "none". |
829 | | If set to "extended" (the default), multiple items may |
830 | | be selected. If "browse", only a single item will be |
831 | | selected at a time. If "none", the selection will not |
832 | | be changed. |
833 | | |
834 | | Note that the application code and tag bindings can set|
Benjamin Petersond23f8222009-04-05 19:13:16 +0000835 | | the selection however they wish, regardless of the |
836 | | value of this option. |
Guilherme Polo5f238482009-01-28 14:41:10 +0000837 +----------------+--------------------------------------------------------+
838 | show | A list containing zero or more of the following values,|
839 | | specifying which elements of the tree to display. |
840 | | |
841 | | * tree: display tree labels in column #0. |
842 | | * headings: display the heading row. |
843 | | |
844 | | The default is "tree headings", i.e., show all |
845 | | elements. |
846 | | |
Benjamin Petersond23f8222009-04-05 19:13:16 +0000847 | | **Note**: Column #0 always refers to the tree column, |
Guilherme Polo5f238482009-01-28 14:41:10 +0000848 | | even if show="tree" is not specified. |
849 +----------------+--------------------------------------------------------+
850
851
852Item Options
853^^^^^^^^^^^^
854
855The following item options may be specified for items in the insert and item
856widget commands.
857
Georg Brandl44ea77b2013-03-28 13:28:44 +0100858 .. tabularcolumns:: |l|L|
859
Guilherme Polo5f238482009-01-28 14:41:10 +0000860 +--------+---------------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100861 | Option | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000862 +========+===============================================================+
863 | text | The textual label to display for the item. |
864 +--------+---------------------------------------------------------------+
865 | image | A Tk Image, displayed to the left of the label. |
866 +--------+---------------------------------------------------------------+
867 | values | The list of values associated with the item. |
868 | | |
869 | | Each item should have the same number of values as the widget |
870 | | option columns. If there are fewer values than columns, the |
871 | | remaining values are assumed empty. If there are more values |
872 | | than columns, the extra values are ignored. |
873 +--------+---------------------------------------------------------------+
874 | open | True/False value indicating whether the item's children should|
875 | | be displayed or hidden. |
876 +--------+---------------------------------------------------------------+
877 | tags | A list of tags associated with this item. |
878 +--------+---------------------------------------------------------------+
879
880
881Tag Options
882^^^^^^^^^^^
883
884The following options may be specified on tags:
885
Georg Brandl44ea77b2013-03-28 13:28:44 +0100886 .. tabularcolumns:: |l|L|
887
Guilherme Polo5f238482009-01-28 14:41:10 +0000888 +------------+-----------------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100889 | Option | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000890 +============+===========================================================+
891 | foreground | Specifies the text foreground color. |
892 +------------+-----------------------------------------------------------+
893 | background | Specifies the cell or item background color. |
894 +------------+-----------------------------------------------------------+
895 | font | Specifies the font to use when drawing text. |
896 +------------+-----------------------------------------------------------+
897 | image | Specifies the item image, in case the item's image option |
898 | | is empty. |
899 +------------+-----------------------------------------------------------+
900
901
902Column Identifiers
903^^^^^^^^^^^^^^^^^^
904
905Column identifiers take any of the following forms:
906
907* A symbolic name from the list of columns option.
908* An integer n, specifying the nth data column.
909* A string of the form #n, where n is an integer, specifying the nth display
910 column.
911
912Notes:
913
914* Item's option values may be displayed in a different order than the order
915 in which they are stored.
916* Column #0 always refers to the tree column, even if show="tree" is not
917 specified.
918
919A data column number is an index into an item's option values list; a display
920column number is the column number in the tree where the values are displayed.
921Tree labels are displayed in column #0. If option displaycolumns is not set,
922then data column n is displayed in column #n+1. Again, **column #0 always
923refers to the tree column**.
924
925
926Virtual Events
927^^^^^^^^^^^^^^
928
929The Treeview widget generates the following virtual events.
930
Georg Brandl44ea77b2013-03-28 13:28:44 +0100931 .. tabularcolumns:: |l|L|
932
Guilherme Polo5f238482009-01-28 14:41:10 +0000933 +--------------------+--------------------------------------------------+
Georg Brandl44ea77b2013-03-28 13:28:44 +0100934 | Event | Description |
Guilherme Polo5f238482009-01-28 14:41:10 +0000935 +====================+==================================================+
936 | <<TreeviewSelect>> | Generated whenever the selection changes. |
937 +--------------------+--------------------------------------------------+
938 | <<TreeviewOpen>> | Generated just before settings the focus item to |
939 | | open=True. |
940 +--------------------+--------------------------------------------------+
941 | <<TreeviewClose>> | Generated just after setting the focus item to |
942 | | open=False. |
943 +--------------------+--------------------------------------------------+
944
945The :meth:`Treeview.focus` and :meth:`Treeview.selection` methods can be used
946to determine the affected item or items.
947
948
949ttk.Treeview
950^^^^^^^^^^^^
951
952.. class:: Treeview
953
Georg Brandl7f01a132009-09-16 15:58:14 +0000954 .. method:: bbox(item, column=None)
Guilherme Polo5f238482009-01-28 14:41:10 +0000955
956 Returns the bounding box (relative to the treeview widget's window) of
957 the specified *item* in the form (x, y, width, height).
958
959 If *column* is specified, returns the bounding box of that cell. If the
960 *item* is not visible (i.e., if it is a descendant of a closed item or is
961 scrolled offscreen), returns an empty string.
962
963
Georg Brandl7f01a132009-09-16 15:58:14 +0000964 .. method:: get_children(item=None)
Guilherme Polo5f238482009-01-28 14:41:10 +0000965
966 Returns the list of children belonging to *item*.
967
968 If *item* is not specified, returns root children.
969
970
971 .. method:: set_children(item, *newchildren)
972
Benjamin Petersond23f8222009-04-05 19:13:16 +0000973 Replaces *item*'s child with *newchildren*.
Guilherme Polo5f238482009-01-28 14:41:10 +0000974
Benjamin Petersond23f8222009-04-05 19:13:16 +0000975 Children present in *item* that are not present in *newchildren* are
976 detached from the tree. No items in *newchildren* may be an ancestor of
977 *item*. Note that not specifying *newchildren* results in detaching
Guilherme Polo5f238482009-01-28 14:41:10 +0000978 *item*'s children.
979
980
Georg Brandl7f01a132009-09-16 15:58:14 +0000981 .. method:: column(column, option=None, **kw)
Guilherme Polo5f238482009-01-28 14:41:10 +0000982
983 Query or modify the options for the specified *column*.
984
985 If *kw* is not given, returns a dict of the column option values. If
986 *option* is specified then the value for that *option* is returned.
987 Otherwise, sets the options to the corresponding values.
988
989 The valid options/values are:
990
991 * id
Benjamin Petersond23f8222009-04-05 19:13:16 +0000992 Returns the column name. This is a read-only option.
Guilherme Polo5f238482009-01-28 14:41:10 +0000993 * anchor: One of the standard Tk anchor values.
994 Specifies how the text in this column should be aligned with respect
995 to the cell.
996 * minwidth: width
997 The minimum width of the column in pixels. The treeview widget will
Benjamin Petersond23f8222009-04-05 19:13:16 +0000998 not make the column any smaller than specified by this option when
Guilherme Polo5f238482009-01-28 14:41:10 +0000999 the widget is resized or the user drags a column.
1000 * stretch: True/False
Benjamin Petersond23f8222009-04-05 19:13:16 +00001001 Specifies whether the column's width should be adjusted when
Guilherme Polo5f238482009-01-28 14:41:10 +00001002 the widget is resized.
1003 * width: width
1004 The width of the column in pixels.
1005
1006 To configure the tree column, call this with column = "#0"
1007
1008 .. method:: delete(*items)
1009
1010 Delete all specified *items* and all their descendants.
1011
1012 The root item may not be deleted.
1013
1014
1015 .. method:: detach(*items)
1016
1017 Unlinks all of the specified *items* from the tree.
1018
1019 The items and all of their descendants are still present, and may be
1020 reinserted at another point in the tree, but will not be displayed.
1021
1022 The root item may not be detached.
1023
1024
1025 .. method:: exists(item)
1026
Serhiy Storchakafbc1c262013-11-29 12:17:13 +02001027 Returns ``True`` if the specified *item* is present in the tree.
Guilherme Polo5f238482009-01-28 14:41:10 +00001028
1029
Georg Brandl7f01a132009-09-16 15:58:14 +00001030 .. method:: focus(item=None)
Guilherme Polo5f238482009-01-28 14:41:10 +00001031
1032 If *item* is specified, sets the focus item to *item*. Otherwise, returns
1033 the current focus item, or '' if there is none.
1034
1035
Georg Brandl7f01a132009-09-16 15:58:14 +00001036 .. method:: heading(column, option=None, **kw)
Guilherme Polo5f238482009-01-28 14:41:10 +00001037
1038 Query or modify the heading options for the specified *column*.
1039
1040 If *kw* is not given, returns a dict of the heading option values. If
1041 *option* is specified then the value for that *option* is returned.
1042 Otherwise, sets the options to the corresponding values.
1043
1044 The valid options/values are:
1045
1046 * text: text
1047 The text to display in the column heading.
1048 * image: imageName
1049 Specifies an image to display to the right of the column heading.
1050 * anchor: anchor
1051 Specifies how the heading text should be aligned. One of the standard
1052 Tk anchor values.
1053 * command: callback
1054 A callback to be invoked when the heading label is pressed.
1055
Benjamin Petersond23f8222009-04-05 19:13:16 +00001056 To configure the tree column heading, call this with column = "#0".
Guilherme Polo5f238482009-01-28 14:41:10 +00001057
1058
1059 .. method:: identify(component, x, y)
1060
1061 Returns a description of the specified *component* under the point given
1062 by *x* and *y*, or the empty string if no such *component* is present at
1063 that position.
1064
1065
1066 .. method:: identify_row(y)
1067
1068 Returns the item ID of the item at position *y*.
1069
1070
1071 .. method:: identify_column(x)
1072
1073 Returns the data column identifier of the cell at position *x*.
1074
1075 The tree column has ID #0.
1076
1077
1078 .. method:: identify_region(x, y)
1079
1080 Returns one of:
1081
1082 +-----------+--------------------------------------+
1083 | region | meaning |
1084 +===========+======================================+
1085 | heading | Tree heading area. |
1086 +-----------+--------------------------------------+
1087 | separator | Space between two columns headings. |
1088 +-----------+--------------------------------------+
1089 | tree | The tree area. |
1090 +-----------+--------------------------------------+
1091 | cell | A data cell. |
1092 +-----------+--------------------------------------+
1093
1094 Availability: Tk 8.6.
1095
1096
1097 .. method:: identify_element(x, y)
1098
Benjamin Petersond23f8222009-04-05 19:13:16 +00001099 Returns the element at position *x*, *y*.
Guilherme Polo5f238482009-01-28 14:41:10 +00001100
1101 Availability: Tk 8.6.
1102
1103
1104 .. method:: index(item)
1105
1106 Returns the integer index of *item* within its parent's list of children.
1107
1108
Georg Brandl7f01a132009-09-16 15:58:14 +00001109 .. method:: insert(parent, index, iid=None, **kw)
Guilherme Polo5f238482009-01-28 14:41:10 +00001110
Benjamin Petersond23f8222009-04-05 19:13:16 +00001111 Creates a new item and returns the item identifier of the newly created
Guilherme Polo5f238482009-01-28 14:41:10 +00001112 item.
1113
1114 *parent* is the item ID of the parent item, or the empty string to create
1115 a new top-level item. *index* is an integer, or the value "end",
1116 specifying where in the list of parent's children to insert the new item.
1117 If *index* is less than or equal to zero, the new node is inserted at
Benjamin Petersond23f8222009-04-05 19:13:16 +00001118 the beginning; if *index* is greater than or equal to the current number
Guilherme Polo5f238482009-01-28 14:41:10 +00001119 of children, it is inserted at the end. If *iid* is specified, it is used
Benjamin Petersond23f8222009-04-05 19:13:16 +00001120 as the item identifier; *iid* must not already exist in the tree.
Guilherme Polo5f238482009-01-28 14:41:10 +00001121 Otherwise, a new unique identifier is generated.
1122
1123 See `Item Options`_ for the list of available points.
1124
1125
Georg Brandl7f01a132009-09-16 15:58:14 +00001126 .. method:: item(item, option=None, **kw)
Guilherme Polo5f238482009-01-28 14:41:10 +00001127
1128 Query or modify the options for the specified *item*.
1129
1130 If no options are given, a dict with options/values for the item is
1131 returned.
1132 If *option* is specified then the value for that option is returned.
1133 Otherwise, sets the options to the corresponding values as given by *kw*.
1134
1135
1136 .. method:: move(item, parent, index)
1137
1138 Moves *item* to position *index* in *parent*'s list of children.
1139
Benjamin Petersond23f8222009-04-05 19:13:16 +00001140 It is illegal to move an item under one of its descendants. If *index* is
1141 less than or equal to zero, *item* is moved to the beginning; if greater
1142 than or equal to the number of children, it is moved to the end. If *item*
Guilherme Polo5f238482009-01-28 14:41:10 +00001143 was detached it is reattached.
1144
1145
1146 .. method:: next(item)
1147
1148 Returns the identifier of *item*'s next sibling, or '' if *item* is the
1149 last child of its parent.
1150
1151
1152 .. method:: parent(item)
1153
1154 Returns the ID of the parent of *item*, or '' if *item* is at the top
1155 level of the hierarchy.
1156
1157
1158 .. method:: prev(item)
1159
1160 Returns the identifier of *item*'s previous sibling, or '' if *item* is
1161 the first child of its parent.
1162
1163
1164 .. method:: reattach(item, parent, index)
1165
1166 An alias for :meth:`Treeview.move`.
1167
1168
1169 .. method:: see(item)
1170
1171 Ensure that *item* is visible.
1172
Serhiy Storchakafbc1c262013-11-29 12:17:13 +02001173 Sets all of *item*'s ancestors open option to ``True``, and scrolls the
Guilherme Polo5f238482009-01-28 14:41:10 +00001174 widget if necessary so that *item* is within the visible portion of
1175 the tree.
1176
1177
Georg Brandl7f01a132009-09-16 15:58:14 +00001178 .. method:: selection(selop=None, items=None)
Guilherme Polo5f238482009-01-28 14:41:10 +00001179
1180 If *selop* is not specified, returns selected items. Otherwise, it will
1181 act according to the following selection methods.
1182
Serhiy Storchaka2fad1022017-09-24 14:34:09 +03001183 .. deprecated-removed:: 3.6 3.8
1184 Using ``selection()`` for changing the selection state is deprecated.
1185 Use the following selection methods instead.
Guilherme Polo5f238482009-01-28 14:41:10 +00001186
Serhiy Storchaka2fad1022017-09-24 14:34:09 +03001187
1188 .. method:: selection_set(*items)
Guilherme Polo5f238482009-01-28 14:41:10 +00001189
1190 *items* becomes the new selection.
1191
Serhiy Storchaka2fad1022017-09-24 14:34:09 +03001192 .. versionchanged:: 3.6
1193 *items* can be passed as separate arguments, not just as a single tuple.
Guilherme Polo5f238482009-01-28 14:41:10 +00001194
Serhiy Storchaka2fad1022017-09-24 14:34:09 +03001195
1196 .. method:: selection_add(*items)
Guilherme Polo5f238482009-01-28 14:41:10 +00001197
1198 Add *items* to the selection.
1199
Serhiy Storchaka2fad1022017-09-24 14:34:09 +03001200 .. versionchanged:: 3.6
1201 *items* can be passed as separate arguments, not just as a single tuple.
Guilherme Polo5f238482009-01-28 14:41:10 +00001202
Serhiy Storchaka2fad1022017-09-24 14:34:09 +03001203
1204 .. method:: selection_remove(*items)
Guilherme Polo5f238482009-01-28 14:41:10 +00001205
1206 Remove *items* from the selection.
1207
Serhiy Storchaka2fad1022017-09-24 14:34:09 +03001208 .. versionchanged:: 3.6
1209 *items* can be passed as separate arguments, not just as a single tuple.
Guilherme Polo5f238482009-01-28 14:41:10 +00001210
Serhiy Storchaka2fad1022017-09-24 14:34:09 +03001211
1212 .. method:: selection_toggle(*items)
Guilherme Polo5f238482009-01-28 14:41:10 +00001213
1214 Toggle the selection state of each item in *items*.
1215
Serhiy Storchaka2fad1022017-09-24 14:34:09 +03001216 .. versionchanged:: 3.6
1217 *items* can be passed as separate arguments, not just as a single tuple.
1218
Guilherme Polo5f238482009-01-28 14:41:10 +00001219
Georg Brandl7f01a132009-09-16 15:58:14 +00001220 .. method:: set(item, column=None, value=None)
Guilherme Polo5f238482009-01-28 14:41:10 +00001221
1222 With one argument, returns a dictionary of column/value pairs for the
1223 specified *item*. With two arguments, returns the current value of the
1224 specified *column*. With three arguments, sets the value of given
1225 *column* in given *item* to the specified *value*.
1226
1227
Georg Brandl7f01a132009-09-16 15:58:14 +00001228 .. method:: tag_bind(tagname, sequence=None, callback=None)
Guilherme Polo5f238482009-01-28 14:41:10 +00001229
1230 Bind a callback for the given event *sequence* to the tag *tagname*.
Benjamin Petersond23f8222009-04-05 19:13:16 +00001231 When an event is delivered to an item, the callbacks for each of the
Guilherme Polo5f238482009-01-28 14:41:10 +00001232 item's tags option are called.
1233
1234
Georg Brandl7f01a132009-09-16 15:58:14 +00001235 .. method:: tag_configure(tagname, option=None, **kw)
Guilherme Polo5f238482009-01-28 14:41:10 +00001236
1237 Query or modify the options for the specified *tagname*.
1238
1239 If *kw* is not given, returns a dict of the option settings for
1240 *tagname*. If *option* is specified, returns the value for that *option*
1241 for the specified *tagname*. Otherwise, sets the options to the
1242 corresponding values for the given *tagname*.
1243
1244
Georg Brandl7f01a132009-09-16 15:58:14 +00001245 .. method:: tag_has(tagname, item=None)
Guilherme Polo5f238482009-01-28 14:41:10 +00001246
1247 If *item* is specified, returns 1 or 0 depending on whether the specified
1248 *item* has the given *tagname*. Otherwise, returns a list of all items
Benjamin Petersond23f8222009-04-05 19:13:16 +00001249 that have the specified tag.
Guilherme Polo5f238482009-01-28 14:41:10 +00001250
1251 Availability: Tk 8.6
1252
1253
1254 .. method:: xview(*args)
1255
1256 Query or modify horizontal position of the treeview.
1257
1258
1259 .. method:: yview(*args)
1260
1261 Query or modify vertical position of the treeview.
1262
1263
1264.. _TtkStyling:
1265
1266Ttk Styling
1267-----------
1268
1269Each widget in :mod:`ttk` is assigned a style, which specifies the set of
1270elements making up the widget and how they are arranged, along with dynamic
1271and default settings for element options. By default the style name is the
Senthil Kumaranb4760ef2015-06-14 17:35:37 -07001272same as the widget's class name, but it may be overridden by the widget's style
Guilherme Polo5f238482009-01-28 14:41:10 +00001273option. If you don't know the class name of a widget, use the method
1274:meth:`Misc.winfo_class` (somewidget.winfo_class()).
1275
1276.. seealso::
1277
1278 `Tcl'2004 conference presentation <http://tktable.sourceforge.net/tile/tile-tcl2004.pdf>`_
1279 This document explains how the theme engine works
1280
1281
1282.. class:: Style
1283
1284 This class is used to manipulate the style database.
1285
1286
1287 .. method:: configure(style, query_opt=None, **kw)
1288
Benjamin Petersond23f8222009-04-05 19:13:16 +00001289 Query or set the default value of the specified option(s) in *style*.
Guilherme Polo5f238482009-01-28 14:41:10 +00001290
1291 Each key in *kw* is an option and each value is a string identifying
1292 the value for that option.
1293
1294 For example, to change every default button to be a flat button with
Raymond Hettinger238018c2009-04-08 08:23:44 +00001295 some padding and a different background color::
Guilherme Polo5f238482009-01-28 14:41:10 +00001296
1297 from tkinter import ttk
1298 import tkinter
1299
1300 root = tkinter.Tk()
1301
1302 ttk.Style().configure("TButton", padding=6, relief="flat",
1303 background="#ccc")
1304
1305 btn = ttk.Button(text="Sample")
1306 btn.pack()
1307
1308 root.mainloop()
1309
1310
1311 .. method:: map(style, query_opt=None, **kw)
1312
1313 Query or sets dynamic values of the specified option(s) in *style*.
1314
Benjamin Petersond23f8222009-04-05 19:13:16 +00001315 Each key in *kw* is an option and each value should be a list or a
1316 tuple (usually) containing statespecs grouped in tuples, lists, or
Raymond Hettinger238018c2009-04-08 08:23:44 +00001317 some other preference. A statespec is a compound of one
Benjamin Petersond23f8222009-04-05 19:13:16 +00001318 or more states and then a value.
Guilherme Polo5f238482009-01-28 14:41:10 +00001319
1320 An example may make it more understandable::
1321
1322 import tkinter
1323 from tkinter import ttk
1324
1325 root = tkinter.Tk()
1326
1327 style = ttk.Style()
1328 style.map("C.TButton",
1329 foreground=[('pressed', 'red'), ('active', 'blue')],
1330 background=[('pressed', '!disabled', 'black'), ('active', 'white')]
1331 )
1332
1333 colored_btn = ttk.Button(text="Test", style="C.TButton").pack()
1334
1335 root.mainloop()
1336
1337
Benjamin Petersond23f8222009-04-05 19:13:16 +00001338 Note that the order of the (states, value) sequences for an option does
Raymond Hettinger238018c2009-04-08 08:23:44 +00001339 matter, if the order is changed to ``[('active', 'blue'), ('pressed',
1340 'red')]`` in the foreground option, for example, the result would be a
1341 blue foreground when the widget were in active or pressed states.
Guilherme Polo5f238482009-01-28 14:41:10 +00001342
1343
Georg Brandl7f01a132009-09-16 15:58:14 +00001344 .. method:: lookup(style, option, state=None, default=None)
Guilherme Polo5f238482009-01-28 14:41:10 +00001345
1346 Returns the value specified for *option* in *style*.
1347
1348 If *state* is specified, it is expected to be a sequence of one or more
1349 states. If the *default* argument is set, it is used as a fallback value
1350 in case no specification for option is found.
1351
Raymond Hettinger238018c2009-04-08 08:23:44 +00001352 To check what font a Button uses by default::
Guilherme Polo5f238482009-01-28 14:41:10 +00001353
1354 from tkinter import ttk
1355
Ezio Melotti985e24d2009-09-13 07:54:02 +00001356 print(ttk.Style().lookup("TButton", "font"))
Guilherme Polo5f238482009-01-28 14:41:10 +00001357
1358
Georg Brandl7f01a132009-09-16 15:58:14 +00001359 .. method:: layout(style, layoutspec=None)
Guilherme Polo5f238482009-01-28 14:41:10 +00001360
1361 Define the widget layout for given *style*. If *layoutspec* is omitted,
1362 return the layout specification for given style.
1363
Benjamin Petersond23f8222009-04-05 19:13:16 +00001364 *layoutspec*, if specified, is expected to be a list or some other
1365 sequence type (excluding strings), where each item should be a tuple and
Guilherme Polo5f238482009-01-28 14:41:10 +00001366 the first item is the layout name and the second item should have the
Ezio Melottie130a522011-10-19 10:58:56 +03001367 format described in `Layouts`_.
Guilherme Polo5f238482009-01-28 14:41:10 +00001368
Benjamin Petersond23f8222009-04-05 19:13:16 +00001369 To understand the format, see the following example (it is not
1370 intended to do anything useful)::
Guilherme Polo5f238482009-01-28 14:41:10 +00001371
1372 from tkinter import ttk
1373 import tkinter
1374
1375 root = tkinter.Tk()
1376
1377 style = ttk.Style()
1378 style.layout("TMenubutton", [
1379 ("Menubutton.background", None),
1380 ("Menubutton.button", {"children":
1381 [("Menubutton.focus", {"children":
1382 [("Menubutton.padding", {"children":
1383 [("Menubutton.label", {"side": "left", "expand": 1})]
1384 })]
1385 })]
1386 }),
1387 ])
1388
1389 mbtn = ttk.Menubutton(text='Text')
1390 mbtn.pack()
1391 root.mainloop()
1392
1393
1394 .. method:: element_create(elementname, etype, *args, **kw)
1395
Benjamin Petersond23f8222009-04-05 19:13:16 +00001396 Create a new element in the current theme, of the given *etype* which is
Guilherme Polo5f238482009-01-28 14:41:10 +00001397 expected to be either "image", "from" or "vsapi". The latter is only
1398 available in Tk 8.6a for Windows XP and Vista and is not described here.
1399
1400 If "image" is used, *args* should contain the default image name followed
Benjamin Petersond23f8222009-04-05 19:13:16 +00001401 by statespec/value pairs (this is the imagespec), and *kw* may have the
Guilherme Polo5f238482009-01-28 14:41:10 +00001402 following options:
1403
1404 * border=padding
1405 padding is a list of up to four integers, specifying the left, top,
1406 right, and bottom borders, respectively.
1407
1408 * height=height
1409 Specifies a minimum height for the element. If less than zero, the
1410 base image's height is used as a default.
1411
1412 * padding=padding
1413 Specifies the element's interior padding. Defaults to border's value
1414 if not specified.
1415
1416 * sticky=spec
1417 Specifies how the image is placed within the final parcel. spec
Sandro Tosi2b373b02011-12-25 17:07:22 +01001418 contains zero or more characters "n", "s", "w", or "e".
Guilherme Polo5f238482009-01-28 14:41:10 +00001419
1420 * width=width
1421 Specifies a minimum width for the element. If less than zero, the
1422 base image's width is used as a default.
1423
Benjamin Petersond23f8222009-04-05 19:13:16 +00001424 If "from" is used as the value of *etype*,
1425 :meth:`element_create` will clone an existing
1426 element. *args* is expected to contain a themename, from which
Guilherme Polo5f238482009-01-28 14:41:10 +00001427 the element will be cloned, and optionally an element to clone from.
1428 If this element to clone from is not specified, an empty element will
Benjamin Petersond23f8222009-04-05 19:13:16 +00001429 be used. *kw* is discarded.
Guilherme Polo5f238482009-01-28 14:41:10 +00001430
1431
1432 .. method:: element_names()
1433
1434 Returns the list of elements defined in the current theme.
1435
1436
1437 .. method:: element_options(elementname)
1438
1439 Returns the list of *elementname*'s options.
1440
1441
Georg Brandl7f01a132009-09-16 15:58:14 +00001442 .. method:: theme_create(themename, parent=None, settings=None)
Guilherme Polo5f238482009-01-28 14:41:10 +00001443
1444 Create a new theme.
1445
1446 It is an error if *themename* already exists. If *parent* is specified,
1447 the new theme will inherit styles, elements and layouts from the parent
1448 theme. If *settings* are present they are expected to have the same
1449 syntax used for :meth:`theme_settings`.
1450
1451
1452 .. method:: theme_settings(themename, settings)
1453
1454 Temporarily sets the current theme to *themename*, apply specified
1455 *settings* and then restore the previous theme.
1456
1457 Each key in *settings* is a style and each value may contain the keys
1458 'configure', 'map', 'layout' and 'element create' and they are expected
1459 to have the same format as specified by the methods
1460 :meth:`Style.configure`, :meth:`Style.map`, :meth:`Style.layout` and
1461 :meth:`Style.element_create` respectively.
1462
Benjamin Petersond23f8222009-04-05 19:13:16 +00001463 As an example, let's change the Combobox for the default theme a bit::
Guilherme Polo5f238482009-01-28 14:41:10 +00001464
1465 from tkinter import ttk
1466 import tkinter
1467
1468 root = tkinter.Tk()
1469
1470 style = ttk.Style()
1471 style.theme_settings("default", {
1472 "TCombobox": {
1473 "configure": {"padding": 5},
1474 "map": {
1475 "background": [("active", "green2"),
1476 ("!disabled", "green4")],
1477 "fieldbackground": [("!disabled", "green3")],
1478 "foreground": [("focus", "OliveDrab1"),
1479 ("!disabled", "OliveDrab2")]
1480 }
1481 }
1482 })
1483
1484 combo = ttk.Combobox().pack()
1485
1486 root.mainloop()
1487
1488
1489 .. method:: theme_names()
1490
1491 Returns a list of all known themes.
1492
1493
Georg Brandl7f01a132009-09-16 15:58:14 +00001494 .. method:: theme_use(themename=None)
Guilherme Polo5f238482009-01-28 14:41:10 +00001495
Benjamin Petersond23f8222009-04-05 19:13:16 +00001496 If *themename* is not given, returns the theme in use. Otherwise, sets
Guilherme Polo5f238482009-01-28 14:41:10 +00001497 the current theme to *themename*, refreshes all widgets and emits a
1498 <<ThemeChanged>> event.
1499
1500
1501Layouts
1502^^^^^^^
1503
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03001504A layout can be just ``None``, if it takes no options, or a dict of
Benjamin Petersond23f8222009-04-05 19:13:16 +00001505options specifying how to arrange the element. The layout mechanism
1506uses a simplified version of the pack geometry manager: given an
1507initial cavity, each element is allocated a parcel. Valid
1508options/values are:
Guilherme Polo5f238482009-01-28 14:41:10 +00001509
1510 * side: whichside
Benjamin Petersond23f8222009-04-05 19:13:16 +00001511 Specifies which side of the cavity to place the element; one of
Guilherme Polo5f238482009-01-28 14:41:10 +00001512 top, right, bottom or left. If omitted, the element occupies the
1513 entire cavity.
1514
1515 * sticky: nswe
1516 Specifies where the element is placed inside its allocated parcel.
1517
1518 * unit: 0 or 1
1519 If set to 1, causes the element and all of its descendants to be treated as
1520 a single element for the purposes of :meth:`Widget.identify` et al. It's
1521 used for things like scrollbar thumbs with grips.
1522
1523 * children: [sublayout... ]
1524 Specifies a list of elements to place inside the element. Each
1525 element is a tuple (or other sequence type) where the first item is
1526 the layout name, and the other is a `Layout`_.
1527
1528.. _Layout: `Layouts`_