blob: 1795771caa704b84bc01f7f9c52c929ea6b8b73e [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
Benjamin Peterson288618e2009-03-03 22:51:57 +000011The :mod:`ttk` module provides access to the Tk themed widget set, which has
12been introduced in Tk 8.5. If Python is not compiled against Tk 8.5 code may
13still use this module as long as Tile is installed. However, some features
14provided by the new Tk, like anti-aliased font rendering under X11, window
15transparency (on X11 you will need a composition window manager) will be
16missing.
Guilherme Polocda93aa2009-01-28 13:09:03 +000017
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
Benjamin Peterson288618e2009-03-03 22:51:57 +000031To start using Ttk, import its module::
Guilherme Polocda93aa2009-01-28 13:09:03 +000032
33 import ttk
34
Benjamin Peterson288618e2009-03-03 22:51:57 +000035But code like this::
Guilherme Polocda93aa2009-01-28 13:09:03 +000036
37 from Tkinter import *
38
Benjamin Peterson288618e2009-03-03 22:51:57 +000039may optionally want to use this::
Guilherme Polocda93aa2009-01-28 13:09:03 +000040
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`
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +000047and :class:`Scrollbar`) will automatically substitute for the Tk widgets.
Guilherme Polocda93aa2009-01-28 13:09:03 +000048
Benjamin Peterson288618e2009-03-03 22:51:57 +000049This has the direct benefit of using the new widgets, giving better look & feel
50across platforms, but be aware that they are not totally compatible. The main
51difference is that widget options such as "fg", "bg" and others related to
52widget styling are no longer present in Ttk widgets. Use :class:`ttk.Style` to
53achieve the same (or better) styling.
Guilherme Polocda93aa2009-01-28 13:09:03 +000054
55.. seealso::
56
57 `Converting existing applications to use the Tile widgets <http://tktable.sourceforge.net/tile/doc/converting.txt>`_
58 A text which talks in Tcl terms about differences typically found when
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +000059 converting applications to use the new widgets.
Guilherme Polocda93aa2009-01-28 13:09:03 +000060
61
62Ttk Widgets
63-----------
64
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +000065Ttk comes with 17 widgets, 11 of which already exist in Tkinter:
Guilherme Polocda93aa2009-01-28 13:09:03 +000066:class:`Button`, :class:`Checkbutton`, :class:`Entry`, :class:`Frame`,
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +000067:class:`Label`, :class:`LabelFrame`, :class:`Menubutton`,
68:class:`PanedWindow`, :class:`Radiobutton`, :class:`Scale` and
69:class:`Scrollbar`. The 6 new widget classes are: :class:`Combobox`,
70:class:`Notebook`, :class:`Progressbar`, :class:`Separator`,
71:class:`Sizegrip` and :class:`Treeview`. All of these classes are
Guilherme Polocda93aa2009-01-28 13:09:03 +000072subclasses of :class:`Widget`.
73
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +000074As said previously, you will notice changes in look-and-feel as well in the
Guilherme Polocda93aa2009-01-28 13:09:03 +000075styling 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
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +000083Corresponding Ttk code::
Guilherme Polocda93aa2009-01-28 13:09:03 +000084
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
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000104All the :mod:`ttk` widgets accept the following options:
Guilherme Polocda93aa2009-01-28 13:09:03 +0000105
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 |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000113 | | option which may only be specified when the window is |
114 | | created. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000115 +-----------+--------------------------------------------------------------+
116 | cursor | Specifies the mouse cursor to be used for the widget. If set |
117 | | to the empty string (the default), the cursor is inherited |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000118 | | from the parent widget. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000119 +-----------+--------------------------------------------------------------+
120 | takefocus | Determines whether the window accepts the focus during |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000121 | | keyboard traversal. 0, 1 or an empty string is returned. |
122 | | If 0, the window should be skipped entirely |
123 | | during keyboard traversal. If 1, the window |
124 | | should receive the input focus as long as it is viewable. |
125 | | An empty string means that the traversal scripts make the |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000126 | | decision about whether or not to focus on the window. |
127 +-----------+--------------------------------------------------------------+
128 | style | May be used to specify a custom widget style. |
129 +-----------+--------------------------------------------------------------+
130
131
132Scrollable Widget Options
133^^^^^^^^^^^^^^^^^^^^^^^^^
134
135The following options are supported by widgets that are controlled by a
136scrollbar.
137
138 +----------------+---------------------------------------------------------+
139 | option | description |
140 +================+=========================================================+
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000141 | xscrollcommand | Used to communicate with horizontal scrollbars. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000142 | | |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000143 | | When the view in the widget's window changes, the widget|
Guilherme Polocda93aa2009-01-28 13:09:03 +0000144 | | will generate a Tcl command based on the scrollcommand. |
145 | | |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000146 | | Usually this option consists of the |
147 | | :meth:`Scrollbar.set` method of some scrollbar. This |
148 | | will cause |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000149 | | the scrollbar to be updated whenever the view in the |
150 | | window changes. |
151 +----------------+---------------------------------------------------------+
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000152 | yscrollcommand | Used to communicate with vertical scrollbars. |
153 | | For more information, see above. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000154 +----------------+---------------------------------------------------------+
155
156
157Label Options
158^^^^^^^^^^^^^
159
160The following options are supported by labels, buttons and other button-like
161widgets.
162
163 +--------------+-----------------------------------------------------------+
164 | option | description |
165 +==============+===========================================================+
166 | text | Specifies a text string to be displayed inside the widget.|
167 +--------------+-----------------------------------------------------------+
168 | textvariable | Specifies a name whose value will be used in place of the |
169 | | text option resource. |
170 +--------------+-----------------------------------------------------------+
171 | underline | If set, specifies the index (0-based) of a character to |
172 | | underline in the text string. The underline character is |
173 | | used for mnemonic activation. |
174 +--------------+-----------------------------------------------------------+
175 | image | Specifies an image to display. This is a list of 1 or more|
176 | | elements. The first element is the default image name. The|
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000177 | | rest of the list is a sequence of statespec/value pairs as|
Guilherme Polocda93aa2009-01-28 13:09:03 +0000178 | | defined by :meth:`Style.map`, specifying different images |
179 | | to use when the widget is in a particular state or a |
180 | | combination of states. All images in the list should have |
181 | | the same size. |
182 +--------------+-----------------------------------------------------------+
183 | compound | Specifies how to display the image relative to the text, |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000184 | | in the case both text and image options are present. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000185 | | Valid values are: |
186 | | |
187 | | * text: display text only |
188 | | * image: display image only |
189 | | * top, bottom, left, right: display image above, below, |
190 | | left of, or right of the text, respectively. |
191 | | * none: the default. display the image if present, |
192 | | otherwise the text. |
193 +--------------+-----------------------------------------------------------+
194 | width | If greater than zero, specifies how much space, in |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000195 | | character widths, to allocate for the text label; if less |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000196 | | than zero, specifies a minimum width. If zero or |
197 | | unspecified, the natural width of the text label is used. |
198 +--------------+-----------------------------------------------------------+
199
200
201Compatibility Options
202^^^^^^^^^^^^^^^^^^^^^
203
204 +--------+----------------------------------------------------------------+
205 | option | description |
206 +========+================================================================+
207 | state | May be set to "normal" or "disabled" to control the "disabled" |
208 | | state bit. This is a write-only option: setting it changes the |
209 | | widget state, but the :meth:`Widget.state` method does not |
210 | | affect this option. |
211 +--------+----------------------------------------------------------------+
212
213Widget States
214^^^^^^^^^^^^^
215
216The widget state is a bitmap of independent state flags.
217
218 +------------+-------------------------------------------------------------+
219 | flag | description |
220 +============+=============================================================+
221 | active | The mouse cursor is over the widget and pressing a mouse |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000222 | | button will cause some action to occur. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000223 +------------+-------------------------------------------------------------+
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000224 | disabled | Widget is disabled under program control. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000225 +------------+-------------------------------------------------------------+
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000226 | focus | Widget has keyboard focus. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000227 +------------+-------------------------------------------------------------+
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000228 | pressed | Widget is being pressed. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000229 +------------+-------------------------------------------------------------+
230 | selected | "On", "true", or "current" for things like Checkbuttons and |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000231 | | radiobuttons. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000232 +------------+-------------------------------------------------------------+
233 | background | Windows and Mac have a notion of an "active" or foreground |
234 | | window. The *background* state is set for widgets in a |
235 | | background window, and cleared for those in the foreground |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000236 | | window. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000237 +------------+-------------------------------------------------------------+
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000238 | readonly | Widget should not allow user modification. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000239 +------------+-------------------------------------------------------------+
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000240 | alternate | A widget-specific alternate display format. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000241 +------------+-------------------------------------------------------------+
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000242 | invalid | The widget's value is invalid. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000243 +------------+-------------------------------------------------------------+
244
245A state specification is a sequence of state names, optionally prefixed with
246an exclamation point indicating that the bit is off.
247
248
249ttk.Widget
250^^^^^^^^^^
251
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000252Besides the methods described below, the :class:`ttk.Widget` class supports the
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000253:meth:`Tkinter.Widget.cget` and :meth:`Tkinter.Widget.configure` methods.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000254
255.. class:: Widget
256
257 .. method:: identify(x, y)
258
259 Returns the name of the element at position *x* *y*, or the empty string
260 if the point does not lie within any element.
261
262 *x* and *y* are pixel coordinates relative to the widget.
263
264
265 .. method:: instate(statespec[, callback=None[, *args[, **kw]]])
266
267 Test the widget's state. If a callback is not specified, returns True
268 if the widget state matches *statespec* and False otherwise. If callback
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000269 is specified then it is called with *args* if widget state matches
Guilherme Polocda93aa2009-01-28 13:09:03 +0000270 *statespec*.
271
272
273 .. method:: state([statespec=None])
274
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000275 Modify or read widget state. If *statespec* is specified, sets the
276 widget state accordingly and returns a new *statespec* indicating
Guilherme Polocda93aa2009-01-28 13:09:03 +0000277 which flags were changed. If *statespec* is not specified, returns
278 the currently-enabled state flags.
279
280 *statespec* will usually be a list or a tuple.
281
282
283Combobox
284--------
285
286The :class:`ttk.Combobox` widget combines a text field with a pop-down list of
287values. This widget is a subclass of :class:`Entry`.
288
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000289Besides the methods inherited from :class:`Widget` (:meth:`Widget.cget`,
Guilherme Polocda93aa2009-01-28 13:09:03 +0000290:meth:`Widget.configure`, :meth:`Widget.identify`, :meth:`Widget.instate`
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000291and :meth:`Widget.state`) and those inherited from :class:`Entry`
292(:meth:`Entry.bbox`, :meth:`Entry.delete`, :meth:`Entry.icursor`,
Guilherme Polocda93aa2009-01-28 13:09:03 +0000293:meth:`Entry.index`, :meth:`Entry.inset`, :meth:`Entry.selection`,
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000294:meth:`Entry.xview`), this class has some other methods, described at
Guilherme Polocda93aa2009-01-28 13:09:03 +0000295:class:`ttk.Combobox`.
296
297
298Options
299^^^^^^^
300
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000301This widget accepts the following options:
Guilherme Polocda93aa2009-01-28 13:09:03 +0000302
303 +-----------------+--------------------------------------------------------+
304 | option | description |
305 +=================+========================================================+
306 | exportselection | Boolean value. If set, the widget selection is linked |
307 | | to the Window Manager selection (which can be returned |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000308 | | by invoking :meth:`Misc.selection_get`, for example). |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000309 +-----------------+--------------------------------------------------------+
310 | justify | Specifies how the text is aligned within the widget. |
311 | | One of "left", "center", or "right". |
312 +-----------------+--------------------------------------------------------+
313 | height | Specifies the height of the pop-down listbox, in rows. |
314 +-----------------+--------------------------------------------------------+
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000315 | postcommand | A script (possibly registered with |
316 | | :meth:`Misc.register`) that |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000317 | | is called immediately before displaying the values. It |
318 | | may specify which values to display. |
319 +-----------------+--------------------------------------------------------+
320 | state | One of "normal", "readonly", or "disabled". In the |
321 | | "readonly" state, the value may not be edited directly,|
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000322 | | and the user can only select one of the values from the|
Guilherme Polocda93aa2009-01-28 13:09:03 +0000323 | | dropdown list. In the "normal" state, the text field is|
324 | | directly editable. In the "disabled" state, no |
325 | | interaction is possible. |
326 +-----------------+--------------------------------------------------------+
327 | textvariable | Specifies a name whose value is linked to the widget |
328 | | value. Whenever the value associated with that name |
329 | | changes, the widget value is updated, and vice versa. |
330 | | See :class:`Tkinter.StringVar`. |
331 +-----------------+--------------------------------------------------------+
332 | values | Specifies the list of values to display in the |
333 | | drop-down listbox. |
334 +-----------------+--------------------------------------------------------+
335 | width | Specifies an integer value indicating the desired width|
336 | | of the entry window, in average-size characters of the |
337 | | widget's font. |
338 +-----------------+--------------------------------------------------------+
339
340
341Virtual events
342^^^^^^^^^^^^^^
343
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000344The combobox widget generates a **<<ComboboxSelected>>** virtual event
Guilherme Polocda93aa2009-01-28 13:09:03 +0000345when the user selects an element from the list of values.
346
347
348ttk.Combobox
349^^^^^^^^^^^^
350
351.. class:: Combobox
352
353 .. method:: current([newindex=None])
354
355 If *newindex* is specified, sets the combobox value to the element
356 position *newindex*. Otherwise, returns the index of the current value or
357 -1 if the current value is not in the values list.
358
359
360 .. method:: get()
361
362 Returns the current value of the combobox.
363
364
365 .. method:: set(value)
366
367 Sets the value of the combobox to *value*.
368
369
370Notebook
371--------
372
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000373The Ttk Notebook widget manages a collection of windows and displays a single
Guilherme Polocda93aa2009-01-28 13:09:03 +0000374one at a time. Each child window is associated with a tab, which the user
375may select to change the currently-displayed window.
376
377
378Options
379^^^^^^^
380
381This widget accepts the following specific options:
382
383 +---------+----------------------------------------------------------------+
384 | option | description |
385 +=========+================================================================+
386 | height | If present and greater than zero, specifies the desired height |
387 | | of the pane area (not including internal padding or tabs). |
388 | | Otherwise, the maximum height of all panes is used. |
389 +---------+----------------------------------------------------------------+
390 | padding | Specifies the amount of extra space to add around the outside |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000391 | | of the notebook. The padding is a list of up to four length |
392 | | specifications: left top right bottom. If fewer than four |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000393 | | elements are specified, bottom defaults to top, right defaults |
394 | | to left, and top defaults to left. |
395 +---------+----------------------------------------------------------------+
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000396 | width | If present and greater than zero, specifies the desired width |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000397 | | of the pane area (not including internal padding). Otherwise, |
398 | | the maximum width of all panes is used. |
399 +---------+----------------------------------------------------------------+
400
401
402Tab Options
403^^^^^^^^^^^
404
405There are also specific options for tabs:
406
407 +-----------+--------------------------------------------------------------+
408 | option | description |
409 +===========+==============================================================+
410 | state | Either "normal", "disabled" or "hidden". If "disabled", then |
411 | | the tab is not selectable. If "hidden", then the tab is not |
412 | | shown. |
413 +-----------+--------------------------------------------------------------+
414 | sticky | Specifies how the child window is positioned within the pane |
415 | | area. Value is a string containing zero or more of the |
416 | | characters "n", "s", "e" or "w". Each letter refers to a |
417 | | side (north, south, east or west) that the child window will |
418 | | stick to, as per the :meth:`grid` geometry manager. |
419 +-----------+--------------------------------------------------------------+
420 | padding | Specifies the amount of extra space to add between the |
421 | | notebook and this pane. Syntax is the same as for the option |
422 | | padding used by this widget. |
423 +-----------+--------------------------------------------------------------+
424 | text | Specifies a text to be displayed in the tab. |
425 +-----------+--------------------------------------------------------------+
426 | image | Specifies an image to display in the tab. See the option |
427 | | image described in :class:`Widget`. |
428 +-----------+--------------------------------------------------------------+
429 | compound | Specifies how to display the image relative to the text, in |
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000430 | | the case both text and image options are present. See |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000431 | | `Label Options`_ for legal values. |
432 +-----------+--------------------------------------------------------------+
433 | underline | Specifies the index (0-based) of a character to underline in |
434 | | the text string. The underlined character is used for |
435 | | mnemonic activation if :meth:`Notebook.enable_traversal` is |
436 | | called. |
437 +-----------+--------------------------------------------------------------+
438
439
440Tab Identifiers
441^^^^^^^^^^^^^^^
442
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000443The *tab_id* present in several methods of :class:`ttk.Notebook` may take any
Guilherme Polocda93aa2009-01-28 13:09:03 +0000444of the following forms:
445
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000446* An integer between zero and the number of tabs.
447* The name of a child window.
448* A positional specification of the form "@x,y", which identifies the tab.
449* The literal string "current", which identifies the currently-selected tab.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000450* The literal string "end", which returns the number of tabs (only valid for
Andrew M. Kuchling8c2f85c2009-01-31 03:26:02 +0000451 :meth:`Notebook.index`).
Guilherme Polocda93aa2009-01-28 13:09:03 +0000452
453
454Virtual Events
455^^^^^^^^^^^^^^
456
457This widget generates a **<<NotebookTabChanged>>** virtual event after a new
458tab is selected.
459
460
461ttk.Notebook
462^^^^^^^^^^^^
463
464.. class:: Notebook
465
466 .. method:: add(child, **kw)
467
468 Adds a new tab to the notebook.
469
470 If window is currently managed by the notebook but hidden, it is
471 restored to its previous position.
472
473 See `Tab Options`_ for the list of available options.
474
475
476 .. method:: forget(tab_id)
477
478 Removes the tab specified by *tab_id*, unmaps and unmanages the
479 associated window.
480
481
482 .. method:: hide(tab_id)
483
484 Hides the tab specified by *tab_id*.
485
486 The tab will not be displayed, but the associated window remains
487 managed by the notebook and its configuration remembered. Hidden tabs
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000488 may be restored with the :meth:`add` command.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000489
490
491 .. method:: identify(x, y)
492
493 Returns the name of the tab element at position *x*, *y*, or the empty
494 string if none.
495
496
497 .. method:: index(tab_id)
498
499 Returns the numeric index of the tab specified by *tab_id*, or the total
500 number of tabs if *tab_id* is the string "end".
501
502
503 .. method:: insert(pos, child, **kw)
504
505 Inserts a pane at the specified position.
506
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000507 *pos* is either the string "end", an integer index, or the name of a
Guilherme Polocda93aa2009-01-28 13:09:03 +0000508 managed child. If *child* is already managed by the notebook, moves it to
509 the specified position.
510
511 See `Tab Options`_ for the list of available options.
512
513
514 .. method:: select([tab_id])
515
516 Selects the specified *tab_id*.
517
518 The associated child window will be displayed, and the
519 previously-selected window (if different) is unmapped. If *tab_id* is
520 omitted, returns the widget name of the currently selected pane.
521
522
523 .. method:: tab(tab_id[, option=None[, **kw]])
524
525 Query or modify the options of the specific *tab_id*.
526
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000527 If *kw* is not given, returns a dictionary of the tab option values. If
Guilherme Polocda93aa2009-01-28 13:09:03 +0000528 *option* is specified, returns the value of that *option*. Otherwise,
529 sets the options to the corresponding values.
530
531
532 .. method:: tabs()
533
534 Returns a list of windows managed by the notebook.
535
536
537 .. method:: enable_traversal()
538
539 Enable keyboard traversal for a toplevel window containing this notebook.
540
541 This will extend the bindings for the toplevel window containing the
542 notebook as follows:
543
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000544 * Control-Tab: selects the tab following the currently selected one.
545 * Shift-Control-Tab: selects the tab preceding the currently selected one.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000546 * Alt-K: where K is the mnemonic (underlined) character of any tab, will
547 select that tab.
548
549 Multiple notebooks in a single toplevel may be enabled for traversal,
550 including nested notebooks. However, notebook traversal only works
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000551 properly if all panes have the notebook they are in as master.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000552
553
554Progressbar
555-----------
556
557The :class:`ttk.Progressbar` widget shows the status of a long-running
558operation. It can operate in two modes: determinate mode shows the amount
559completed relative to the total amount of work to be done, and indeterminate
560mode provides an animated display to let the user know that something is
561happening.
562
563
564Options
565^^^^^^^
566
567This widget accepts the following specific options:
568
569 +----------+---------------------------------------------------------------+
570 | option | description |
571 +==========+===============================================================+
572 | orient | One of "horizontal" or "vertical". Specifies the orientation |
573 | | of the progress bar. |
574 +----------+---------------------------------------------------------------+
575 | length | Specifies the length of the long axis of the progress bar |
576 | | (width if horizontal, height if vertical). |
577 +----------+---------------------------------------------------------------+
578 | mode | One of "determinate" or "indeterminate". |
579 +----------+---------------------------------------------------------------+
580 | maximum | A number specifying the maximum value. Defaults to 100. |
581 +----------+---------------------------------------------------------------+
582 | value | The current value of the progress bar. In "determinate" mode, |
583 | | this represents the amount of work completed. In |
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000584 | | "indeterminate" mode, it is interpreted as modulo *maximum*; |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000585 | | that is, the progress bar completes one "cycle" when its value|
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000586 | | increases by *maximum*. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000587 +----------+---------------------------------------------------------------+
588 | variable | A name which is linked to the option value. If specified, the |
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000589 | | value of the progress bar is automatically set to the value of|
Guilherme Polocda93aa2009-01-28 13:09:03 +0000590 | | this name whenever the latter is modified. |
591 +----------+---------------------------------------------------------------+
592 | phase | Read-only option. The widget periodically increments the value|
593 | | of this option whenever its value is greater than 0 and, in |
594 | | determinate mode, less than maximum. This option may be used |
595 | | by the current theme to provide additional animation effects. |
596 +----------+---------------------------------------------------------------+
597
598
599ttk.Progressbar
600^^^^^^^^^^^^^^^
601
602.. class:: Progressbar
603
604 .. method:: start([interval])
605
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000606 Begin autoincrement mode: schedules a recurring timer event that calls
Guilherme Polocda93aa2009-01-28 13:09:03 +0000607 :meth:`Progressbar.step` every *interval* milliseconds. If omitted,
608 *interval* defaults to 50 milliseconds.
609
610
611 .. method:: step([amount])
612
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000613 Increments the progress bar's value by *amount*.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000614
615 *amount* defaults to 1.0 if omitted.
616
617
618 .. method:: stop()
619
620 Stop autoincrement mode: cancels any recurring timer event initiated by
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000621 :meth:`Progressbar.start` for this progress bar.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000622
623
624Separator
625---------
626
627The :class:`ttk.Separator` widget displays a horizontal or vertical separator
628bar.
629
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000630It has no other methods besides the ones inherited from :class:`ttk.Widget`.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000631
632
633Options
634^^^^^^^
635
636This widget accepts the following specific option:
637
638 +--------+----------------------------------------------------------------+
639 | option | description |
640 +========+================================================================+
641 | orient | One of "horizontal" or "vertical". Specifies the orientation of|
642 | | the separator. |
643 +--------+----------------------------------------------------------------+
644
645
646Sizegrip
647--------
648
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000649The :class:`ttk.Sizegrip` widget (also known as a grow box) allows the user to
Guilherme Polocda93aa2009-01-28 13:09:03 +0000650resize the containing toplevel window by pressing and dragging the grip.
651
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000652This widget has neither specific options nor specific methods, besides the
Guilherme Polocda93aa2009-01-28 13:09:03 +0000653ones inherited from :class:`ttk.Widget`.
654
655
656Platform-specific notes
657^^^^^^^^^^^^^^^^^^^^^^^
658
Georg Brandl89b12962009-04-05 10:29:57 +0000659* On Mac OS X, toplevel windows automatically include a built-in size grip
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000660 by default. Adding a :class:`Sizegrip` is harmless, since the built-in
Guilherme Polocda93aa2009-01-28 13:09:03 +0000661 grip will just mask the widget.
662
663
664Bugs
665^^^^
666
667* If the containing toplevel's position was specified relative to the right
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000668 or bottom of the screen (e.g. ....), the :class:`Sizegrip` widget will
669 not resize the window.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000670* This widget supports only "southeast" resizing.
671
672
673Treeview
674--------
675
676The :class:`ttk.Treeview` widget displays a hierarchical collection of items.
677Each item has a textual label, an optional image, and an optional list of data
678values. The data values are displayed in successive columns after the tree
679label.
680
681The order in which data values are displayed may be controlled by setting
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000682the widget option ``displaycolumns``. The tree widget can also display column
Guilherme Polocda93aa2009-01-28 13:09:03 +0000683headings. Columns may be accessed by number or symbolic names listed in the
684widget option columns. See `Column Identifiers`_.
685
686Each item is identified by an unique name. The widget will generate item IDs
687if they are not supplied by the caller. There is a distinguished root item,
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000688named ``{}``. The root item itself is not displayed; its children appear at the
Guilherme Polocda93aa2009-01-28 13:09:03 +0000689top level of the hierarchy.
690
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000691Each item also has a list of tags, which can be used to associate event bindings
Guilherme Polocda93aa2009-01-28 13:09:03 +0000692with individual items and control the appearance of the item.
693
694The Treeview widget supports horizontal and vertical scrolling, according to
695the options described in `Scrollable Widget Options`_ and the methods
696:meth:`Treeview.xview` and :meth:`Treeview.yview`.
697
698
699Options
700^^^^^^^
701
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000702This widget accepts the following specific options:
Guilherme Polocda93aa2009-01-28 13:09:03 +0000703
704 +----------------+--------------------------------------------------------+
705 | option | description |
706 +================+========================================================+
707 | columns | A list of column identifiers, specifying the number of |
708 | | columns and their names. |
709 +----------------+--------------------------------------------------------+
710 | displaycolumns | A list of column identifiers (either symbolic or |
711 | | integer indices) specifying which data columns are |
712 | | displayed and the order in which they appear, or the |
713 | | string "#all". |
714 +----------------+--------------------------------------------------------+
715 | height | Specifies the number of rows which should be visible. |
716 | | Note: the requested width is determined from the sum |
717 | | of the column widths. |
718 +----------------+--------------------------------------------------------+
719 | padding | Specifies the internal padding for the widget. The |
720 | | padding is a list of up to four length specifications. |
721 +----------------+--------------------------------------------------------+
722 | selectmode | Controls how the built-in class bindings manage the |
723 | | selection. One of "extended", "browse" or "none". |
724 | | If set to "extended" (the default), multiple items may |
725 | | be selected. If "browse", only a single item will be |
726 | | selected at a time. If "none", the selection will not |
727 | | be changed. |
728 | | |
729 | | Note that the application code and tag bindings can set|
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000730 | | the selection however they wish, regardless of the |
731 | | value of this option. |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000732 +----------------+--------------------------------------------------------+
733 | show | A list containing zero or more of the following values,|
734 | | specifying which elements of the tree to display. |
735 | | |
736 | | * tree: display tree labels in column #0. |
737 | | * headings: display the heading row. |
738 | | |
739 | | The default is "tree headings", i.e., show all |
740 | | elements. |
741 | | |
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000742 | | **Note**: Column #0 always refers to the tree column, |
Guilherme Polocda93aa2009-01-28 13:09:03 +0000743 | | even if show="tree" is not specified. |
744 +----------------+--------------------------------------------------------+
745
746
747Item Options
748^^^^^^^^^^^^
749
750The following item options may be specified for items in the insert and item
751widget commands.
752
753 +--------+---------------------------------------------------------------+
754 | option | description |
755 +========+===============================================================+
756 | text | The textual label to display for the item. |
757 +--------+---------------------------------------------------------------+
758 | image | A Tk Image, displayed to the left of the label. |
759 +--------+---------------------------------------------------------------+
760 | values | The list of values associated with the item. |
761 | | |
762 | | Each item should have the same number of values as the widget |
763 | | option columns. If there are fewer values than columns, the |
764 | | remaining values are assumed empty. If there are more values |
765 | | than columns, the extra values are ignored. |
766 +--------+---------------------------------------------------------------+
767 | open | True/False value indicating whether the item's children should|
768 | | be displayed or hidden. |
769 +--------+---------------------------------------------------------------+
770 | tags | A list of tags associated with this item. |
771 +--------+---------------------------------------------------------------+
772
773
774Tag Options
775^^^^^^^^^^^
776
777The following options may be specified on tags:
778
779 +------------+-----------------------------------------------------------+
780 | option | description |
781 +============+===========================================================+
782 | foreground | Specifies the text foreground color. |
783 +------------+-----------------------------------------------------------+
784 | background | Specifies the cell or item background color. |
785 +------------+-----------------------------------------------------------+
786 | font | Specifies the font to use when drawing text. |
787 +------------+-----------------------------------------------------------+
788 | image | Specifies the item image, in case the item's image option |
789 | | is empty. |
790 +------------+-----------------------------------------------------------+
791
792
793Column Identifiers
794^^^^^^^^^^^^^^^^^^
795
796Column identifiers take any of the following forms:
797
798* A symbolic name from the list of columns option.
799* An integer n, specifying the nth data column.
800* A string of the form #n, where n is an integer, specifying the nth display
801 column.
802
803Notes:
804
805* Item's option values may be displayed in a different order than the order
806 in which they are stored.
807* Column #0 always refers to the tree column, even if show="tree" is not
808 specified.
809
810A data column number is an index into an item's option values list; a display
811column number is the column number in the tree where the values are displayed.
812Tree labels are displayed in column #0. If option displaycolumns is not set,
813then data column n is displayed in column #n+1. Again, **column #0 always
814refers to the tree column**.
815
816
817Virtual Events
818^^^^^^^^^^^^^^
819
820The Treeview widget generates the following virtual events.
821
822 +--------------------+--------------------------------------------------+
823 | event | description |
824 +====================+==================================================+
825 | <<TreeviewSelect>> | Generated whenever the selection changes. |
826 +--------------------+--------------------------------------------------+
827 | <<TreeviewOpen>> | Generated just before settings the focus item to |
828 | | open=True. |
829 +--------------------+--------------------------------------------------+
830 | <<TreeviewClose>> | Generated just after setting the focus item to |
831 | | open=False. |
832 +--------------------+--------------------------------------------------+
833
834The :meth:`Treeview.focus` and :meth:`Treeview.selection` methods can be used
835to determine the affected item or items.
836
837
838ttk.Treeview
839^^^^^^^^^^^^
840
841.. class:: Treeview
842
843 .. method:: bbox(item[, column=None])
844
845 Returns the bounding box (relative to the treeview widget's window) of
846 the specified *item* in the form (x, y, width, height).
847
848 If *column* is specified, returns the bounding box of that cell. If the
849 *item* is not visible (i.e., if it is a descendant of a closed item or is
850 scrolled offscreen), returns an empty string.
851
852
853 .. method:: get_children([item])
854
855 Returns the list of children belonging to *item*.
856
857 If *item* is not specified, returns root children.
858
859
860 .. method:: set_children(item, *newchildren)
861
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000862 Replaces *item*'s child with *newchildren*.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000863
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000864 Children present in *item* that are not present in *newchildren* are
865 detached from the tree. No items in *newchildren* may be an ancestor of
866 *item*. Note that not specifying *newchildren* results in detaching
Guilherme Polocda93aa2009-01-28 13:09:03 +0000867 *item*'s children.
868
869
870 .. method:: column(column[, option=None[, **kw]])
871
872 Query or modify the options for the specified *column*.
873
874 If *kw* is not given, returns a dict of the column option values. If
875 *option* is specified then the value for that *option* is returned.
876 Otherwise, sets the options to the corresponding values.
877
878 The valid options/values are:
879
880 * id
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000881 Returns the column name. This is a read-only option.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000882 * anchor: One of the standard Tk anchor values.
883 Specifies how the text in this column should be aligned with respect
884 to the cell.
885 * minwidth: width
886 The minimum width of the column in pixels. The treeview widget will
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000887 not make the column any smaller than specified by this option when
Guilherme Polocda93aa2009-01-28 13:09:03 +0000888 the widget is resized or the user drags a column.
889 * stretch: True/False
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000890 Specifies whether the column's width should be adjusted when
Guilherme Polocda93aa2009-01-28 13:09:03 +0000891 the widget is resized.
892 * width: width
893 The width of the column in pixels.
894
895 To configure the tree column, call this with column = "#0"
896
897 .. method:: delete(*items)
898
899 Delete all specified *items* and all their descendants.
900
901 The root item may not be deleted.
902
903
904 .. method:: detach(*items)
905
906 Unlinks all of the specified *items* from the tree.
907
908 The items and all of their descendants are still present, and may be
909 reinserted at another point in the tree, but will not be displayed.
910
911 The root item may not be detached.
912
913
914 .. method:: exists(item)
915
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000916 Returns True if the specified *item* is present in the tree.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000917
918
919 .. method:: focus([item=None])
920
921 If *item* is specified, sets the focus item to *item*. Otherwise, returns
922 the current focus item, or '' if there is none.
923
924
925 .. method:: heading(column[, option=None[, **kw]])
926
927 Query or modify the heading options for the specified *column*.
928
929 If *kw* is not given, returns a dict of the heading option values. If
930 *option* is specified then the value for that *option* is returned.
931 Otherwise, sets the options to the corresponding values.
932
933 The valid options/values are:
934
935 * text: text
936 The text to display in the column heading.
937 * image: imageName
938 Specifies an image to display to the right of the column heading.
939 * anchor: anchor
940 Specifies how the heading text should be aligned. One of the standard
941 Tk anchor values.
942 * command: callback
943 A callback to be invoked when the heading label is pressed.
944
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000945 To configure the tree column heading, call this with column = "#0".
Guilherme Polocda93aa2009-01-28 13:09:03 +0000946
947
948 .. method:: identify(component, x, y)
949
950 Returns a description of the specified *component* under the point given
951 by *x* and *y*, or the empty string if no such *component* is present at
952 that position.
953
954
955 .. method:: identify_row(y)
956
957 Returns the item ID of the item at position *y*.
958
959
960 .. method:: identify_column(x)
961
962 Returns the data column identifier of the cell at position *x*.
963
964 The tree column has ID #0.
965
966
967 .. method:: identify_region(x, y)
968
969 Returns one of:
970
971 +-----------+--------------------------------------+
972 | region | meaning |
973 +===========+======================================+
974 | heading | Tree heading area. |
975 +-----------+--------------------------------------+
976 | separator | Space between two columns headings. |
977 +-----------+--------------------------------------+
978 | tree | The tree area. |
979 +-----------+--------------------------------------+
980 | cell | A data cell. |
981 +-----------+--------------------------------------+
982
983 Availability: Tk 8.6.
984
985
986 .. method:: identify_element(x, y)
987
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +0000988 Returns the element at position *x*, *y*.
Guilherme Polocda93aa2009-01-28 13:09:03 +0000989
990 Availability: Tk 8.6.
991
992
993 .. method:: index(item)
994
995 Returns the integer index of *item* within its parent's list of children.
996
997
998 .. method:: insert(parent, index[, iid=None[, **kw]])
999
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001000 Creates a new item and returns the item identifier of the newly created
Guilherme Polocda93aa2009-01-28 13:09:03 +00001001 item.
1002
1003 *parent* is the item ID of the parent item, or the empty string to create
1004 a new top-level item. *index* is an integer, or the value "end",
1005 specifying where in the list of parent's children to insert the new item.
1006 If *index* is less than or equal to zero, the new node is inserted at
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001007 the beginning; if *index* is greater than or equal to the current number
Guilherme Polocda93aa2009-01-28 13:09:03 +00001008 of children, it is inserted at the end. If *iid* is specified, it is used
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001009 as the item identifier; *iid* must not already exist in the tree.
Guilherme Polocda93aa2009-01-28 13:09:03 +00001010 Otherwise, a new unique identifier is generated.
1011
1012 See `Item Options`_ for the list of available points.
1013
1014
1015 .. method:: item(item[, option[, **kw]])
1016
1017 Query or modify the options for the specified *item*.
1018
1019 If no options are given, a dict with options/values for the item is
1020 returned.
1021 If *option* is specified then the value for that option is returned.
1022 Otherwise, sets the options to the corresponding values as given by *kw*.
1023
1024
1025 .. method:: move(item, parent, index)
1026
1027 Moves *item* to position *index* in *parent*'s list of children.
1028
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001029 It is illegal to move an item under one of its descendants. If *index* is
1030 less than or equal to zero, *item* is moved to the beginning; if greater
1031 than or equal to the number of children, it is moved to the end. If *item*
Guilherme Polocda93aa2009-01-28 13:09:03 +00001032 was detached it is reattached.
1033
1034
1035 .. method:: next(item)
1036
1037 Returns the identifier of *item*'s next sibling, or '' if *item* is the
1038 last child of its parent.
1039
1040
1041 .. method:: parent(item)
1042
1043 Returns the ID of the parent of *item*, or '' if *item* is at the top
1044 level of the hierarchy.
1045
1046
1047 .. method:: prev(item)
1048
1049 Returns the identifier of *item*'s previous sibling, or '' if *item* is
1050 the first child of its parent.
1051
1052
1053 .. method:: reattach(item, parent, index)
1054
1055 An alias for :meth:`Treeview.move`.
1056
1057
1058 .. method:: see(item)
1059
1060 Ensure that *item* is visible.
1061
1062 Sets all of *item*'s ancestors open option to True, and scrolls the
1063 widget if necessary so that *item* is within the visible portion of
1064 the tree.
1065
1066
1067 .. method:: selection([selop=None[, items=None]])
1068
1069 If *selop* is not specified, returns selected items. Otherwise, it will
1070 act according to the following selection methods.
1071
1072
1073 .. method:: selection_set(items)
1074
1075 *items* becomes the new selection.
1076
1077
1078 .. method:: selection_add(items)
1079
1080 Add *items* to the selection.
1081
1082
1083 .. method:: selection_remove(items)
1084
1085 Remove *items* from the selection.
1086
1087
1088 .. method:: selection_toggle(items)
1089
1090 Toggle the selection state of each item in *items*.
1091
1092
1093 .. method:: set(item[, column=None[, value=None]])
1094
1095 With one argument, returns a dictionary of column/value pairs for the
1096 specified *item*. With two arguments, returns the current value of the
1097 specified *column*. With three arguments, sets the value of given
1098 *column* in given *item* to the specified *value*.
1099
1100
1101 .. method:: tag_bind(tagname[, sequence=None[, callback=None]])
1102
1103 Bind a callback for the given event *sequence* to the tag *tagname*.
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001104 When an event is delivered to an item, the callbacks for each of the
Guilherme Polocda93aa2009-01-28 13:09:03 +00001105 item's tags option are called.
1106
1107
1108 .. method:: tag_configure(tagname[, option=None[, **kw]])
1109
1110 Query or modify the options for the specified *tagname*.
1111
1112 If *kw* is not given, returns a dict of the option settings for
1113 *tagname*. If *option* is specified, returns the value for that *option*
1114 for the specified *tagname*. Otherwise, sets the options to the
1115 corresponding values for the given *tagname*.
1116
1117
1118 .. method:: tag_has(tagname[, item])
1119
1120 If *item* is specified, returns 1 or 0 depending on whether the specified
1121 *item* has the given *tagname*. Otherwise, returns a list of all items
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001122 that have the specified tag.
Guilherme Polocda93aa2009-01-28 13:09:03 +00001123
1124 Availability: Tk 8.6
1125
1126
1127 .. method:: xview(*args)
1128
1129 Query or modify horizontal position of the treeview.
1130
1131
1132 .. method:: yview(*args)
1133
1134 Query or modify vertical position of the treeview.
1135
1136
1137.. _TtkStyling:
1138
1139Ttk Styling
1140-----------
1141
1142Each widget in :mod:`ttk` is assigned a style, which specifies the set of
Benjamin Peterson288618e2009-03-03 22:51:57 +00001143elements making up the widget and how they are arranged, along with dynamic and
1144default settings for element options. By default the style name is the same as
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001145the widget's class name, but it may be overridden by the widget's style
1146option. If the class name of a widget is unknown, use the method
Guilherme Polocda93aa2009-01-28 13:09:03 +00001147:meth:`Misc.winfo_class` (somewidget.winfo_class()).
1148
1149.. seealso::
1150
1151 `Tcl'2004 conference presentation <http://tktable.sourceforge.net/tile/tile-tcl2004.pdf>`_
1152 This document explains how the theme engine works
1153
1154
1155.. class:: Style
1156
1157 This class is used to manipulate the style database.
1158
1159
1160 .. method:: configure(style, query_opt=None, **kw)
1161
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001162 Query or set the default value of the specified option(s) in *style*.
Guilherme Polocda93aa2009-01-28 13:09:03 +00001163
1164 Each key in *kw* is an option and each value is a string identifying
1165 the value for that option.
1166
Benjamin Peterson288618e2009-03-03 22:51:57 +00001167 For example, to change every default button to be a flat button with some
1168 padding and a different background color do::
Guilherme Polocda93aa2009-01-28 13:09:03 +00001169
1170 import ttk
1171 import Tkinter
1172
1173 root = Tkinter.Tk()
1174
1175 ttk.Style().configure("TButton", padding=6, relief="flat",
1176 background="#ccc")
1177
1178 btn = ttk.Button(text="Sample")
1179 btn.pack()
1180
1181 root.mainloop()
1182
1183
1184 .. method:: map(style, query_opt=None, **kw)
1185
1186 Query or sets dynamic values of the specified option(s) in *style*.
1187
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001188 Each key in *kw* is an option and each value should be a list or a
1189 tuple (usually) containing statespecs grouped in tuples, lists, or
1190 something else of your preference. A statespec is a compound of one
1191 or more states and then a value.
Guilherme Polocda93aa2009-01-28 13:09:03 +00001192
Benjamin Peterson288618e2009-03-03 22:51:57 +00001193 An example::
Guilherme Polocda93aa2009-01-28 13:09:03 +00001194
1195 import Tkinter
1196 import ttk
1197
1198 root = Tkinter.Tk()
1199
1200 style = ttk.Style()
1201 style.map("C.TButton",
1202 foreground=[('pressed', 'red'), ('active', 'blue')],
1203 background=[('pressed', '!disabled', 'black'), ('active', 'white')]
1204 )
1205
1206 colored_btn = ttk.Button(text="Test", style="C.TButton").pack()
1207
1208 root.mainloop()
1209
1210
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001211 Note that the order of the (states, value) sequences for an
1212 option matters. In the previous example, if you change the
1213 order to ``[('active', 'blue'), ('pressed', 'red')]`` in the
1214 foreground option, for example, you would get a blue foreground
1215 when the widget is in the active or pressed states.
Guilherme Polocda93aa2009-01-28 13:09:03 +00001216
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
Benjamin Peterson288618e2009-03-03 22:51:57 +00001225 To check what font a Button uses by default, do::
Guilherme Polocda93aa2009-01-28 13:09:03 +00001226
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
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001237 *layoutspec*, if specified, is expected to be a list or some other
1238 sequence type (excluding strings), where each item should be a tuple and
Guilherme Polocda93aa2009-01-28 13:09:03 +00001239 the first item is the layout name and the second item should have the
1240 format described described in `Layouts`_.
1241
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001242 To understand the format, see the following example (it is not
1243 intended to do anything useful)::
Guilherme Polocda93aa2009-01-28 13:09:03 +00001244
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
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001269 Create a new element in the current theme, of the given *etype* which is
Guilherme Polocda93aa2009-01-28 13:09:03 +00001270 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
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001274 by statespec/value pairs (this is the imagespec), and *kw* may have the
Guilherme Polocda93aa2009-01-28 13:09:03 +00001275 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
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001297 If "from" is used as the value of *etype*,
1298 :meth:`element_create` will clone an existing
1299 element. *args* is expected to contain a themename, from which
Guilherme Polocda93aa2009-01-28 13:09:03 +00001300 the element will be cloned, and optionally an element to clone from.
1301 If this element to clone from is not specified, an empty element will
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001302 be used. *kw* is discarded.
Guilherme Polocda93aa2009-01-28 13:09:03 +00001303
1304
1305 .. method:: element_names()
1306
1307 Returns the list of elements defined in the current theme.
1308
1309
1310 .. method:: element_options(elementname)
1311
1312 Returns the list of *elementname*'s options.
1313
1314
1315 .. method:: theme_create(themename[, parent=None[, settings=None]])
1316
1317 Create a new theme.
1318
1319 It is an error if *themename* already exists. If *parent* is specified,
1320 the new theme will inherit styles, elements and layouts from the parent
1321 theme. If *settings* are present they are expected to have the same
1322 syntax used for :meth:`theme_settings`.
1323
1324
1325 .. method:: theme_settings(themename, settings)
1326
1327 Temporarily sets the current theme to *themename*, apply specified
1328 *settings* and then restore the previous theme.
1329
1330 Each key in *settings* is a style and each value may contain the keys
1331 'configure', 'map', 'layout' and 'element create' and they are expected
1332 to have the same format as specified by the methods
1333 :meth:`Style.configure`, :meth:`Style.map`, :meth:`Style.layout` and
1334 :meth:`Style.element_create` respectively.
1335
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001336 As an example, let's change the Combobox for the default theme a bit::
Guilherme Polocda93aa2009-01-28 13:09:03 +00001337
1338 import ttk
1339 import Tkinter
1340
1341 root = Tkinter.Tk()
1342
1343 style = ttk.Style()
1344 style.theme_settings("default", {
1345 "TCombobox": {
1346 "configure": {"padding": 5},
1347 "map": {
1348 "background": [("active", "green2"),
1349 ("!disabled", "green4")],
1350 "fieldbackground": [("!disabled", "green3")],
1351 "foreground": [("focus", "OliveDrab1"),
1352 ("!disabled", "OliveDrab2")]
1353 }
1354 }
1355 })
1356
1357 combo = ttk.Combobox().pack()
1358
1359 root.mainloop()
1360
1361
1362 .. method:: theme_names()
1363
1364 Returns a list of all known themes.
1365
1366
1367 .. method:: theme_use([themename])
1368
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001369 If *themename* is not given, returns the theme in use. Otherwise, sets
Guilherme Polocda93aa2009-01-28 13:09:03 +00001370 the current theme to *themename*, refreshes all widgets and emits a
1371 <<ThemeChanged>> event.
1372
1373
1374Layouts
1375^^^^^^^
1376
Andrew M. Kuchling55acfc62009-03-30 22:31:11 +00001377A layout can be just None, if it takes no options, or a dict of
1378options specifying how to arrange the element. The layout mechanism
1379uses a simplified version of the pack geometry manager: given an
1380initial cavity, each element is allocated a parcel. Valid
1381options/values are:
Guilherme Polocda93aa2009-01-28 13:09:03 +00001382
1383 * side: whichside
Andrew M. Kuchlinga178a692009-04-03 21:45:29 +00001384 Specifies which side of the cavity to place the element; one of
Guilherme Polocda93aa2009-01-28 13:09:03 +00001385 top, right, bottom or left. If omitted, the element occupies the
1386 entire cavity.
1387
1388 * sticky: nswe
1389 Specifies where the element is placed inside its allocated parcel.
1390
1391 * unit: 0 or 1
1392 If set to 1, causes the element and all of its descendants to be treated as
1393 a single element for the purposes of :meth:`Widget.identify` et al. It's
1394 used for things like scrollbar thumbs with grips.
1395
1396 * children: [sublayout... ]
1397 Specifies a list of elements to place inside the element. Each
1398 element is a tuple (or other sequence type) where the first item is
1399 the layout name, and the other is a `Layout`_.
1400
1401.. _Layout: `Layouts`_