blob: 36a4666bb26c86fe456e170305f55b7aa2a25b88 [file] [log] [blame]
Guilherme Polocda93aa2009-01-28 13:09:03 +00001:mod:`ttk` --- Tk themed widgets
2================================
3
4.. module:: ttk
5 :synopsis: Tk themed widget set
6.. sectionauthor:: Guilherme Polo <ggpolo@gmail.com>
7
8
9.. index:: single: ttk
10
11The :mod:`ttk` module provides access to the Tk themed widget set, which
12has been introduced in Tk 8.5. If you do not have Python compiled against
13Tk 8.5 you may still use this module as long as you have Tile installed, but
14then you will miss some features provided by the new Tk, like anti-aliased font
15rendering under X11, window transparency (on X11 you will need a composition
16window manager) and others.
17
18The basic idea of :mod:`ttk` is to separate, to the extent possible, the code
19implementing a widget's behavior from the code implementing its appearance.
20
21
22.. seealso::
23
24 `Tk Widget Styling Support <http://www.tcl.tk/cgi-bin/tct/tip/48>`_
25 The document which brought up theming support for Tk
26
27
28Using Ttk
29---------
30
31Basically, to start using Ttk, you have to import its module::
32
33 import ttk
34
35But if you already have some code that does::
36
37 from Tkinter import *
38
39You may optionally want to use::
40
41 from Tkinter import *
42 from ttk import *
43
44And 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`
47and :class:`Scrollbar`) will automatically substitute the Tk widgets.
48
49This has the direct benefit of using the new widgets which gives better
50look & feel across platforms, but you should be aware that they are not
51totally compatible. The main difference you will find out is that widget
52options such as "fg", "bg" and others related to widget styling are no
53longer present in Ttk widgets, instead you will have to use :class:`ttk.Style`
54to achieve the same (or better) styling.
55
56.. seealso::
57
58 `Converting existing applications to use the Tile widgets <http://tktable.sourceforge.net/tile/doc/converting.txt>`_
59 A text which talks in Tcl terms about differences typically found when
60 moving applications to use the new widgets.
61
62
63Ttk Widgets
64-----------
65
66Ttk comes with 17 widgets, where 11 of these already existed in Tkinter:
67:class:`Button`, :class:`Checkbutton`, :class:`Entry`, :class:`Frame`,
68:class:`Label`, :class:`LabelFrame`, :class:`Menubutton`, :class:`PanedWindow`,
69:class:`Radiobutton`, :class:`Scale` and :class:`Scrollbar`. The others 6 are
70new: :class:`Combobox`, :class:`Notebook`, :class:`Progressbar`,
71:class:`Separator`, :class:`Sizegrip` and :class:`Treeview`. And all them are
72subclasses of :class:`Widget`.
73
74Like it was told before, you will notice changes in look & feel as well in the
75styling code. To demonstrate the latter, a very simple example is shown below.
76
77Tk code::
78
79 l1 = Tkinter.Label(text="Test", fg="black", bg="white")
80 l2 = Tkinter.Label(text="Test", fg="black", bg="white")
81
82
83Ttk code::
84
85 style = ttk.Style()
86 style.configure("BW.TLabel", foreground="black", background="white")
87
88 l1 = ttk.Label(text="Test", style="BW.TLabel")
89 l2 = ttk.Label(text="Test", style="BW.TLabel")
90
91For more information about TtkStyling_ read the :class:`Style` class
92documentation.
93
94Widget
95------
96
97:class:`ttk.Widget` defines standard options and methods supported by Tk
98themed widgets and is not supposed to be directly instantiated.
99
100
101Standard Options
102^^^^^^^^^^^^^^^^
103
104All the :mod:`ttk` Widgets accepts the following options:
105
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 | | which may only be specified when the window is created |
114 +-----------+--------------------------------------------------------------+
115 | cursor | Specifies the mouse cursor to be used for the widget. If set |
116 | | to the empty string (the default), the cursor is inherited |
117 | | for the parent widget. |
118 +-----------+--------------------------------------------------------------+
119 | takefocus | Determines whether the window accepts the focus during |
120 | | keyboard traversal. 0, 1 or an empty is return. If 0 is |
121 | | returned, it means that the window should be skipped entirely|
122 | | during keyboard traversal. If 1, it means that the window |
123 | | should receive the input focus as long as it is viewable. And|
124 | | an empty string means that the traversal scripts make the |
125 | | decision about whether or not to focus on the window. |
126 +-----------+--------------------------------------------------------------+
127 | style | May be used to specify a custom widget style. |
128 +-----------+--------------------------------------------------------------+
129
130
131Scrollable Widget Options
132^^^^^^^^^^^^^^^^^^^^^^^^^
133
134The following options are supported by widgets that are controlled by a
135scrollbar.
136
137 +----------------+---------------------------------------------------------+
138 | option | description |
139 +================+=========================================================+
140 | xscrollcommand | Used to comunicate with horizontal scrollbars. |
141 | | |
142 | | When the view in the widget's window change, the widget |
143 | | will generate a Tcl command based on the scrollcommand. |
144 | | |
145 | | Usually this option consists of the method |
146 | | :meth:`Scrollbar.set` of some scrollbar. This will cause|
147 | | the scrollbar to be updated whenever the view in the |
148 | | window changes. |
149 +----------------+---------------------------------------------------------+
150 | yscrollcommand | Used to comunicate with vertical scrollbars. |
151 | | For some more information, see above. |
152 +----------------+---------------------------------------------------------+
153
154
155Label Options
156^^^^^^^^^^^^^
157
158The following options are supported by labels, buttons and other button-like
159widgets.
160
161 +--------------+-----------------------------------------------------------+
162 | option | description |
163 +==============+===========================================================+
164 | text | Specifies a text string to be displayed inside the widget.|
165 +--------------+-----------------------------------------------------------+
166 | textvariable | Specifies a name whose value will be used in place of the |
167 | | text option resource. |
168 +--------------+-----------------------------------------------------------+
169 | underline | If set, specifies the index (0-based) of a character to |
170 | | underline in the text string. The underline character is |
171 | | used for mnemonic activation. |
172 +--------------+-----------------------------------------------------------+
173 | image | Specifies an image to display. This is a list of 1 or more|
174 | | elements. The first element is the default image name. The|
175 | | rest of the list if a sequence of statespec/value pairs as|
176 | | defined by :meth:`Style.map`, specifying different images |
177 | | to use when the widget is in a particular state or a |
178 | | combination of states. All images in the list should have |
179 | | the same size. |
180 +--------------+-----------------------------------------------------------+
181 | compound | Specifies how to display the image relative to the text, |
182 | | in the case both text and images options are present. |
183 | | Valid values are: |
184 | | |
185 | | * text: display text only |
186 | | * image: display image only |
187 | | * top, bottom, left, right: display image above, below, |
188 | | left of, or right of the text, respectively. |
189 | | * none: the default. display the image if present, |
190 | | otherwise the text. |
191 +--------------+-----------------------------------------------------------+
192 | width | If greater than zero, specifies how much space, in |
193 | | character widths, to allocate for the text label, if less |
194 | | than zero, specifies a minimum width. If zero or |
195 | | unspecified, the natural width of the text label is used. |
196 +--------------+-----------------------------------------------------------+
197
198
199Compatibility Options
200^^^^^^^^^^^^^^^^^^^^^
201
202 +--------+----------------------------------------------------------------+
203 | option | description |
204 +========+================================================================+
205 | state | May be set to "normal" or "disabled" to control the "disabled" |
206 | | state bit. This is a write-only option: setting it changes the |
207 | | widget state, but the :meth:`Widget.state` method does not |
208 | | affect this option. |
209 +--------+----------------------------------------------------------------+
210
211Widget States
212^^^^^^^^^^^^^
213
214The widget state is a bitmap of independent state flags.
215
216 +------------+-------------------------------------------------------------+
217 | flag | description |
218 +============+=============================================================+
219 | active | The mouse cursor is over the widget and pressing a mouse |
220 | | button will cause some action to occur |
221 +------------+-------------------------------------------------------------+
222 | disabled | Widget is disabled under program control |
223 +------------+-------------------------------------------------------------+
224 | focus | Widget has keyboard focus |
225 +------------+-------------------------------------------------------------+
226 | pressed | Widget is being pressed |
227 +------------+-------------------------------------------------------------+
228 | selected | "On", "true", or "current" for things like Checkbuttons and |
229 | | radiobuttons |
230 +------------+-------------------------------------------------------------+
231 | background | Windows and Mac have a notion of an "active" or foreground |
232 | | window. The *background* state is set for widgets in a |
233 | | background window, and cleared for those in the foreground |
234 | | window |
235 +------------+-------------------------------------------------------------+
236 | readonly | Widget should not allow user modification |
237 +------------+-------------------------------------------------------------+
238 | alternate | A widget-specific alternate display format |
239 +------------+-------------------------------------------------------------+
240 | invalid | The widget's value is invalid |
241 +------------+-------------------------------------------------------------+
242
243A state specification is a sequence of state names, optionally prefixed with
244an exclamation point indicating that the bit is off.
245
246
247ttk.Widget
248^^^^^^^^^^
249
250Besides the methods described below, the class :class:`ttk.Widget` supports the
251methods :meth:`Tkinter.Widget.cget` and :meth:`Tkinter.Widget.configure`.
252
253.. class:: Widget
254
255 .. method:: identify(x, y)
256
257 Returns the name of the element at position *x* *y*, or the empty string
258 if the point does not lie within any element.
259
260 *x* and *y* are pixel coordinates relative to the widget.
261
262
263 .. method:: instate(statespec[, callback=None[, *args[, **kw]]])
264
265 Test the widget's state. If a callback is not specified, returns True
266 if the widget state matches *statespec* and False otherwise. If callback
267 is specified then it is called with args if widget state matches
268 *statespec*.
269
270
271 .. method:: state([statespec=None])
272
273 Modify or inquire widget state. If *statespec* is specified, sets the
274 widget state according to it and return a new *statespec* indicating
275 which flags were changed. If *statespec* is not specified, returns
276 the currently-enabled state flags.
277
278 *statespec* will usually be a list or a tuple.
279
280
281Combobox
282--------
283
284The :class:`ttk.Combobox` widget combines a text field with a pop-down list of
285values. This widget is a subclass of :class:`Entry`.
286
287Besides the methods inherited from :class:`Widget`: :meth:`Widget.cget`,
288:meth:`Widget.configure`, :meth:`Widget.identify`, :meth:`Widget.instate`
289and :meth:`Widget.state`, and the following inherited from :class:`Entry`:
290:meth:`Entry.bbox`, :meth:`Entry.delete`, :meth:`Entry.icursor`,
291:meth:`Entry.index`, :meth:`Entry.inset`, :meth:`Entry.selection`,
292:meth:`Entry.xview`, it has some other methods, described at
293:class:`ttk.Combobox`.
294
295
296Options
297^^^^^^^
298
299This widget accepts the following specific options:
300
301 +-----------------+--------------------------------------------------------+
302 | option | description |
303 +=================+========================================================+
304 | exportselection | Boolean value. If set, the widget selection is linked |
305 | | to the Window Manager selection (which can be returned |
306 | | by invoking Misc.selection_get, for example). |
307 +-----------------+--------------------------------------------------------+
308 | justify | Specifies how the text is aligned within the widget. |
309 | | One of "left", "center", or "right". |
310 +-----------------+--------------------------------------------------------+
311 | height | Specifies the height of the pop-down listbox, in rows. |
312 +-----------------+--------------------------------------------------------+
313 | postcommand | A script (possibly registered with Misc.register) that |
314 | | is called immediately before displaying the values. It |
315 | | may specify which values to display. |
316 +-----------------+--------------------------------------------------------+
317 | state | One of "normal", "readonly", or "disabled". In the |
318 | | "readonly" state, the value may not be edited directly,|
319 | | and the user can only selection of the values from the |
320 | | dropdown list. In the "normal" state, the text field is|
321 | | directly editable. In the "disabled" state, no |
322 | | interaction is possible. |
323 +-----------------+--------------------------------------------------------+
324 | textvariable | Specifies a name whose value is linked to the widget |
325 | | value. Whenever the value associated with that name |
326 | | changes, the widget value is updated, and vice versa. |
327 | | See :class:`Tkinter.StringVar`. |
328 +-----------------+--------------------------------------------------------+
329 | values | Specifies the list of values to display in the |
330 | | drop-down listbox. |
331 +-----------------+--------------------------------------------------------+
332 | width | Specifies an integer value indicating the desired width|
333 | | of the entry window, in average-size characters of the |
334 | | widget's font. |
335 +-----------------+--------------------------------------------------------+
336
337
338Virtual events
339^^^^^^^^^^^^^^
340
341The combobox widgets generates a **<<ComboboxSelected>>** virtual event
342when the user selects an element from the list of values.
343
344
345ttk.Combobox
346^^^^^^^^^^^^
347
348.. class:: Combobox
349
350 .. method:: current([newindex=None])
351
352 If *newindex* is specified, sets the combobox value to the element
353 position *newindex*. Otherwise, returns the index of the current value or
354 -1 if the current value is not in the values list.
355
356
357 .. method:: get()
358
359 Returns the current value of the combobox.
360
361
362 .. method:: set(value)
363
364 Sets the value of the combobox to *value*.
365
366
367Notebook
368--------
369
370Ttk Notebook widget manages a collection of windows and displays a single
371one at a time. Each child window is associated with a tab, which the user
372may select to change the currently-displayed window.
373
374
375Options
376^^^^^^^
377
378This widget accepts the following specific options:
379
380 +---------+----------------------------------------------------------------+
381 | option | description |
382 +=========+================================================================+
383 | height | If present and greater than zero, specifies the desired height |
384 | | of the pane area (not including internal padding or tabs). |
385 | | Otherwise, the maximum height of all panes is used. |
386 +---------+----------------------------------------------------------------+
387 | padding | Specifies the amount of extra space to add around the outside |
388 | | of the notebook. The padding is a list up to four length |
389 | | specifications left top right bottom. If fewer than four |
390 | | elements are specified, bottom defaults to top, right defaults |
391 | | to left, and top defaults to left. |
392 +---------+----------------------------------------------------------------+
393 | width | If present and greater than zero, specified the desired width |
394 | | of the pane area (not including internal padding). Otherwise, |
395 | | the maximum width of all panes is used. |
396 +---------+----------------------------------------------------------------+
397
398
399Tab Options
400^^^^^^^^^^^
401
402There are also specific options for tabs:
403
404 +-----------+--------------------------------------------------------------+
405 | option | description |
406 +===========+==============================================================+
407 | state | Either "normal", "disabled" or "hidden". If "disabled", then |
408 | | the tab is not selectable. If "hidden", then the tab is not |
409 | | shown. |
410 +-----------+--------------------------------------------------------------+
411 | sticky | Specifies how the child window is positioned within the pane |
412 | | area. Value is a string containing zero or more of the |
413 | | characters "n", "s", "e" or "w". Each letter refers to a |
414 | | side (north, south, east or west) that the child window will |
415 | | stick to, as per the :meth:`grid` geometry manager. |
416 +-----------+--------------------------------------------------------------+
417 | padding | Specifies the amount of extra space to add between the |
418 | | notebook and this pane. Syntax is the same as for the option |
419 | | padding used by this widget. |
420 +-----------+--------------------------------------------------------------+
421 | text | Specifies a text to be displayed in the tab. |
422 +-----------+--------------------------------------------------------------+
423 | image | Specifies an image to display in the tab. See the option |
424 | | image described in :class:`Widget`. |
425 +-----------+--------------------------------------------------------------+
426 | compound | Specifies how to display the image relative to the text, in |
427 | | the case both options text and image are present. See |
428 | | `Label Options`_ for legal values. |
429 +-----------+--------------------------------------------------------------+
430 | underline | Specifies the index (0-based) of a character to underline in |
431 | | the text string. The underlined character is used for |
432 | | mnemonic activation if :meth:`Notebook.enable_traversal` is |
433 | | called. |
434 +-----------+--------------------------------------------------------------+
435
436
437Tab Identifiers
438^^^^^^^^^^^^^^^
439
440The tab_id present in several methods of :class:`ttk.Notebook` may take any
441of the following forms:
442
443* An integer between zero and the number of tabs
444* The name of a child window
445* A positional specification of the form "@x,y", which identifies the tab
446* The literal string "current", which identifies the currently-selected tab
447* The literal string "end", which returns the number of tabs (only valid for
448 :meth:`Notebook.index`)
449
450
451Virtual Events
452^^^^^^^^^^^^^^
453
454This widget generates a **<<NotebookTabChanged>>** virtual event after a new
455tab is selected.
456
457
458ttk.Notebook
459^^^^^^^^^^^^
460
461.. class:: Notebook
462
463 .. method:: add(child, **kw)
464
465 Adds a new tab to the notebook.
466
467 If window is currently managed by the notebook but hidden, it is
468 restored to its previous position.
469
470 See `Tab Options`_ for the list of available options.
471
472
473 .. method:: forget(tab_id)
474
475 Removes the tab specified by *tab_id*, unmaps and unmanages the
476 associated window.
477
478
479 .. method:: hide(tab_id)
480
481 Hides the tab specified by *tab_id*.
482
483 The tab will not be displayed, but the associated window remains
484 managed by the notebook and its configuration remembered. Hidden tabs
485 may be restored with the add command.
486
487
488 .. method:: identify(x, y)
489
490 Returns the name of the tab element at position *x*, *y*, or the empty
491 string if none.
492
493
494 .. method:: index(tab_id)
495
496 Returns the numeric index of the tab specified by *tab_id*, or the total
497 number of tabs if *tab_id* is the string "end".
498
499
500 .. method:: insert(pos, child, **kw)
501
502 Inserts a pane at the specified position.
503
504 *pos* is either the string end, an integer index, or the name of a
505 managed child. If *child* is already managed by the notebook, moves it to
506 the specified position.
507
508 See `Tab Options`_ for the list of available options.
509
510
511 .. method:: select([tab_id])
512
513 Selects the specified *tab_id*.
514
515 The associated child window will be displayed, and the
516 previously-selected window (if different) is unmapped. If *tab_id* is
517 omitted, returns the widget name of the currently selected pane.
518
519
520 .. method:: tab(tab_id[, option=None[, **kw]])
521
522 Query or modify the options of the specific *tab_id*.
523
524 If *kw* is not given, returns a dict of the tab option values. If
525 *option* is specified, returns the value of that *option*. Otherwise,
526 sets the options to the corresponding values.
527
528
529 .. method:: tabs()
530
531 Returns a list of windows managed by the notebook.
532
533
534 .. method:: enable_traversal()
535
536 Enable keyboard traversal for a toplevel window containing this notebook.
537
538 This will extend the bindings for the toplevel window containing the
539 notebook as follows:
540
541 * Control-Tab: selects the tab following the currently selected one
542 * Shift-Control-Tab: selects the tab preceding the currently selected one
543 * Alt-K: where K is the mnemonic (underlined) character of any tab, will
544 select that tab.
545
546 Multiple notebooks in a single toplevel may be enabled for traversal,
547 including nested notebooks. However, notebook traversal only works
548 properly if all panes have as master the notebook they are in.
549
550
551Progressbar
552-----------
553
554The :class:`ttk.Progressbar` widget shows the status of a long-running
555operation. It can operate in two modes: determinate mode shows the amount
556completed relative to the total amount of work to be done, and indeterminate
557mode provides an animated display to let the user know that something is
558happening.
559
560
561Options
562^^^^^^^
563
564This widget accepts the following specific options:
565
566 +----------+---------------------------------------------------------------+
567 | option | description |
568 +==========+===============================================================+
569 | orient | One of "horizontal" or "vertical". Specifies the orientation |
570 | | of the progress bar. |
571 +----------+---------------------------------------------------------------+
572 | length | Specifies the length of the long axis of the progress bar |
573 | | (width if horizontal, height if vertical). |
574 +----------+---------------------------------------------------------------+
575 | mode | One of "determinate" or "indeterminate". |
576 +----------+---------------------------------------------------------------+
577 | maximum | A number specifying the maximum value. Defaults to 100. |
578 +----------+---------------------------------------------------------------+
579 | value | The current value of the progress bar. In "determinate" mode, |
580 | | this represents the amount of work completed. In |
581 | | "indeterminate" mode, it is interpreted as modulo maximum; |
582 | | that is, the progress bar completes one "cycle" when its value|
583 | | increases by maximum. |
584 +----------+---------------------------------------------------------------+
585 | variable | A name which is linked to the option value. If specified, the |
586 | | value of the progressbar is automatically set to the value of |
587 | | this name whenever the latter is modified. |
588 +----------+---------------------------------------------------------------+
589 | phase | Read-only option. The widget periodically increments the value|
590 | | of this option whenever its value is greater than 0 and, in |
591 | | determinate mode, less than maximum. This option may be used |
592 | | by the current theme to provide additional animation effects. |
593 +----------+---------------------------------------------------------------+
594
595
596ttk.Progressbar
597^^^^^^^^^^^^^^^
598
599.. class:: Progressbar
600
601 .. method:: start([interval])
602
603 Begin autoincrement mode: schedules a recurring timer even that calls
604 :meth:`Progressbar.step` every *interval* milliseconds. If omitted,
605 *interval* defaults to 50 milliseconds.
606
607
608 .. method:: step([amount])
609
610 Increments progressbar's value by *amount*.
611
612 *amount* defaults to 1.0 if omitted.
613
614
615 .. method:: stop()
616
617 Stop autoincrement mode: cancels any recurring timer event initiated by
618 :meth:`Progressbar.start` for this progressbar.
619
620
621Separator
622---------
623
624The :class:`ttk.Separator` widget displays a horizontal or vertical separator
625bar.
626
627It has no other method besides the ones inherited from :class:`ttk.Widget`.
628
629
630Options
631^^^^^^^
632
633This widget accepts the following specific option:
634
635 +--------+----------------------------------------------------------------+
636 | option | description |
637 +========+================================================================+
638 | orient | One of "horizontal" or "vertical". Specifies the orientation of|
639 | | the separator. |
640 +--------+----------------------------------------------------------------+
641
642
643Sizegrip
644--------
645
646The :class:`ttk.Sizegrip` widget (also known as grow box) allows the user to
647resize the containing toplevel window by pressing and dragging the grip.
648
649This widget has no specific options neither specific methods, besides the
650ones inherited from :class:`ttk.Widget`.
651
652
653Platform-specific notes
654^^^^^^^^^^^^^^^^^^^^^^^
655
656* On Mac OSX, toplevel windows automatically include a built-in size grip
657 by default. Adding a Sizegrip there is harmless, since the built-in
658 grip will just mask the widget.
659
660
661Bugs
662^^^^
663
664* If the containing toplevel's position was specified relative to the right
665 or bottom of the screen (e.g. ....), the Sizegrip widget will not resize
666 the window.
667* This widget supports only "southeast" resizing.
668
669
670Treeview
671--------
672
673The :class:`ttk.Treeview` widget displays a hierarchical collection of items.
674Each item has a textual label, an optional image, and an optional list of data
675values. The data values are displayed in successive columns after the tree
676label.
677
678The order in which data values are displayed may be controlled by setting
679the widget option displaycolumns. The tree widget can also display column
680headings. Columns may be accessed by number or symbolic names listed in the
681widget option columns. See `Column Identifiers`_.
682
683Each item is identified by an unique name. The widget will generate item IDs
684if they are not supplied by the caller. There is a distinguished root item,
685named {}. The root item itself is not displayed; its children appear at the
686top level of the hierarchy.
687
688Each item also has a list of tags, which can be used to associate even bindings
689with individual items and control the appearance of the item.
690
691The Treeview widget supports horizontal and vertical scrolling, according to
692the options described in `Scrollable Widget Options`_ and the methods
693:meth:`Treeview.xview` and :meth:`Treeview.yview`.
694
695
696Options
697^^^^^^^
698
699This widget accepts the following specific option:
700
701 +----------------+--------------------------------------------------------+
702 | option | description |
703 +================+========================================================+
704 | columns | A list of column identifiers, specifying the number of |
705 | | columns and their names. |
706 +----------------+--------------------------------------------------------+
707 | displaycolumns | A list of column identifiers (either symbolic or |
708 | | integer indices) specifying which data columns are |
709 | | displayed and the order in which they appear, or the |
710 | | string "#all". |
711 +----------------+--------------------------------------------------------+
712 | height | Specifies the number of rows which should be visible. |
713 | | Note: the requested width is determined from the sum |
714 | | of the column widths. |
715 +----------------+--------------------------------------------------------+
716 | padding | Specifies the internal padding for the widget. The |
717 | | padding is a list of up to four length specifications. |
718 +----------------+--------------------------------------------------------+
719 | selectmode | Controls how the built-in class bindings manage the |
720 | | selection. One of "extended", "browse" or "none". |
721 | | If set to "extended" (the default), multiple items may |
722 | | be selected. If "browse", only a single item will be |
723 | | selected at a time. If "none", the selection will not |
724 | | be changed. |
725 | | |
726 | | Note that the application code and tag bindings can set|
727 | | the selection however they wish, regardless the value |
728 | | of this option. |
729 +----------------+--------------------------------------------------------+
730 | show | A list containing zero or more of the following values,|
731 | | specifying which elements of the tree to display. |
732 | | |
733 | | * tree: display tree labels in column #0. |
734 | | * headings: display the heading row. |
735 | | |
736 | | The default is "tree headings", i.e., show all |
737 | | elements. |
738 | | |
739 | | **Note**: Column #0 always refer to the tree column, |
740 | | even if show="tree" is not specified. |
741 +----------------+--------------------------------------------------------+
742
743
744Item Options
745^^^^^^^^^^^^
746
747The following item options may be specified for items in the insert and item
748widget commands.
749
750 +--------+---------------------------------------------------------------+
751 | option | description |
752 +========+===============================================================+
753 | text | The textual label to display for the item. |
754 +--------+---------------------------------------------------------------+
755 | image | A Tk Image, displayed to the left of the label. |
756 +--------+---------------------------------------------------------------+
757 | values | The list of values associated with the item. |
758 | | |
759 | | Each item should have the same number of values as the widget |
760 | | option columns. If there are fewer values than columns, the |
761 | | remaining values are assumed empty. If there are more values |
762 | | than columns, the extra values are ignored. |
763 +--------+---------------------------------------------------------------+
764 | open | True/False value indicating whether the item's children should|
765 | | be displayed or hidden. |
766 +--------+---------------------------------------------------------------+
767 | tags | A list of tags associated with this item. |
768 +--------+---------------------------------------------------------------+
769
770
771Tag Options
772^^^^^^^^^^^
773
774The following options may be specified on tags:
775
776 +------------+-----------------------------------------------------------+
777 | option | description |
778 +============+===========================================================+
779 | foreground | Specifies the text foreground color. |
780 +------------+-----------------------------------------------------------+
781 | background | Specifies the cell or item background color. |
782 +------------+-----------------------------------------------------------+
783 | font | Specifies the font to use when drawing text. |
784 +------------+-----------------------------------------------------------+
785 | image | Specifies the item image, in case the item's image option |
786 | | is empty. |
787 +------------+-----------------------------------------------------------+
788
789
790Column Identifiers
791^^^^^^^^^^^^^^^^^^
792
793Column identifiers take any of the following forms:
794
795* A symbolic name from the list of columns option.
796* An integer n, specifying the nth data column.
797* A string of the form #n, where n is an integer, specifying the nth display
798 column.
799
800Notes:
801
802* Item's option values may be displayed in a different order than the order
803 in which they are stored.
804* Column #0 always refers to the tree column, even if show="tree" is not
805 specified.
806
807A data column number is an index into an item's option values list; a display
808column number is the column number in the tree where the values are displayed.
809Tree labels are displayed in column #0. If option displaycolumns is not set,
810then data column n is displayed in column #n+1. Again, **column #0 always
811refers to the tree column**.
812
813
814Virtual Events
815^^^^^^^^^^^^^^
816
817The Treeview widget generates the following virtual events.
818
819 +--------------------+--------------------------------------------------+
820 | event | description |
821 +====================+==================================================+
822 | <<TreeviewSelect>> | Generated whenever the selection changes. |
823 +--------------------+--------------------------------------------------+
824 | <<TreeviewOpen>> | Generated just before settings the focus item to |
825 | | open=True. |
826 +--------------------+--------------------------------------------------+
827 | <<TreeviewClose>> | Generated just after setting the focus item to |
828 | | open=False. |
829 +--------------------+--------------------------------------------------+
830
831The :meth:`Treeview.focus` and :meth:`Treeview.selection` methods can be used
832to determine the affected item or items.
833
834
835ttk.Treeview
836^^^^^^^^^^^^
837
838.. class:: Treeview
839
840 .. method:: bbox(item[, column=None])
841
842 Returns the bounding box (relative to the treeview widget's window) of
843 the specified *item* in the form (x, y, width, height).
844
845 If *column* is specified, returns the bounding box of that cell. If the
846 *item* is not visible (i.e., if it is a descendant of a closed item or is
847 scrolled offscreen), returns an empty string.
848
849
850 .. method:: get_children([item])
851
852 Returns the list of children belonging to *item*.
853
854 If *item* is not specified, returns root children.
855
856
857 .. method:: set_children(item, *newchildren)
858
859 Replaces item's child with *newchildren*.
860
861 Children present in item that are not present in *newchildren* are
862 detached from tree. No items in *newchildren* may be an ancestor of
863 item. Note that not specifying *newchildren* results in detaching
864 *item*'s children.
865
866
867 .. method:: column(column[, option=None[, **kw]])
868
869 Query or modify the options for the specified *column*.
870
871 If *kw* is not given, returns a dict of the column option values. If
872 *option* is specified then the value for that *option* is returned.
873 Otherwise, sets the options to the corresponding values.
874
875 The valid options/values are:
876
877 * id
878 Returns the column name, this is a read-only option.
879 * anchor: One of the standard Tk anchor values.
880 Specifies how the text in this column should be aligned with respect
881 to the cell.
882 * minwidth: width
883 The minimum width of the column in pixels. The treeview widget will
884 not make the column any smaller than the specified by this option when
885 the widget is resized or the user drags a column.
886 * stretch: True/False
887 Specifies wheter or not the column's width should be adjusted when
888 the widget is resized.
889 * width: width
890 The width of the column in pixels.
891
892 To configure the tree column, call this with column = "#0"
893
894 .. method:: delete(*items)
895
896 Delete all specified *items* and all their descendants.
897
898 The root item may not be deleted.
899
900
901 .. method:: detach(*items)
902
903 Unlinks all of the specified *items* from the tree.
904
905 The items and all of their descendants are still present, and may be
906 reinserted at another point in the tree, but will not be displayed.
907
908 The root item may not be detached.
909
910
911 .. method:: exists(item)
912
913 Returns True if the specified *item* is present in the three,
914 False otherwise.
915
916
917 .. method:: focus([item=None])
918
919 If *item* is specified, sets the focus item to *item*. Otherwise, returns
920 the current focus item, or '' if there is none.
921
922
923 .. method:: heading(column[, option=None[, **kw]])
924
925 Query or modify the heading options for the specified *column*.
926
927 If *kw* is not given, returns a dict of the heading option values. If
928 *option* is specified then the value for that *option* is returned.
929 Otherwise, sets the options to the corresponding values.
930
931 The valid options/values are:
932
933 * text: text
934 The text to display in the column heading.
935 * image: imageName
936 Specifies an image to display to the right of the column heading.
937 * anchor: anchor
938 Specifies how the heading text should be aligned. One of the standard
939 Tk anchor values.
940 * command: callback
941 A callback to be invoked when the heading label is pressed.
942
943 To configure the tree column heading, call this with column = "#0"
944
945
946 .. method:: identify(component, x, y)
947
948 Returns a description of the specified *component* under the point given
949 by *x* and *y*, or the empty string if no such *component* is present at
950 that position.
951
952
953 .. method:: identify_row(y)
954
955 Returns the item ID of the item at position *y*.
956
957
958 .. method:: identify_column(x)
959
960 Returns the data column identifier of the cell at position *x*.
961
962 The tree column has ID #0.
963
964
965 .. method:: identify_region(x, y)
966
967 Returns one of:
968
969 +-----------+--------------------------------------+
970 | region | meaning |
971 +===========+======================================+
972 | heading | Tree heading area. |
973 +-----------+--------------------------------------+
974 | separator | Space between two columns headings. |
975 +-----------+--------------------------------------+
976 | tree | The tree area. |
977 +-----------+--------------------------------------+
978 | cell | A data cell. |
979 +-----------+--------------------------------------+
980
981 Availability: Tk 8.6.
982
983
984 .. method:: identify_element(x, y)
985
986 Returns the element at position x, y.
987
988 Availability: Tk 8.6.
989
990
991 .. method:: index(item)
992
993 Returns the integer index of *item* within its parent's list of children.
994
995
996 .. method:: insert(parent, index[, iid=None[, **kw]])
997
998 Creates a new item and return the item identifier of the newly created
999 item.
1000
1001 *parent* is the item ID of the parent item, or the empty string to create
1002 a new top-level item. *index* is an integer, or the value "end",
1003 specifying where in the list of parent's children to insert the new item.
1004 If *index* is less than or equal to zero, the new node is inserted at
1005 the beginning, if *index* is greater than or equal to the current number
1006 of children, it is inserted at the end. If *iid* is specified, it is used
1007 as the item identifier, *iid* must not already exist in the tree.
1008 Otherwise, a new unique identifier is generated.
1009
1010 See `Item Options`_ for the list of available points.
1011
1012
1013 .. method:: item(item[, option[, **kw]])
1014
1015 Query or modify the options for the specified *item*.
1016
1017 If no options are given, a dict with options/values for the item is
1018 returned.
1019 If *option* is specified then the value for that option is returned.
1020 Otherwise, sets the options to the corresponding values as given by *kw*.
1021
1022
1023 .. method:: move(item, parent, index)
1024
1025 Moves *item* to position *index* in *parent*'s list of children.
1026
1027 It is illegal to move an item under one of its descendants. If index is
1028 less than or equal to zero, item is moved to the beginning, if greater
1029 than or equal to the number of children, it is moved to the end. If item
1030 was detached it is reattached.
1031
1032
1033 .. method:: next(item)
1034
1035 Returns the identifier of *item*'s next sibling, or '' if *item* is the
1036 last child of its parent.
1037
1038
1039 .. method:: parent(item)
1040
1041 Returns the ID of the parent of *item*, or '' if *item* is at the top
1042 level of the hierarchy.
1043
1044
1045 .. method:: prev(item)
1046
1047 Returns the identifier of *item*'s previous sibling, or '' if *item* is
1048 the first child of its parent.
1049
1050
1051 .. method:: reattach(item, parent, index)
1052
1053 An alias for :meth:`Treeview.move`.
1054
1055
1056 .. method:: see(item)
1057
1058 Ensure that *item* is visible.
1059
1060 Sets all of *item*'s ancestors open option to True, and scrolls the
1061 widget if necessary so that *item* is within the visible portion of
1062 the tree.
1063
1064
1065 .. method:: selection([selop=None[, items=None]])
1066
1067 If *selop* is not specified, returns selected items. Otherwise, it will
1068 act according to the following selection methods.
1069
1070
1071 .. method:: selection_set(items)
1072
1073 *items* becomes the new selection.
1074
1075
1076 .. method:: selection_add(items)
1077
1078 Add *items* to the selection.
1079
1080
1081 .. method:: selection_remove(items)
1082
1083 Remove *items* from the selection.
1084
1085
1086 .. method:: selection_toggle(items)
1087
1088 Toggle the selection state of each item in *items*.
1089
1090
1091 .. method:: set(item[, column=None[, value=None]])
1092
1093 With one argument, returns a dictionary of column/value pairs for the
1094 specified *item*. With two arguments, returns the current value of the
1095 specified *column*. With three arguments, sets the value of given
1096 *column* in given *item* to the specified *value*.
1097
1098
1099 .. method:: tag_bind(tagname[, sequence=None[, callback=None]])
1100
1101 Bind a callback for the given event *sequence* to the tag *tagname*.
1102 When an event is delivered to an item, the *callbacks* for each of the
1103 item's tags option are called.
1104
1105
1106 .. method:: tag_configure(tagname[, option=None[, **kw]])
1107
1108 Query or modify the options for the specified *tagname*.
1109
1110 If *kw* is not given, returns a dict of the option settings for
1111 *tagname*. If *option* is specified, returns the value for that *option*
1112 for the specified *tagname*. Otherwise, sets the options to the
1113 corresponding values for the given *tagname*.
1114
1115
1116 .. method:: tag_has(tagname[, item])
1117
1118 If *item* is specified, returns 1 or 0 depending on whether the specified
1119 *item* has the given *tagname*. Otherwise, returns a list of all items
1120 which have the specified tag.
1121
1122 Availability: Tk 8.6
1123
1124
1125 .. method:: xview(*args)
1126
1127 Query or modify horizontal position of the treeview.
1128
1129
1130 .. method:: yview(*args)
1131
1132 Query or modify vertical position of the treeview.
1133
1134
1135.. _TtkStyling:
1136
1137Ttk Styling
1138-----------
1139
1140Each widget in :mod:`ttk` is assigned a style, which specifies the set of
1141elements making up the widget and how they are arranged, along with dynamic
1142and default settings for element options. By default the style name is the
1143same as the widget's class name, but it may be overriden by the widget's style
1144option. If you don't know the class name of a widget, use the method
1145:meth:`Misc.winfo_class` (somewidget.winfo_class()).
1146
1147.. seealso::
1148
1149 `Tcl'2004 conference presentation <http://tktable.sourceforge.net/tile/tile-tcl2004.pdf>`_
1150 This document explains how the theme engine works
1151
1152
1153.. class:: Style
1154
1155 This class is used to manipulate the style database.
1156
1157
1158 .. method:: configure(style, query_opt=None, **kw)
1159
1160 Query or sets the default value of the specified option(s) in *style*.
1161
1162 Each key in *kw* is an option and each value is a string identifying
1163 the value for that option.
1164
1165 For example, to change every default button to be a flat button with
1166 some padding and a different background color you could do::
1167
1168 import ttk
1169 import Tkinter
1170
1171 root = Tkinter.Tk()
1172
1173 ttk.Style().configure("TButton", padding=6, relief="flat",
1174 background="#ccc")
1175
1176 btn = ttk.Button(text="Sample")
1177 btn.pack()
1178
1179 root.mainloop()
1180
1181
1182 .. method:: map(style, query_opt=None, **kw)
1183
1184 Query or sets dynamic values of the specified option(s) in *style*.
1185
1186 Each key in kw is an option and each value should be a list or a
1187 tuple (usually) containing statespecs grouped in tuples, or list, or
1188 something else of your preference. A statespec is compound of one or more
1189 states and then a value.
1190
1191 An example may make it more understandable::
1192
1193 import Tkinter
1194 import ttk
1195
1196 root = Tkinter.Tk()
1197
1198 style = ttk.Style()
1199 style.map("C.TButton",
1200 foreground=[('pressed', 'red'), ('active', 'blue')],
1201 background=[('pressed', '!disabled', 'black'), ('active', 'white')]
1202 )
1203
1204 colored_btn = ttk.Button(text="Test", style="C.TButton").pack()
1205
1206 root.mainloop()
1207
1208
1209 There is a thing to note in this previous short example:
1210
1211 * The order of the (states, value) sequences for an option does matter,
1212 if you changed the order to [('active', 'blue'), ('pressed', 'red')]
1213 in the foreground option, for example, you would get a blue foreground
1214 when the widget were in active or pressed states.
1215
1216
1217 .. method:: lookup(style, option[, state=None[, default=None]])
1218
1219 Returns the value specified for *option* in *style*.
1220
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.
1224
1225 To check what font a Button uses by default, you would do::
1226
1227 import ttk
1228
1229 print ttk.Style().lookup("TButton", "font")
1230
1231
1232 .. method:: layout(style[, layoutspec=None])
1233
1234 Define the widget layout for given *style*. If *layoutspec* is omitted,
1235 return the layout specification for given style.
1236
1237 *layoutspec*, if specified, is expected to be a list, or some other
1238 sequence type (excluding string), 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`_.
1241
1242 To understand the format, check this example below (it is not intended
1243 to do anything useful)::
1244
1245 import ttk
1246 import Tkinter
1247
1248 root = Tkinter.Tk()
1249
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 ])
1261
1262 mbtn = ttk.Menubutton(text='Text')
1263 mbtn.pack()
1264 root.mainloop()
1265
1266
1267 .. method:: element_create(elementname, etype, *args, **kw)
1268
1269 Create a new element in the current theme of 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.
1272
1273 If "image" is used, *args* should contain the default image name followed
1274 by statespec/value pairs (this is the imagespec), *kw* may have the
1275 following options:
1276
1277 * border=padding
1278 padding is a list of up to four integers, specifying the left, top,
1279 right, and bottom borders, respectively.
1280
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.
1284
1285 * padding=padding
1286 Specifies the element's interior padding. Defaults to border's value
1287 if not specified.
1288
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”.
1292
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.
1296
1297 But if "from" is used, then :meth:`element_create` will clone an existing
1298 element. *args* is expected to contain a themename, which is from where
1299 the element will be cloned, and optionally an element to clone from.
1300 If this element to clone from is not specified, an empty element will
1301 be used. *kw* is discarded here.
1302
1303
1304 .. method:: element_names()
1305
1306 Returns the list of elements defined in the current theme.
1307
1308
1309 .. method:: element_options(elementname)
1310
1311 Returns the list of *elementname*'s options.
1312
1313
1314 .. method:: theme_create(themename[, parent=None[, settings=None]])
1315
1316 Create a new theme.
1317
1318 It is an error if *themename* already exists. If *parent* is specified,
1319 the new theme will inherit styles, elements and layouts from the parent
1320 theme. If *settings* are present they are expected to have the same
1321 syntax used for :meth:`theme_settings`.
1322
1323
1324 .. method:: theme_settings(themename, settings)
1325
1326 Temporarily sets the current theme to *themename*, apply specified
1327 *settings* and then restore the previous theme.
1328
1329 Each key in *settings* is a style and each value may contain the keys
1330 'configure', 'map', 'layout' and 'element create' and they are expected
1331 to have the same format as specified by the methods
1332 :meth:`Style.configure`, :meth:`Style.map`, :meth:`Style.layout` and
1333 :meth:`Style.element_create` respectively.
1334
1335 As an example, lets change the Combobox for the default theme a bit::
1336
1337 import ttk
1338 import Tkinter
1339
1340 root = Tkinter.Tk()
1341
1342 style = ttk.Style()
1343 style.theme_settings("default", {
1344 "TCombobox": {
1345 "configure": {"padding": 5},
1346 "map": {
1347 "background": [("active", "green2"),
1348 ("!disabled", "green4")],
1349 "fieldbackground": [("!disabled", "green3")],
1350 "foreground": [("focus", "OliveDrab1"),
1351 ("!disabled", "OliveDrab2")]
1352 }
1353 }
1354 })
1355
1356 combo = ttk.Combobox().pack()
1357
1358 root.mainloop()
1359
1360
1361 .. method:: theme_names()
1362
1363 Returns a list of all known themes.
1364
1365
1366 .. method:: theme_use([themename])
1367
1368 If *themename* is not given, returns the theme in use, otherwise, set
1369 the current theme to *themename*, refreshes all widgets and emits a
1370 <<ThemeChanged>> event.
1371
1372
1373Layouts
1374^^^^^^^
1375
1376A layout can be just None, if takes no options, or a dict of options specifying
1377how to arrange the element. The layout mechanism uses a simplified
1378version of the pack geometry manager: given an initial cavity, each element is
1379allocated a parcel. Valid options/values are:
1380
1381 * side: whichside
1382 Specifies which side of the cavity to place the the element; one of
1383 top, right, bottom or left. If omitted, the element occupies the
1384 entire cavity.
1385
1386 * sticky: nswe
1387 Specifies where the element is placed inside its allocated parcel.
1388
1389 * unit: 0 or 1
1390 If set to 1, causes the element and all of its descendants to be treated as
1391 a single element for the purposes of :meth:`Widget.identify` et al. It's
1392 used for things like scrollbar thumbs with grips.
1393
1394 * children: [sublayout... ]
1395 Specifies a list of elements to place inside the element. Each
1396 element is a tuple (or other sequence type) where the first item is
1397 the layout name, and the other is a `Layout`_.
1398
1399.. _Layout: `Layouts`_