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