mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 23:14:06 +08:00
82 lines
103 KiB
HTML
82 lines
103 KiB
HTML
<div class="body" role="main"><div class="section" id="module-tkinter.ttk"><h1><span class="yiyi-st" id="yiyi-10">25.2. <a class="reference internal" href="#module-tkinter.ttk" title="tkinter.ttk: Tk themed widget set"><code class="xref py py-mod docutils literal"><span class="pre">tkinter.ttk</span></code></a> — Tk themed widgets</span></h1><p><span class="yiyi-st" id="yiyi-11"><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/tkinter/ttk.py">Lib/tkinter/ttk.py</a></span></p><p><span class="yiyi-st" id="yiyi-12">The <a class="reference internal" href="#module-tkinter.ttk" title="tkinter.ttk: Tk themed widget set"><code class="xref py py-mod docutils literal"><span class="pre">tkinter.ttk</span></code></a> module provides access to the Tk themed widget set, introduced in Tk 8.5. </span><span class="yiyi-st" id="yiyi-13">If Python has not been compiled against Tk 8.5, this module can still be accessed if <em>Tile</em> has been installed. </span><span class="yiyi-st" id="yiyi-14">使用Tk 8.5的前一种方法提供了额外的好处,包括在X11和窗口透明性下的反别名字体渲染(需要在X11上使用组合窗口管理器)。</span></p><p><span class="yiyi-st" id="yiyi-15">The basic idea for <a class="reference internal" href="#module-tkinter.ttk" title="tkinter.ttk: Tk themed widget set"><code class="xref py py-mod docutils literal"><span class="pre">tkinter.ttk</span></code></a> is to separate, to the extent possible, the code implementing a widget’s behavior from the code implementing its appearance.</span></p><div class="admonition seealso"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-16">See also</span></p><dl class="last docutils"><dt><span class="yiyi-st" id="yiyi-17"><a class="reference external" href="https://www.tcl.tk/cgi-bin/tct/tip/48">Tk Widget Styling Support</a></span></dt><dd><span class="yiyi-st" id="yiyi-18">A document introducing theming support for Tk</span></dd></dl></div><div class="section" id="using-ttk"><h2><span class="yiyi-st" id="yiyi-19">25.2.1. </span><span class="yiyi-st" id="yiyi-20">Using Ttk</span></h2><p><span class="yiyi-st" id="yiyi-21">To start using Ttk, import its module:</span></p><pre><code class="language-python"><span></span><span class="kn">from</span> <span class="nn">tkinter</span> <span class="k">import</span> <span class="n">ttk</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-22">To override the basic Tk widgets, the import should follow the Tk import:</span></p><pre><code class="language-python"><span></span><span class="kn">from</span> <span class="nn">tkinter</span> <span class="k">import</span> <span class="o">*</span>
|
||
<span class="kn">from</span> <span class="nn">tkinter.ttk</span> <span class="k">import</span> <span class="o">*</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-23">该代码将导致几个 <a class="reference internal" href="#module-tkinter.ttk" title="tkinter.ttk: Tk themed widget set"><code class="xref py py-mod docutils literal"><span class="pre">tkinter.ttk</span></code></a> 小部件 (<code class="xref py py-class docutils literal"><span class="pre">Button</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Checkbutton</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Entry</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Frame</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Label</span></code>, <code class="xref py py-class docutils literal"><span class="pre">LabelFrame</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Menubutton</span></code>, <code class="xref py py-class docutils literal"><span class="pre">PanedWindow</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Radiobutton</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Scale</span></code> and <code class="xref py py-class docutils literal"><span class="pre">Scrollbar</span></code>) 自动替换为 Tk 小部件.</span></p><p><span class="yiyi-st" id="yiyi-24">This has the direct benefit of using the new widgets which gives a better look and feel across platforms; however, the replacement widgets are not completely compatible. </span><span class="yiyi-st" id="yiyi-25">The main difference is that widget options such as “fg”, “bg” and others related to widget styling are no longer present in Ttk widgets. </span><span class="yiyi-st" id="yiyi-26">Instead, use the <code class="xref py py-class docutils literal"><span class="pre">ttk.Style</span></code> class for improved styling effects.</span></p><div class="admonition seealso"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-27">See also</span></p><dl class="last docutils"><dt><span class="yiyi-st" id="yiyi-28"><a class="reference external" href="http://tktable.sourceforge.net/tile/doc/converting.txt">Converting existing applications to use Tile widgets</a></span></dt><dd><span class="yiyi-st" id="yiyi-29">A monograph (using Tcl terminology) about differences typically encountered when moving applications to use the new widgets.</span></dd></dl></div></div><div class="section" id="ttk-widgets"><h2><span class="yiyi-st" id="yiyi-30">25.2.2. </span><span class="yiyi-st" id="yiyi-31">Ttk Widgets</span></h2><p><span class="yiyi-st" id="yiyi-32">Ttk有17个小部件,其中11个已经存在于tkinter: <code class="xref py py-class docutils literal"><span class="pre">Button</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Checkbutton</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Entry</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Frame</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Label</span></code>, <code class="xref py py-class docutils literal"><span class="pre">LabelFrame</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Menubutton</span></code>, <code class="xref py py-class docutils literal"><span class="pre">PanedWindow</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Radiobutton</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Scale</span></code> and <code class="xref py py-class docutils literal"><span class="pre">Scrollbar</span></code>. </span><span class="yiyi-st" id="yiyi-33">The other six are new: <a class="reference internal" href="#tkinter.ttk.Combobox" title="tkinter.ttk.Combobox"><code class="xref py py-class docutils literal"><span class="pre">Combobox</span></code></a>, <a class="reference internal" href="#tkinter.ttk.Notebook" title="tkinter.ttk.Notebook"><code class="xref py py-class docutils literal"><span class="pre">Notebook</span></code></a>, <a class="reference internal" href="#tkinter.ttk.Progressbar" title="tkinter.ttk.Progressbar"><code class="xref py py-class docutils literal"><span class="pre">Progressbar</span></code></a>, <code class="xref py py-class docutils literal"><span class="pre">Separator</span></code>, <code class="xref py py-class docutils literal"><span class="pre">Sizegrip</span></code> and <a class="reference internal" href="#tkinter.ttk.Treeview" title="tkinter.ttk.Treeview"><code class="xref py py-class docutils literal"><span class="pre">Treeview</span></code></a>. </span><span class="yiyi-st" id="yiyi-34">And all them are subclasses of <a class="reference internal" href="#tkinter.ttk.Widget" title="tkinter.ttk.Widget"><code class="xref py py-class docutils literal"><span class="pre">Widget</span></code></a>.</span></p><p><span class="yiyi-st" id="yiyi-35">Using the Ttk widgets gives the application an improved look and feel. </span><span class="yiyi-st" id="yiyi-36">As discussed above, there are differences in how the styling is coded.</span></p><p><span class="yiyi-st" id="yiyi-37">Tk code:</span></p><pre><code class="language-python"><span></span><span class="n">l1</span> <span class="o">=</span> <span class="n">tkinter</span><span class="o">.</span><span class="n">Label</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="s2">"Test"</span><span class="p">,</span> <span class="n">fg</span><span class="o">=</span><span class="s2">"black"</span><span class="p">,</span> <span class="n">bg</span><span class="o">=</span><span class="s2">"white"</span><span class="p">)</span>
|
||
<span class="n">l2</span> <span class="o">=</span> <span class="n">tkinter</span><span class="o">.</span><span class="n">Label</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="s2">"Test"</span><span class="p">,</span> <span class="n">fg</span><span class="o">=</span><span class="s2">"black"</span><span class="p">,</span> <span class="n">bg</span><span class="o">=</span><span class="s2">"white"</span><span class="p">)</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-38">Ttk code:</span></p><pre><code class="language-python"><span></span><span class="n">style</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Style</span><span class="p">()</span>
|
||
<span class="n">style</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="s2">"BW.TLabel"</span><span class="p">,</span> <span class="n">foreground</span><span class="o">=</span><span class="s2">"black"</span><span class="p">,</span> <span class="n">background</span><span class="o">=</span><span class="s2">"white"</span><span class="p">)</span>
|
||
|
||
<span class="n">l1</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Label</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="s2">"Test"</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="s2">"BW.TLabel"</span><span class="p">)</span>
|
||
<span class="n">l2</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Label</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="s2">"Test"</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="s2">"BW.TLabel"</span><span class="p">)</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-39">For more information about <a class="reference internal" href="#ttkstyling">TtkStyling</a>, see the <a class="reference internal" href="#tkinter.ttk.Style" title="tkinter.ttk.Style"><code class="xref py py-class docutils literal"><span class="pre">Style</span></code></a> class documentation.</span></p></div><div class="section" id="widget"><h2><span class="yiyi-st" id="yiyi-40">25.2.3. </span><span class="yiyi-st" id="yiyi-41">Widget</span></h2><p><span class="yiyi-st" id="yiyi-42"><code class="xref py py-class docutils literal"><span class="pre">ttk.Widget</span></code>定义了Tk主题小部件支持的标准选项和方法,不应该直接实例化。</span></p><div class="section" id="standard-options"><h3><span class="yiyi-st" id="yiyi-43">25.2.3.1. </span><span class="yiyi-st" id="yiyi-44">Standard Options</span></h3><p><span class="yiyi-st" id="yiyi-45">All the <code class="xref py py-mod docutils literal"><span class="pre">ttk</span></code> Widgets accepts the following options:</span></p><span class="yiyi-st" id="yiyi-46"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="15%"/> <col width="85%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>class</td> <td>Specifies the window class. The class is used when querying the option database for the window’s other options, to determine the default bindtags for the window, and to select the widget’s default layout and style. This option is read-only, and may only be specified when the window is created.</td> </tr> <tr class="row-odd"><td>cursor</td> <td>Specifies the mouse cursor to be used for the widget. If set to the empty string (the default), the cursor is inherited for the parent widget.</td> </tr> <tr class="row-even"><td>takefocus</td> <td>Determines whether the window accepts the focus during keyboard traversal. 0, 1 or an empty string is returned. If 0 is returned, it means that the window should be skipped entirely during keyboard traversal. If 1, it means that the window should receive the input focus as long as it is viewable. And an empty string means that the traversal scripts make the decision about whether or not to focus on the window.</td> </tr> <tr class="row-odd"><td>style</td> <td>May be used to specify a custom widget style.</td> </tr> </tbody> </table> </div></blockquote></span></div><div class="section" id="scrollable-widget-options"><h3><span class="yiyi-st" id="yiyi-47">25.2.3.2. </span><span class="yiyi-st" id="yiyi-48">Scrollable Widget Options</span></h3><p><span class="yiyi-st" id="yiyi-49">The following options are supported by widgets that are controlled by a scrollbar.</span></p><span class="yiyi-st" id="yiyi-50"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="22%"/> <col width="78%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>xscrollcommand</td> <td><p class="first">Used to communicate with horizontal scrollbars.</p> <p>When the view in the widget’s window change, the widget will generate a Tcl command based on the scrollcommand.</p> <p class="last">Usually this option consists of the method <code class="xref py py-meth docutils literal"><span class="pre">Scrollbar.set()</span></code> of some scrollbar. This will cause the scrollbar to be updated whenever the view in the window changes.</p> </td> </tr> <tr class="row-odd"><td>yscrollcommand</td> <td>Used to communicate with vertical scrollbars. For some more information, see above.</td> </tr> </tbody> </table> </div></blockquote></span></div><div class="section" id="label-options"><h3><span class="yiyi-st" id="yiyi-51">25.2.3.3. </span><span class="yiyi-st" id="yiyi-52">Label Options</span></h3><p><span class="yiyi-st" id="yiyi-53">The following options are supported by labels, buttons and other button-like widgets.</span></p><span class="yiyi-st" id="yiyi-54"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="19%"/> <col width="81%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>text</td> <td>Specifies a text string to be displayed inside the widget.</td> </tr> <tr class="row-odd"><td>textvariable</td> <td>Specifies a name whose value will be used in place of the text option resource.</td> </tr> <tr class="row-even"><td>underline</td> <td>If set, specifies the index (0-based) of a character to underline in the text string. The underline character is used for mnemonic activation.</td> </tr> <tr class="row-odd"><td>image</td> <td>Specifies an image to display. This is a list of 1 or more elements. The first element is the default image name. The rest of the list if a sequence of statespec/value pairs as defined by <a class="reference internal" href="#tkinter.ttk.Style.map" title="tkinter.ttk.Style.map"><code class="xref py py-meth docutils literal"><span class="pre">Style.map()</span></code></a>, specifying different images to use when the widget is in a particular state or a combination of states. All images in the list should have the same size.</td> </tr> <tr class="row-even"><td>compound</td> <td><p class="first">Specifies how to display the image relative to the text, in the case both text and images options are present. Valid values are:</p> <ul class="last simple"> <li>text: display text only</li> <li>image: display image only</li> <li>top, bottom, left, right: display image above, below, left of, or right of the text, respectively.</li> <li>none: the default. display the image if present, otherwise the text.</li> </ul> </td> </tr> <tr class="row-odd"><td>width</td> <td>If greater than zero, specifies how much space, in character widths, to allocate for the text label, if less than zero, specifies a minimum width. If zero or unspecified, the natural width of the text label is used.</td> </tr> </tbody> </table> </div></blockquote></span></div><div class="section" id="compatibility-options"><h3><span class="yiyi-st" id="yiyi-55">25.2.3.4. </span><span class="yiyi-st" id="yiyi-56">Compatibility Options</span></h3><span class="yiyi-st" id="yiyi-57"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="11%"/> <col width="89%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>state</td> <td>May be set to “normal” or “disabled” to control the “disabled” state bit. This is a write-only option: setting it changes the widget state, but the <a class="reference internal" href="#tkinter.ttk.Widget.state" title="tkinter.ttk.Widget.state"><code class="xref py py-meth docutils literal"><span class="pre">Widget.state()</span></code></a> method does not affect this option.</td> </tr> </tbody> </table> </div></blockquote></span></div><div class="section" id="widget-states"><h3><span class="yiyi-st" id="yiyi-58">25.2.3.5. </span><span class="yiyi-st" id="yiyi-59">Widget States</span></h3><p><span class="yiyi-st" id="yiyi-60">The widget state is a bitmap of independent state flags.</span></p><span class="yiyi-st" id="yiyi-62"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="16%"/> <col width="84%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Flag</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>active</td> <td>The mouse cursor is over the widget and pressing a mouse button will cause some action to occur</td> </tr> <tr class="row-odd"><td>disabled</td> <td>Widget is disabled under program control</td> </tr> <tr class="row-even"><td>focus</td> <td>Widget has keyboard focus</td> </tr> <tr class="row-odd"><td>pressed</td> <td>Widget is being pressed</td> </tr> <tr class="row-even"><td>selected</td> <td>“On”, “true”, or “current” for things like Checkbuttons and radiobuttons</td> </tr> <tr class="row-odd"><td>background</td> <td>Windows and Mac have a notion of an “active” or foreground window. The <em>background</em> state is set for widgets in a background window, and cleared for those in the foreground window</td> </tr> <tr class="row-even"><td>readonly</td> <td>Widget should not allow user modification</td> </tr> <tr class="row-odd"><td>alternate</td> <td>A widget-specific alternate display format</td> </tr> <tr class="row-even"><td>invalid</td> <td>The widget’s value is invalid</td> </tr> </tbody> </table> </div></blockquote></span><p><span class="yiyi-st" id="yiyi-61">A state specification is a sequence of state names, optionally prefixed with an exclamation point indicating that the bit is off.</span></p></div><div class="section" id="ttk-widget"><h3><span class="yiyi-st" id="yiyi-63">25.2.3.6. ttk.Widget</span></h3><p><span class="yiyi-st" id="yiyi-64">Besides the methods described below, the <code class="xref py py-class docutils literal"><span class="pre">ttk.Widget</span></code> supports the methods <code class="xref py py-meth docutils literal"><span class="pre">tkinter.Widget.cget()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">tkinter.Widget.configure()</span></code>.</span></p><dl class="class"><dt id="tkinter.ttk.Widget"><span class="yiyi-st" id="yiyi-65"> <em class="property">class </em><code class="descclassname">tkinter.ttk.</code><code class="descname">Widget</code></span></dt><dd><dl class="method"><dt id="tkinter.ttk.Widget.identify"><span class="yiyi-st" id="yiyi-66"> <code class="descname">identify</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-67">Returns the name of the element at position <em>x</em> <em>y</em>, or the empty string if the point does not lie within any element.</span></p><p><span class="yiyi-st" id="yiyi-68"><em>x</em> and <em>y</em> are pixel coordinates relative to the widget.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Widget.instate"><span class="yiyi-st" id="yiyi-69"> <code class="descname">instate</code><span class="sig-paren">(</span><em>statespec</em>, <em>callback=None</em>, <em>*args</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-70">Test the widget’s state. </span><span class="yiyi-st" id="yiyi-71">If a callback is not specified, returns <code class="docutils literal"><span class="pre">True</span></code> if the widget state matches <em>statespec</em> and <code class="docutils literal"><span class="pre">False</span></code> otherwise. </span><span class="yiyi-st" id="yiyi-72">If callback is specified then it is called with args if widget state matches <em>statespec</em>.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Widget.state"><span class="yiyi-st" id="yiyi-73"> <code class="descname">state</code><span class="sig-paren">(</span><em>statespec=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-74">Modify or inquire widget state. </span><span class="yiyi-st" id="yiyi-75">If <em>statespec</em> is specified, sets the widget state according to it and return a new <em>statespec</em> indicating which flags were changed. </span><span class="yiyi-st" id="yiyi-76">If <em>statespec</em> is not specified, returns the currently-enabled state flags.</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-77"><em>statespec</em> will usually be a list or a tuple.</span></p></dd></dl></div></div><div class="section" id="combobox"><h2><span class="yiyi-st" id="yiyi-78">25.2.4. </span><span class="yiyi-st" id="yiyi-79">Combobox</span></h2><p><span class="yiyi-st" id="yiyi-80">The <code class="xref py py-class docutils literal"><span class="pre">ttk.Combobox</span></code> widget combines a text field with a pop-down list of values. </span><span class="yiyi-st" id="yiyi-81">This widget is a subclass of <code class="xref py py-class docutils literal"><span class="pre">Entry</span></code>.</span></p><p><span class="yiyi-st" id="yiyi-82">除了从<a class="reference internal" href="#tkinter.ttk.Widget" title="tkinter.ttk.Widget"><code class="xref py py-class docutils literal"><span class="pre">Widget</span></code></a>继承的方法: <code class="xref py py-meth docutils literal"><span class="pre">Widget.cget()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">Widget.configure()</span></code>, <a class="reference internal" href="#tkinter.ttk.Widget.identify" title="tkinter.ttk.Widget.identify"><code class="xref py py-meth docutils literal"><span class="pre">Widget.identify()</span></code></a>, <a class="reference internal" href="#tkinter.ttk.Widget.instate" title="tkinter.ttk.Widget.instate"><code class="xref py py-meth docutils literal"><span class="pre">Widget.instate()</span></code></a> 和 <a class="reference internal" href="#tkinter.ttk.Widget.state" title="tkinter.ttk.Widget.state"><code class="xref py py-meth docutils literal"><span class="pre">Widget.state()</span></code></a>, 和以下继承自 <code class="xref py py-class docutils literal"><span class="pre">Entry</span></code>: <code class="xref py py-meth docutils literal"><span class="pre">Entry.bbox()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">Entry.delete()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">Entry.icursor()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">Entry.index()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">Entry.insert()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">Entry.selection()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">Entry.xview()</span></code>, 它还有一些其他的方法,在 <code class="xref py py-class docutils literal"><span class="pre">ttk.Combobox</span></code>中描述。</span></p><div class="section" id="options"><h3><span class="yiyi-st" id="yiyi-83">25.2.4.1. </span><span class="yiyi-st" id="yiyi-84">Options</span></h3><p><span class="yiyi-st" id="yiyi-85">This widget accepts the following specific options:</span></p><span class="yiyi-st" id="yiyi-86"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="23%"/> <col width="77%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>exportselection</td> <td>Boolean value. If set, the widget selection is linked to the Window Manager selection (which can be returned by invoking Misc.selection_get, for example).</td> </tr> <tr class="row-odd"><td>justify</td> <td>Specifies how the text is aligned within the widget. One of “left”, “center”, or “right”.</td> </tr> <tr class="row-even"><td>height</td> <td>Specifies the height of the pop-down listbox, in rows.</td> </tr> <tr class="row-odd"><td>postcommand</td> <td>A script (possibly registered with Misc.register) that is called immediately before displaying the values. It may specify which values to display.</td> </tr> <tr class="row-even"><td>state</td> <td>One of “normal”, “readonly”, or “disabled”. In the “readonly” state, the value may not be edited directly, and the user can only selection of the values from the dropdown list. In the “normal” state, the text field is directly editable. In the “disabled” state, no interaction is possible.</td> </tr> <tr class="row-odd"><td>textvariable</td> <td>Specifies a name whose value is linked to the widget value. Whenever the value associated with that name changes, the widget value is updated, and vice versa. See <code class="xref py py-class docutils literal"><span class="pre">tkinter.StringVar</span></code>.</td> </tr> <tr class="row-even"><td>values</td> <td>Specifies the list of values to display in the drop-down listbox.</td> </tr> <tr class="row-odd"><td>width</td> <td>Specifies an integer value indicating the desired width of the entry window, in average-size characters of the widget’s font.</td> </tr> </tbody> </table> </div></blockquote></span></div><div class="section" id="virtual-events"><h3><span class="yiyi-st" id="yiyi-87">25.2.4.2. </span><span class="yiyi-st" id="yiyi-88">Virtual events</span></h3><p><span class="yiyi-st" id="yiyi-89">The combobox widgets generates a <strong><<ComboboxSelected>></strong> virtual event when the user selects an element from the list of values.</span></p></div><div class="section" id="ttk-combobox"><h3><span class="yiyi-st" id="yiyi-90">25.2.4.3. ttk.Combobox</span></h3><dl class="class"><dt id="tkinter.ttk.Combobox"><span class="yiyi-st" id="yiyi-91"> <em class="property">class </em><code class="descclassname">tkinter.ttk.</code><code class="descname">Combobox</code></span></dt><dd><dl class="method"><dt id="tkinter.ttk.Combobox.current"><span class="yiyi-st" id="yiyi-92"> <code class="descname">current</code><span class="sig-paren">(</span><em>newindex=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-93">If <em>newindex</em> is specified, sets the combobox value to the element position <em>newindex</em>. </span><span class="yiyi-st" id="yiyi-94">Otherwise, returns the index of the current value or -1 if the current value is not in the values list.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Combobox.get"><span class="yiyi-st" id="yiyi-95"> <code class="descname">get</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-96">Returns the current value of the combobox.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Combobox.set"><span class="yiyi-st" id="yiyi-97"> <code class="descname">set</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-98">Sets the value of the combobox to <em>value</em>.</span></p></dd></dl></dd></dl></div></div><div class="section" id="notebook"><h2><span class="yiyi-st" id="yiyi-99">25.2.5. </span><span class="yiyi-st" id="yiyi-100">Notebook</span></h2><p><span class="yiyi-st" id="yiyi-101">Ttk Notebook widget manages a collection of windows and displays a single one at a time. </span><span class="yiyi-st" id="yiyi-102">Each child window is associated with a tab, which the user may select to change the currently-displayed window.</span></p><div class="section" id="id1"><h3><span class="yiyi-st" id="yiyi-103">25.2.5.1. </span><span class="yiyi-st" id="yiyi-104">Options</span></h3><p><span class="yiyi-st" id="yiyi-105">This widget accepts the following specific options:</span></p><span class="yiyi-st" id="yiyi-106"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="12%"/> <col width="88%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>height</td> <td>If present and greater than zero, specifies the desired height of the pane area (not including internal padding or tabs). Otherwise, the maximum height of all panes is used.</td> </tr> <tr class="row-odd"><td>padding</td> <td>Specifies the amount of extra space to add around the outside of the notebook. The padding is a list up to four length specifications left top right bottom. If fewer than four elements are specified, bottom defaults to top, right defaults to left, and top defaults to left.</td> </tr> <tr class="row-even"><td>width</td> <td>If present and greater than zero, specified the desired width of the pane area (not including internal padding). Otherwise, the maximum width of all panes is used.</td> </tr> </tbody> </table> </div></blockquote></span></div><div class="section" id="tab-options"><h3><span class="yiyi-st" id="yiyi-107">25.2.5.2. </span><span class="yiyi-st" id="yiyi-108">Tab Options</span></h3><p><span class="yiyi-st" id="yiyi-109">There are also specific options for tabs:</span></p><span class="yiyi-st" id="yiyi-110"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="15%"/> <col width="85%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>state</td> <td>Either “normal”, “disabled” or “hidden”. If “disabled”, then the tab is not selectable. If “hidden”, then the tab is not shown.</td> </tr> <tr class="row-odd"><td>sticky</td> <td>Specifies how the child window is positioned within the pane area. Value is a string containing zero or more of the characters “n”, “s”, “e” or “w”. Each letter refers to a side (north, south, east or west) that the child window will stick to, as per the <code class="xref py py-meth docutils literal"><span class="pre">grid()</span></code> geometry manager.</td> </tr> <tr class="row-even"><td>padding</td> <td>Specifies the amount of extra space to add between the notebook and this pane. Syntax is the same as for the option padding used by this widget.</td> </tr> <tr class="row-odd"><td>text</td> <td>Specifies a text to be displayed in the tab.</td> </tr> <tr class="row-even"><td>image</td> <td>Specifies an image to display in the tab. See the option image described in <a class="reference internal" href="#tkinter.ttk.Widget" title="tkinter.ttk.Widget"><code class="xref py py-class docutils literal"><span class="pre">Widget</span></code></a>.</td> </tr> <tr class="row-odd"><td>compound</td> <td>Specifies how to display the image relative to the text, in the case both options text and image are present. See <a class="reference internal" href="#label-options">Label Options</a> for legal values.</td> </tr> <tr class="row-even"><td>underline</td> <td>Specifies the index (0-based) of a character to underline in the text string. The underlined character is used for mnemonic activation if <a class="reference internal" href="#tkinter.ttk.Notebook.enable_traversal" title="tkinter.ttk.Notebook.enable_traversal"><code class="xref py py-meth docutils literal"><span class="pre">Notebook.enable_traversal()</span></code></a> is called.</td> </tr> </tbody> </table> </div></blockquote></span></div><div class="section" id="tab-identifiers"><h3><span class="yiyi-st" id="yiyi-111">25.2.5.3. </span><span class="yiyi-st" id="yiyi-112">Tab Identifiers</span></h3><p><span class="yiyi-st" id="yiyi-113">The tab_id present in several methods of <code class="xref py py-class docutils literal"><span class="pre">ttk.Notebook</span></code> may take any of the following forms:</span></p><ul class="simple"><li><span class="yiyi-st" id="yiyi-114">An integer between zero and the number of tabs</span></li><li><span class="yiyi-st" id="yiyi-115">The name of a child window</span></li><li><span class="yiyi-st" id="yiyi-116">A positional specification of the form “@x,y”, which identifies the tab</span></li><li><span class="yiyi-st" id="yiyi-117">The literal string “current”, which identifies the currently-selected tab</span></li><li><span class="yiyi-st" id="yiyi-118">The literal string “end”, which returns the number of tabs (only valid for <a class="reference internal" href="#tkinter.ttk.Notebook.index" title="tkinter.ttk.Notebook.index"><code class="xref py py-meth docutils literal"><span class="pre">Notebook.index()</span></code></a>)</span></li></ul></div><div class="section" id="id2"><h3><span class="yiyi-st" id="yiyi-119">25.2.5.4. </span><span class="yiyi-st" id="yiyi-120">Virtual Events</span></h3><p><span class="yiyi-st" id="yiyi-121">This widget generates a <strong><<NotebookTabChanged>></strong> virtual event after a new tab is selected.</span></p></div><div class="section" id="ttk-notebook"><h3><span class="yiyi-st" id="yiyi-122">25.2.5.5. ttk.Notebook</span></h3><dl class="class"><dt id="tkinter.ttk.Notebook"><span class="yiyi-st" id="yiyi-123"> <em class="property">class </em><code class="descclassname">tkinter.ttk.</code><code class="descname">Notebook</code></span></dt><dd><dl class="method"><dt id="tkinter.ttk.Notebook.add"><span class="yiyi-st" id="yiyi-124"> <code class="descname">add</code><span class="sig-paren">(</span><em>child</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-125">Adds a new tab to the notebook.</span></p><p><span class="yiyi-st" id="yiyi-126">If window is currently managed by the notebook but hidden, it is restored to its previous position.</span></p><p><span class="yiyi-st" id="yiyi-127">See <a class="reference internal" href="#tab-options">Tab Options</a> for the list of available options.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Notebook.forget"><span class="yiyi-st" id="yiyi-128"> <code class="descname">forget</code><span class="sig-paren">(</span><em>tab_id</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-129">Removes the tab specified by <em>tab_id</em>, unmaps and unmanages the associated window.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Notebook.hide"><span class="yiyi-st" id="yiyi-130"> <code class="descname">hide</code><span class="sig-paren">(</span><em>tab_id</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-131">Hides the tab specified by <em>tab_id</em>.</span></p><p><span class="yiyi-st" id="yiyi-132">The tab will not be displayed, but the associated window remains managed by the notebook and its configuration remembered. </span><span class="yiyi-st" id="yiyi-133">Hidden tabs may be restored with the <a class="reference internal" href="#tkinter.ttk.Notebook.add" title="tkinter.ttk.Notebook.add"><code class="xref py py-meth docutils literal"><span class="pre">add()</span></code></a> command.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Notebook.identify"><span class="yiyi-st" id="yiyi-134"> <code class="descname">identify</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-135">Returns the name of the tab element at position <em>x</em>, <em>y</em>, or the empty string if none.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Notebook.index"><span class="yiyi-st" id="yiyi-136"> <code class="descname">index</code><span class="sig-paren">(</span><em>tab_id</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-137">Returns the numeric index of the tab specified by <em>tab_id</em>, or the total number of tabs if <em>tab_id</em> is the string “end”.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Notebook.insert"><span class="yiyi-st" id="yiyi-138"> <code class="descname">insert</code><span class="sig-paren">(</span><em>pos</em>, <em>child</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-139">Inserts a pane at the specified position.</span></p><p><span class="yiyi-st" id="yiyi-140"><em>pos</em> is either the string “end”, an integer index, or the name of a managed child. </span><span class="yiyi-st" id="yiyi-141">If <em>child</em> is already managed by the notebook, moves it to the specified position.</span></p><p><span class="yiyi-st" id="yiyi-142">See <a class="reference internal" href="#tab-options">Tab Options</a> for the list of available options.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Notebook.select"><span class="yiyi-st" id="yiyi-143"> <code class="descname">select</code><span class="sig-paren">(</span><em>tab_id=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-144">Selects the specified <em>tab_id</em>.</span></p><p><span class="yiyi-st" id="yiyi-145">The associated child window will be displayed, and the previously-selected window (if different) is unmapped. </span><span class="yiyi-st" id="yiyi-146">If <em>tab_id</em> is omitted, returns the widget name of the currently selected pane.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Notebook.tab"><span class="yiyi-st" id="yiyi-147"> <code class="descname">tab</code><span class="sig-paren">(</span><em>tab_id</em>, <em>option=None</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-148">Query or modify the options of the specific <em>tab_id</em>.</span></p><p><span class="yiyi-st" id="yiyi-149">If <em>kw</em> is not given, returns a dictionary of the tab option values. </span><span class="yiyi-st" id="yiyi-150">If <em>option</em> is specified, returns the value of that <em>option</em>. </span><span class="yiyi-st" id="yiyi-151">Otherwise, sets the options to the corresponding values.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Notebook.tabs"><span class="yiyi-st" id="yiyi-152"> <code class="descname">tabs</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-153">Returns a list of windows managed by the notebook.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Notebook.enable_traversal"><span class="yiyi-st" id="yiyi-154"> <code class="descname">enable_traversal</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-155">Enable keyboard traversal for a toplevel window containing this notebook.</span></p><p><span class="yiyi-st" id="yiyi-156">This will extend the bindings for the toplevel window containing the notebook as follows:</span></p><ul class="simple"><li><span class="yiyi-st" id="yiyi-157"><code class="kbd docutils literal"><span class="pre">Control-Tab</span></code>: selects the tab following the currently selected one.</span></li><li><span class="yiyi-st" id="yiyi-158"><code class="kbd docutils literal"><span class="pre">Shift-Control-Tab</span></code>: selects the tab preceding the currently selected one.</span></li><li><span class="yiyi-st" id="yiyi-159"><code class="kbd docutils literal"><span class="pre">Alt-K</span></code>: where <em>K</em> is the mnemonic (underlined) character of any tab, will select that tab.</span></li></ul><p><span class="yiyi-st" id="yiyi-160">Multiple notebooks in a single toplevel may be enabled for traversal, including nested notebooks. </span><span class="yiyi-st" id="yiyi-161">However, notebook traversal only works properly if all panes have the notebook they are in as master.</span></p></dd></dl></dd></dl></div></div><div class="section" id="progressbar"><h2><span class="yiyi-st" id="yiyi-162">25.2.6. </span><span class="yiyi-st" id="yiyi-163">Progressbar</span></h2><p><span class="yiyi-st" id="yiyi-164">The <code class="xref py py-class docutils literal"><span class="pre">ttk.Progressbar</span></code> widget shows the status of a long-running operation. </span><span class="yiyi-st" id="yiyi-165">It can operate in two modes: 1) the determinate mode which shows the amount completed relative to the total amount of work to be done and 2) the indeterminate mode which provides an animated display to let the user know that work is progressing.</span></p><div class="section" id="id3"><h3><span class="yiyi-st" id="yiyi-166">25.2.6.1. </span><span class="yiyi-st" id="yiyi-167">Options</span></h3><p><span class="yiyi-st" id="yiyi-168">This widget accepts the following specific options:</span></p><span class="yiyi-st" id="yiyi-169"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="14%"/> <col width="86%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>orient</td> <td>One of “horizontal” or “vertical”. Specifies the orientation of the progress bar.</td> </tr> <tr class="row-odd"><td>length</td> <td>Specifies the length of the long axis of the progress bar (width if horizontal, height if vertical).</td> </tr> <tr class="row-even"><td>mode</td> <td>One of “determinate” or “indeterminate”.</td> </tr> <tr class="row-odd"><td>maximum</td> <td>A number specifying the maximum value. Defaults to 100.</td> </tr> <tr class="row-even"><td>value</td> <td>The current value of the progress bar. In “determinate” mode, this represents the amount of work completed. In “indeterminate” mode, it is interpreted as modulo <em>maximum</em>; that is, the progress bar completes one “cycle” when its value increases by <em>maximum</em>.</td> </tr> <tr class="row-odd"><td>variable</td> <td>A name which is linked to the option value. If specified, the value of the progress bar is automatically set to the value of this name whenever the latter is modified.</td> </tr> <tr class="row-even"><td>phase</td> <td>Read-only option. The widget periodically increments the value of this option whenever its value is greater than 0 and, in determinate mode, less than maximum. This option may be used by the current theme to provide additional animation effects.</td> </tr> </tbody> </table> </div></blockquote></span></div><div class="section" id="ttk-progressbar"><h3><span class="yiyi-st" id="yiyi-170">25.2.6.2. ttk.Progressbar</span></h3><dl class="class"><dt id="tkinter.ttk.Progressbar"><span class="yiyi-st" id="yiyi-171"> <em class="property">class </em><code class="descclassname">tkinter.ttk.</code><code class="descname">Progressbar</code></span></dt><dd><dl class="method"><dt id="tkinter.ttk.Progressbar.start"><span class="yiyi-st" id="yiyi-172"> <code class="descname">start</code><span class="sig-paren">(</span><em>interval=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-173">Begin autoincrement mode: schedules a recurring timer event that calls <a class="reference internal" href="#tkinter.ttk.Progressbar.step" title="tkinter.ttk.Progressbar.step"><code class="xref py py-meth docutils literal"><span class="pre">Progressbar.step()</span></code></a> every <em>interval</em> milliseconds. </span><span class="yiyi-st" id="yiyi-174">If omitted, <em>interval</em> defaults to 50 milliseconds.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Progressbar.step"><span class="yiyi-st" id="yiyi-175"> <code class="descname">step</code><span class="sig-paren">(</span><em>amount=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-176">Increments the progress bar’s value by <em>amount</em>.</span></p><p><span class="yiyi-st" id="yiyi-177"><em>amount</em> defaults to 1.0 if omitted.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Progressbar.stop"><span class="yiyi-st" id="yiyi-178"> <code class="descname">stop</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-179">Stop autoincrement mode: cancels any recurring timer event initiated by <a class="reference internal" href="#tkinter.ttk.Progressbar.start" title="tkinter.ttk.Progressbar.start"><code class="xref py py-meth docutils literal"><span class="pre">Progressbar.start()</span></code></a> for this progress bar.</span></p></dd></dl></dd></dl></div></div><div class="section" id="separator"><h2><span class="yiyi-st" id="yiyi-180">25.2.7. </span><span class="yiyi-st" id="yiyi-181">Separator</span></h2><p><span class="yiyi-st" id="yiyi-182">The <code class="xref py py-class docutils literal"><span class="pre">ttk.Separator</span></code> widget displays a horizontal or vertical separator bar.</span></p><p><span class="yiyi-st" id="yiyi-183">It has no other methods besides the ones inherited from <code class="xref py py-class docutils literal"><span class="pre">ttk.Widget</span></code>.</span></p><div class="section" id="id4"><h3><span class="yiyi-st" id="yiyi-184">25.2.7.1. </span><span class="yiyi-st" id="yiyi-185">Options</span></h3><p><span class="yiyi-st" id="yiyi-186">This widget accepts the following specific option:</span></p><span class="yiyi-st" id="yiyi-187"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="11%"/> <col width="89%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>orient</td> <td>One of “horizontal” or “vertical”. Specifies the orientation of the separator.</td> </tr> </tbody> </table> </div></blockquote></span></div></div><div class="section" id="sizegrip"><h2><span class="yiyi-st" id="yiyi-188">25.2.8. </span><span class="yiyi-st" id="yiyi-189">Sizegrip</span></h2><p><span class="yiyi-st" id="yiyi-190">The <code class="xref py py-class docutils literal"><span class="pre">ttk.Sizegrip</span></code> widget (also known as a grow box) allows the user to resize the containing toplevel window by pressing and dragging the grip.</span></p><p><span class="yiyi-st" id="yiyi-191">This widget has neither specific options nor specific methods, besides the ones inherited from <code class="xref py py-class docutils literal"><span class="pre">ttk.Widget</span></code>.</span></p><div class="section" id="platform-specific-notes"><h3><span class="yiyi-st" id="yiyi-192">25.2.8.1. </span><span class="yiyi-st" id="yiyi-193">Platform-specific notes</span></h3><ul class="simple"><li><span class="yiyi-st" id="yiyi-194">On MacOS X, toplevel windows automatically include a built-in size grip by default. </span><span class="yiyi-st" id="yiyi-195">Adding a <code class="xref py py-class docutils literal"><span class="pre">Sizegrip</span></code> is harmless, since the built-in grip will just mask the widget.</span></li></ul></div><div class="section" id="bugs"><h3><span class="yiyi-st" id="yiyi-196">25.2.8.2. </span><span class="yiyi-st" id="yiyi-197">Bugs</span></h3><ul class="simple"><li><span class="yiyi-st" id="yiyi-198">If the containing toplevel’s position was specified relative to the right or bottom of the screen (e.g. </span><span class="yiyi-st" id="yiyi-199">....), the <code class="xref py py-class docutils literal"><span class="pre">Sizegrip</span></code> widget will not resize the window.</span></li><li><span class="yiyi-st" id="yiyi-200">This widget supports only “southeast” resizing.</span></li></ul></div></div><div class="section" id="treeview"><h2><span class="yiyi-st" id="yiyi-201">25.2.9. </span><span class="yiyi-st" id="yiyi-202">Treeview</span></h2><p><span class="yiyi-st" id="yiyi-203">The <code class="xref py py-class docutils literal"><span class="pre">ttk.Treeview</span></code> widget displays a hierarchical collection of items. </span><span class="yiyi-st" id="yiyi-204">Each item has a textual label, an optional image, and an optional list of data values. </span><span class="yiyi-st" id="yiyi-205">The data values are displayed in successive columns after the tree label.</span></p><p><span class="yiyi-st" id="yiyi-206">The order in which data values are displayed may be controlled by setting the widget option <code class="docutils literal"><span class="pre">displaycolumns</span></code>. </span><span class="yiyi-st" id="yiyi-207">The tree widget can also display column headings. </span><span class="yiyi-st" id="yiyi-208">Columns may be accessed by number or symbolic names listed in the widget option columns. </span><span class="yiyi-st" id="yiyi-209">See <a class="reference internal" href="#column-identifiers">Column Identifiers</a>.</span></p><p><span class="yiyi-st" id="yiyi-210">Each item is identified by a unique name. </span><span class="yiyi-st" id="yiyi-211">The widget will generate item IDs if they are not supplied by the caller. </span><span class="yiyi-st" id="yiyi-212">There is a distinguished root item, named <code class="docutils literal"><span class="pre">{}</span></code>. </span><span class="yiyi-st" id="yiyi-213">The root item itself is not displayed; its children appear at the top level of the hierarchy.</span></p><p><span class="yiyi-st" id="yiyi-214">Each item also has a list of tags, which can be used to associate event bindings with individual items and control the appearance of the item.</span></p><p><span class="yiyi-st" id="yiyi-215">The Treeview widget supports horizontal and vertical scrolling, according to the options described in <a class="reference internal" href="#scrollable-widget-options">Scrollable Widget Options</a> and the methods <a class="reference internal" href="#tkinter.ttk.Treeview.xview" title="tkinter.ttk.Treeview.xview"><code class="xref py py-meth docutils literal"><span class="pre">Treeview.xview()</span></code></a> and <a class="reference internal" href="#tkinter.ttk.Treeview.yview" title="tkinter.ttk.Treeview.yview"><code class="xref py py-meth docutils literal"><span class="pre">Treeview.yview()</span></code></a>.</span></p><div class="section" id="id5"><h3><span class="yiyi-st" id="yiyi-216">25.2.9.1. </span><span class="yiyi-st" id="yiyi-217">Options</span></h3><p><span class="yiyi-st" id="yiyi-218">This widget accepts the following specific options:</span></p><span class="yiyi-st" id="yiyi-219"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="22%"/> <col width="78%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>columns</td> <td>A list of column identifiers, specifying the number of columns and their names.</td> </tr> <tr class="row-odd"><td>displaycolumns</td> <td>A list of column identifiers (either symbolic or integer indices) specifying which data columns are displayed and the order in which they appear, or the string “#all”.</td> </tr> <tr class="row-even"><td>height</td> <td>Specifies the number of rows which should be visible. Note: the requested width is determined from the sum of the column widths.</td> </tr> <tr class="row-odd"><td>padding</td> <td>Specifies the internal padding for the widget. The padding is a list of up to four length specifications.</td> </tr> <tr class="row-even"><td>selectmode</td> <td><p class="first">Controls how the built-in class bindings manage the selection. One of “extended”, “browse” or “none”. If set to “extended” (the default), multiple items may be selected. If “browse”, only a single item will be selected at a time. If “none”, the selection will not be changed.</p> <p class="last">Note that the application code and tag bindings can set the selection however they wish, regardless of the value of this option.</p> </td> </tr> <tr class="row-odd"><td>show</td> <td><p class="first">A list containing zero or more of the following values, specifying which elements of the tree to display.</p> <ul class="simple"> <li>tree: display tree labels in column #0.</li> <li>headings: display the heading row.</li> </ul> <p>The default is “tree headings”, i.e., show all elements.</p> <p class="last"><strong>Note</strong>: Column #0 always refers to the tree column, even if show=”tree” is not specified.</p> </td> </tr> </tbody> </table> </div></blockquote></span></div><div class="section" id="item-options"><h3><span class="yiyi-st" id="yiyi-220">25.2.9.2. </span><span class="yiyi-st" id="yiyi-221">Item Options</span></h3><p><span class="yiyi-st" id="yiyi-222">The following item options may be specified for items in the insert and item widget commands.</span></p><span class="yiyi-st" id="yiyi-223"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="11%"/> <col width="89%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>text</td> <td>The textual label to display for the item.</td> </tr> <tr class="row-odd"><td>image</td> <td>A Tk Image, displayed to the left of the label.</td> </tr> <tr class="row-even"><td>values</td> <td><p class="first">The list of values associated with the item.</p> <p class="last">Each item should have the same number of values as the widget option columns. If there are fewer values than columns, the remaining values are assumed empty. If there are more values than columns, the extra values are ignored.</p> </td> </tr> <tr class="row-odd"><td>open</td> <td>True/False value indicating whether the item’s children should be displayed or hidden.</td> </tr> <tr class="row-even"><td>tags</td> <td>A list of tags associated with this item.</td> </tr> </tbody> </table> </div></blockquote></span></div><div class="section" id="tag-options"><h3><span class="yiyi-st" id="yiyi-224">25.2.9.3. </span><span class="yiyi-st" id="yiyi-225">Tag Options</span></h3><p><span class="yiyi-st" id="yiyi-226">The following options may be specified on tags:</span></p><span class="yiyi-st" id="yiyi-227"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="17%"/> <col width="83%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>foreground</td> <td>Specifies the text foreground color.</td> </tr> <tr class="row-odd"><td>background</td> <td>Specifies the cell or item background color.</td> </tr> <tr class="row-even"><td>font</td> <td>Specifies the font to use when drawing text.</td> </tr> <tr class="row-odd"><td>image</td> <td>Specifies the item image, in case the item’s image option is empty.</td> </tr> </tbody> </table> </div></blockquote></span></div><div class="section" id="column-identifiers"><h3><span class="yiyi-st" id="yiyi-228">25.2.9.4. </span><span class="yiyi-st" id="yiyi-229">Column Identifiers</span></h3><p><span class="yiyi-st" id="yiyi-230">Column identifiers take any of the following forms:</span></p><ul class="simple"><li><span class="yiyi-st" id="yiyi-231">A symbolic name from the list of columns option.</span></li><li><span class="yiyi-st" id="yiyi-232">An integer n, specifying the nth data column.</span></li><li><span class="yiyi-st" id="yiyi-233">A string of the form #n, where n is an integer, specifying the nth display column.</span></li></ul><p><span class="yiyi-st" id="yiyi-234">Notes:</span></p><ul class="simple"><li><span class="yiyi-st" id="yiyi-235">Item’s option values may be displayed in a different order than the order in which they are stored.</span></li><li><span class="yiyi-st" id="yiyi-236">Column #0 always refers to the tree column, even if show=”tree” is not specified.</span></li></ul><p><span class="yiyi-st" id="yiyi-237">A data column number is an index into an item’s option values list; a display column number is the column number in the tree where the values are displayed. </span><span class="yiyi-st" id="yiyi-238">Tree labels are displayed in column #0. </span><span class="yiyi-st" id="yiyi-239">If option displaycolumns is not set, then data column n is displayed in column #n+1. </span><span class="yiyi-st" id="yiyi-240">Again, <strong>column #0 always refers to the tree column</strong>.</span></p></div><div class="section" id="id6"><h3><span class="yiyi-st" id="yiyi-241">25.2.9.5. </span><span class="yiyi-st" id="yiyi-242">Virtual Events</span></h3><p><span class="yiyi-st" id="yiyi-243">The Treeview widget generates the following virtual events.</span></p><span class="yiyi-st" id="yiyi-245"> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="29%"/> <col width="71%"/> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Event</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><<TreeviewSelect>></td> <td>Generated whenever the selection changes.</td> </tr> <tr class="row-odd"><td><<TreeviewOpen>></td> <td>Generated just before settings the focus item to open=True.</td> </tr> <tr class="row-even"><td><<TreeviewClose>></td> <td>Generated just after setting the focus item to open=False.</td> </tr> </tbody> </table> </div></blockquote></span><p><span class="yiyi-st" id="yiyi-244">The <a class="reference internal" href="#tkinter.ttk.Treeview.focus" title="tkinter.ttk.Treeview.focus"><code class="xref py py-meth docutils literal"><span class="pre">Treeview.focus()</span></code></a> and <a class="reference internal" href="#tkinter.ttk.Treeview.selection" title="tkinter.ttk.Treeview.selection"><code class="xref py py-meth docutils literal"><span class="pre">Treeview.selection()</span></code></a> methods can be used to determine the affected item or items.</span></p></div><div class="section" id="ttk-treeview"><h3><span class="yiyi-st" id="yiyi-246">25.2.9.6. ttk.Treeview</span></h3><dl class="class"><dt id="tkinter.ttk.Treeview"><span class="yiyi-st" id="yiyi-247"> <em class="property">class </em><code class="descclassname">tkinter.ttk.</code><code class="descname">Treeview</code></span></dt><dd><dl class="method"><dt id="tkinter.ttk.Treeview.bbox"><span class="yiyi-st" id="yiyi-248"> <code class="descname">bbox</code><span class="sig-paren">(</span><em>item</em>, <em>column=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-249">Returns the bounding box (relative to the treeview widget’s window) of the specified <em>item</em> in the form (x, y, width, height).</span></p><p><span class="yiyi-st" id="yiyi-250">If <em>column</em> is specified, returns the bounding box of that cell. </span><span class="yiyi-st" id="yiyi-251">If the <em>item</em> is not visible (i.e., if it is a descendant of a closed item or is scrolled offscreen), returns an empty string.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.get_children"><span class="yiyi-st" id="yiyi-252"> <code class="descname">get_children</code><span class="sig-paren">(</span><em>item=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-253">Returns the list of children belonging to <em>item</em>.</span></p><p><span class="yiyi-st" id="yiyi-254">If <em>item</em> is not specified, returns root children.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.set_children"><span class="yiyi-st" id="yiyi-255"> <code class="descname">set_children</code><span class="sig-paren">(</span><em>item</em>, <em>*newchildren</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-256">Replaces <em>item</em>‘s child with <em>newchildren</em>.</span></p><p><span class="yiyi-st" id="yiyi-257">Children present in <em>item</em> that are not present in <em>newchildren</em> are detached from the tree. </span><span class="yiyi-st" id="yiyi-258">No items in <em>newchildren</em> may be an ancestor of <em>item</em>. </span><span class="yiyi-st" id="yiyi-259">Note that not specifying <em>newchildren</em> results in detaching <em>item</em>‘s children.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.column"><span class="yiyi-st" id="yiyi-260"> <code class="descname">column</code><span class="sig-paren">(</span><em>column</em>, <em>option=None</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-261">Query or modify the options for the specified <em>column</em>.</span></p><p><span class="yiyi-st" id="yiyi-262">If <em>kw</em> is not given, returns a dict of the column option values. </span><span class="yiyi-st" id="yiyi-263">If <em>option</em> is specified then the value for that <em>option</em> is returned. </span><span class="yiyi-st" id="yiyi-264">Otherwise, sets the options to the corresponding values.</span></p><p><span class="yiyi-st" id="yiyi-265">The valid options/values are:</span></p><ul><li><dl class="first docutils"><dt><span class="yiyi-st" id="yiyi-266">id</span></dt><dd><p class="first last"><span class="yiyi-st" id="yiyi-267">Returns the column name. </span><span class="yiyi-st" id="yiyi-268">This is a read-only option.</span></p></dd></dl></li><li><dl class="first docutils"><dt><span class="yiyi-st" id="yiyi-269">anchor: One of the standard Tk anchor values.</span></dt><dd><p class="first last"><span class="yiyi-st" id="yiyi-270">Specifies how the text in this column should be aligned with respect to the cell.</span></p></dd></dl></li><li><dl class="first docutils"><dt><span class="yiyi-st" id="yiyi-271">minwidth: width</span></dt><dd><p class="first last"><span class="yiyi-st" id="yiyi-272">The minimum width of the column in pixels. </span><span class="yiyi-st" id="yiyi-273">The treeview widget will not make the column any smaller than specified by this option when the widget is resized or the user drags a column.</span></p></dd></dl></li><li><dl class="first docutils"><dt><span class="yiyi-st" id="yiyi-274">stretch: True/False</span></dt><dd><p class="first last"><span class="yiyi-st" id="yiyi-275">Specifies whether the column’s width should be adjusted when the widget is resized.</span></p></dd></dl></li><li><dl class="first docutils"><dt><span class="yiyi-st" id="yiyi-276">width: width</span></dt><dd><p class="first last"><span class="yiyi-st" id="yiyi-277">The width of the column in pixels.</span></p></dd></dl></li></ul><p><span class="yiyi-st" id="yiyi-278">To configure the tree column, call this with column = “#0”</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.delete"><span class="yiyi-st" id="yiyi-279"> <code class="descname">delete</code><span class="sig-paren">(</span><em>*items</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-280">Delete all specified <em>items</em> and all their descendants.</span></p><p><span class="yiyi-st" id="yiyi-281">The root item may not be deleted.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.detach"><span class="yiyi-st" id="yiyi-282"> <code class="descname">detach</code><span class="sig-paren">(</span><em>*items</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-283">Unlinks all of the specified <em>items</em> from the tree.</span></p><p><span class="yiyi-st" id="yiyi-284">The items and all of their descendants are still present, and may be reinserted at another point in the tree, but will not be displayed.</span></p><p><span class="yiyi-st" id="yiyi-285">The root item may not be detached.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.exists"><span class="yiyi-st" id="yiyi-286"> <code class="descname">exists</code><span class="sig-paren">(</span><em>item</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-287">Returns <code class="docutils literal"><span class="pre">True</span></code> if the specified <em>item</em> is present in the tree.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.focus"><span class="yiyi-st" id="yiyi-288"> <code class="descname">focus</code><span class="sig-paren">(</span><em>item=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-289">If <em>item</em> is specified, sets the focus item to <em>item</em>. </span><span class="yiyi-st" id="yiyi-290">Otherwise, returns the current focus item, or ‘’ if there is none.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.heading"><span class="yiyi-st" id="yiyi-291"> <code class="descname">heading</code><span class="sig-paren">(</span><em>column</em>, <em>option=None</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-292">Query or modify the heading options for the specified <em>column</em>.</span></p><p><span class="yiyi-st" id="yiyi-293">If <em>kw</em> is not given, returns a dict of the heading option values. </span><span class="yiyi-st" id="yiyi-294">If <em>option</em> is specified then the value for that <em>option</em> is returned. </span><span class="yiyi-st" id="yiyi-295">Otherwise, sets the options to the corresponding values.</span></p><p><span class="yiyi-st" id="yiyi-296">The valid options/values are:</span></p><ul><li><dl class="first docutils"><dt><span class="yiyi-st" id="yiyi-297">text: text</span></dt><dd><p class="first last"><span class="yiyi-st" id="yiyi-298">The text to display in the column heading.</span></p></dd></dl></li><li><dl class="first docutils"><dt><span class="yiyi-st" id="yiyi-299">image: imageName</span></dt><dd><p class="first last"><span class="yiyi-st" id="yiyi-300">Specifies an image to display to the right of the column heading.</span></p></dd></dl></li><li><dl class="first docutils"><dt><span class="yiyi-st" id="yiyi-301">anchor: anchor</span></dt><dd><p class="first last"><span class="yiyi-st" id="yiyi-302">Specifies how the heading text should be aligned. </span><span class="yiyi-st" id="yiyi-303">One of the standard Tk anchor values.</span></p></dd></dl></li><li><dl class="first docutils"><dt><span class="yiyi-st" id="yiyi-304">command: callback</span></dt><dd><p class="first last"><span class="yiyi-st" id="yiyi-305">A callback to be invoked when the heading label is pressed.</span></p></dd></dl></li></ul><p><span class="yiyi-st" id="yiyi-306">To configure the tree column heading, call this with column = “#0”.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.identify"><span class="yiyi-st" id="yiyi-307"> <code class="descname">identify</code><span class="sig-paren">(</span><em>component</em>, <em>x</em>, <em>y</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-308">Returns a description of the specified <em>component</em> under the point given by <em>x</em> and <em>y</em>, or the empty string if no such <em>component</em> is present at that position.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.identify_row"><span class="yiyi-st" id="yiyi-309"> <code class="descname">identify_row</code><span class="sig-paren">(</span><em>y</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-310">Returns the item ID of the item at position <em>y</em>.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.identify_column"><span class="yiyi-st" id="yiyi-311"> <code class="descname">identify_column</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-312">Returns the data column identifier of the cell at position <em>x</em>.</span></p><p><span class="yiyi-st" id="yiyi-313">The tree column has ID #0.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.identify_region"><span class="yiyi-st" id="yiyi-314"> <code class="descname">identify_region</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-315">Returns one of:</span></p><table border="1" class="docutils"><thead valign="bottom"><tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-316">region</span></th><th class="head"><span class="yiyi-st" id="yiyi-317">meaning</span></th></tr></thead><tbody valign="top"><tr class="row-even"><td><span class="yiyi-st" id="yiyi-318">heading</span></td><td><span class="yiyi-st" id="yiyi-319">Tree heading area.</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-320">separator</span></td><td><span class="yiyi-st" id="yiyi-321">Space between two columns headings.</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-322">tree</span></td><td><span class="yiyi-st" id="yiyi-323">The tree area.</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-324">cell</span></td><td><span class="yiyi-st" id="yiyi-325">A data cell.</span></td></tr></tbody></table><p><span class="yiyi-st" id="yiyi-326">Availability: Tk 8.6.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.identify_element"><span class="yiyi-st" id="yiyi-327"> <code class="descname">identify_element</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-328">Returns the element at position <em>x</em>, <em>y</em>.</span></p><p><span class="yiyi-st" id="yiyi-329">Availability: Tk 8.6.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.index"><span class="yiyi-st" id="yiyi-330"> <code class="descname">index</code><span class="sig-paren">(</span><em>item</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-331">Returns the integer index of <em>item</em> within its parent’s list of children.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.insert"><span class="yiyi-st" id="yiyi-332"> <code class="descname">insert</code><span class="sig-paren">(</span><em>parent</em>, <em>index</em>, <em>iid=None</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-333">Creates a new item and returns the item identifier of the newly created item.</span></p><p><span class="yiyi-st" id="yiyi-334"><em>parent</em> is the item ID of the parent item, or the empty string to create a new top-level item. </span><span class="yiyi-st" id="yiyi-335"><em>index</em> is an integer, or the value “end”, specifying where in the list of parent’s children to insert the new item. </span><span class="yiyi-st" id="yiyi-336">If <em>index</em> is less than or equal to zero, the new node is inserted at the beginning; if <em>index</em> is greater than or equal to the current number of children, it is inserted at the end. </span><span class="yiyi-st" id="yiyi-337">If <em>iid</em> is specified, it is used as the item identifier; <em>iid</em> must not already exist in the tree. </span><span class="yiyi-st" id="yiyi-338">Otherwise, a new unique identifier is generated.</span></p><p><span class="yiyi-st" id="yiyi-339">See <a class="reference internal" href="#item-options">Item Options</a> for the list of available points.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.item"><span class="yiyi-st" id="yiyi-340"> <code class="descname">item</code><span class="sig-paren">(</span><em>item</em>, <em>option=None</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-341">Query or modify the options for the specified <em>item</em>.</span></p><p><span class="yiyi-st" id="yiyi-342">If no options are given, a dict with options/values for the item is returned. </span><span class="yiyi-st" id="yiyi-343">If <em>option</em> is specified then the value for that option is returned. </span><span class="yiyi-st" id="yiyi-344">Otherwise, sets the options to the corresponding values as given by <em>kw</em>.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.move"><span class="yiyi-st" id="yiyi-345"> <code class="descname">move</code><span class="sig-paren">(</span><em>item</em>, <em>parent</em>, <em>index</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-346">Moves <em>item</em> to position <em>index</em> in <em>parent</em>‘s list of children.</span></p><p><span class="yiyi-st" id="yiyi-347">It is illegal to move an item under one of its descendants. </span><span class="yiyi-st" id="yiyi-348">If <em>index</em> is less than or equal to zero, <em>item</em> is moved to the beginning; if greater than or equal to the number of children, it is moved to the end. </span><span class="yiyi-st" id="yiyi-349">If <em>item</em> was detached it is reattached.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.next"><span class="yiyi-st" id="yiyi-350"> <code class="descname">next</code><span class="sig-paren">(</span><em>item</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-351">Returns the identifier of <em>item</em>‘s next sibling, or ‘’ if <em>item</em> is the last child of its parent.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.parent"><span class="yiyi-st" id="yiyi-352"> <code class="descname">parent</code><span class="sig-paren">(</span><em>item</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-353">Returns the ID of the parent of <em>item</em>, or ‘’ if <em>item</em> is at the top level of the hierarchy.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.prev"><span class="yiyi-st" id="yiyi-354"> <code class="descname">prev</code><span class="sig-paren">(</span><em>item</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-355">Returns the identifier of <em>item</em>‘s previous sibling, or ‘’ if <em>item</em> is the first child of its parent.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.reattach"><span class="yiyi-st" id="yiyi-356"> <code class="descname">reattach</code><span class="sig-paren">(</span><em>item</em>, <em>parent</em>, <em>index</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-357">An alias for <a class="reference internal" href="#tkinter.ttk.Treeview.move" title="tkinter.ttk.Treeview.move"><code class="xref py py-meth docutils literal"><span class="pre">Treeview.move()</span></code></a>.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.see"><span class="yiyi-st" id="yiyi-358"> <code class="descname">see</code><span class="sig-paren">(</span><em>item</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-359">Ensure that <em>item</em> is visible.</span></p><p><span class="yiyi-st" id="yiyi-360">Sets all of <em>item</em>‘s ancestors open option to <code class="docutils literal"><span class="pre">True</span></code>, and scrolls the widget if necessary so that <em>item</em> is within the visible portion of the tree.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.selection"><span class="yiyi-st" id="yiyi-361"> <code class="descname">selection</code><span class="sig-paren">(</span><em>selop=None</em>, <em>items=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-362">If <em>selop</em> is not specified, returns selected items. </span><span class="yiyi-st" id="yiyi-363">Otherwise, it will act according to the following selection methods.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.selection_set"><span class="yiyi-st" id="yiyi-364"> <code class="descname">selection_set</code><span class="sig-paren">(</span><em>items</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-365"><em>items</em> becomes the new selection.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.selection_add"><span class="yiyi-st" id="yiyi-366"> <code class="descname">selection_add</code><span class="sig-paren">(</span><em>items</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-367">Add <em>items</em> to the selection.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.selection_remove"><span class="yiyi-st" id="yiyi-368"> <code class="descname">selection_remove</code><span class="sig-paren">(</span><em>items</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-369">Remove <em>items</em> from the selection.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.selection_toggle"><span class="yiyi-st" id="yiyi-370"> <code class="descname">selection_toggle</code><span class="sig-paren">(</span><em>items</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-371">Toggle the selection state of each item in <em>items</em>.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.set"><span class="yiyi-st" id="yiyi-372"> <code class="descname">set</code><span class="sig-paren">(</span><em>item</em>, <em>column=None</em>, <em>value=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-373">With one argument, returns a dictionary of column/value pairs for the specified <em>item</em>. </span><span class="yiyi-st" id="yiyi-374">With two arguments, returns the current value of the specified <em>column</em>. </span><span class="yiyi-st" id="yiyi-375">With three arguments, sets the value of given <em>column</em> in given <em>item</em> to the specified <em>value</em>.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.tag_bind"><span class="yiyi-st" id="yiyi-376"> <code class="descname">tag_bind</code><span class="sig-paren">(</span><em>tagname</em>, <em>sequence=None</em>, <em>callback=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-377">Bind a callback for the given event <em>sequence</em> to the tag <em>tagname</em>. </span><span class="yiyi-st" id="yiyi-378">When an event is delivered to an item, the callbacks for each of the item’s tags option are called.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.tag_configure"><span class="yiyi-st" id="yiyi-379"> <code class="descname">tag_configure</code><span class="sig-paren">(</span><em>tagname</em>, <em>option=None</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-380">Query or modify the options for the specified <em>tagname</em>.</span></p><p><span class="yiyi-st" id="yiyi-381">If <em>kw</em> is not given, returns a dict of the option settings for <em>tagname</em>. </span><span class="yiyi-st" id="yiyi-382">If <em>option</em> is specified, returns the value for that <em>option</em> for the specified <em>tagname</em>. </span><span class="yiyi-st" id="yiyi-383">Otherwise, sets the options to the corresponding values for the given <em>tagname</em>.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.tag_has"><span class="yiyi-st" id="yiyi-384"> <code class="descname">tag_has</code><span class="sig-paren">(</span><em>tagname</em>, <em>item=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-385">If <em>item</em> is specified, returns 1 or 0 depending on whether the specified <em>item</em> has the given <em>tagname</em>. </span><span class="yiyi-st" id="yiyi-386">Otherwise, returns a list of all items that have the specified tag.</span></p><p><span class="yiyi-st" id="yiyi-387">Availability: Tk 8.6</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.xview"><span class="yiyi-st" id="yiyi-388"> <code class="descname">xview</code><span class="sig-paren">(</span><em>*args</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-389">Query or modify horizontal position of the treeview.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Treeview.yview"><span class="yiyi-st" id="yiyi-390"> <code class="descname">yview</code><span class="sig-paren">(</span><em>*args</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-391">Query or modify vertical position of the treeview.</span></p></dd></dl></dd></dl></div></div><div class="section" id="ttk-styling"><h2><span class="yiyi-st" id="yiyi-392">25.2.10. </span><span class="yiyi-st" id="yiyi-393">Ttk Styling</span></h2><p><span class="yiyi-st" id="yiyi-394">Each widget in <code class="xref py py-mod docutils literal"><span class="pre">ttk</span></code> is assigned a style, which specifies the set of elements making up the widget and how they are arranged, along with dynamic and default settings for element options. </span><span class="yiyi-st" id="yiyi-395">By default the style name is the same as the widget’s class name, but it may be overridden by the widget’s style option. </span><span class="yiyi-st" id="yiyi-396">If you don’t know the class name of a widget, use the method <code class="xref py py-meth docutils literal"><span class="pre">Misc.winfo_class()</span></code> (somewidget.winfo_class()).</span></p><div class="admonition seealso"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-397">See also</span></p><dl class="last docutils"><dt><span class="yiyi-st" id="yiyi-398"><a class="reference external" href="http://tktable.sourceforge.net/tile/tile-tcl2004.pdf">Tcl‘2004 conference presentation</a></span></dt><dd><span class="yiyi-st" id="yiyi-399">This document explains how the theme engine works</span></dd></dl></div><dl class="class"><dt id="tkinter.ttk.Style"><span class="yiyi-st" id="yiyi-400"> <em class="property">class </em><code class="descclassname">tkinter.ttk.</code><code class="descname">Style</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-401">This class is used to manipulate the style database.</span></p><dl class="method"><dt id="tkinter.ttk.Style.configure"><span class="yiyi-st" id="yiyi-402"> <code class="descname">configure</code><span class="sig-paren">(</span><em>style</em>, <em>query_opt=None</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-403">Query or set the default value of the specified option(s) in <em>style</em>.</span></p><p><span class="yiyi-st" id="yiyi-404">Each key in <em>kw</em> is an option and each value is a string identifying the value for that option.</span></p><p><span class="yiyi-st" id="yiyi-405">For example, to change every default button to be a flat button with some padding and a different background color:</span></p><pre><code class="language-python"><span></span><span class="kn">from</span> <span class="nn">tkinter</span> <span class="k">import</span> <span class="n">ttk</span>
|
||
<span class="kn">import</span> <span class="nn">tkinter</span>
|
||
|
||
<span class="n">root</span> <span class="o">=</span> <span class="n">tkinter</span><span class="o">.</span><span class="n">Tk</span><span class="p">()</span>
|
||
|
||
<span class="n">ttk</span><span class="o">.</span><span class="n">Style</span><span class="p">()</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="s2">"TButton"</span><span class="p">,</span> <span class="n">padding</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">relief</span><span class="o">=</span><span class="s2">"flat"</span><span class="p">,</span>
|
||
<span class="n">background</span><span class="o">=</span><span class="s2">"#ccc"</span><span class="p">)</span>
|
||
|
||
<span class="n">btn</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Button</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="s2">"Sample"</span><span class="p">)</span>
|
||
<span class="n">btn</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>
|
||
|
||
<span class="n">root</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>
|
||
</code></pre></dd></dl><dl class="method"><dt id="tkinter.ttk.Style.map"><span class="yiyi-st" id="yiyi-406"> <code class="descname">map</code><span class="sig-paren">(</span><em>style</em>, <em>query_opt=None</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-407">Query or sets dynamic values of the specified option(s) in <em>style</em>.</span></p><p><span class="yiyi-st" id="yiyi-408">Each key in <em>kw</em> is an option and each value should be a list or a tuple (usually) containing statespecs grouped in tuples, lists, or some other preference. </span><span class="yiyi-st" id="yiyi-409">A statespec is a compound of one or more states and then a value.</span></p><p><span class="yiyi-st" id="yiyi-410">An example may make it more understandable:</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">tkinter</span>
|
||
<span class="kn">from</span> <span class="nn">tkinter</span> <span class="k">import</span> <span class="n">ttk</span>
|
||
|
||
<span class="n">root</span> <span class="o">=</span> <span class="n">tkinter</span><span class="o">.</span><span class="n">Tk</span><span class="p">()</span>
|
||
|
||
<span class="n">style</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Style</span><span class="p">()</span>
|
||
<span class="n">style</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="s2">"C.TButton"</span><span class="p">,</span>
|
||
<span class="n">foreground</span><span class="o">=</span><span class="p">[(</span><span class="s1">'pressed'</span><span class="p">,</span> <span class="s1">'red'</span><span class="p">),</span> <span class="p">(</span><span class="s1">'active'</span><span class="p">,</span> <span class="s1">'blue'</span><span class="p">)],</span>
|
||
<span class="n">background</span><span class="o">=</span><span class="p">[(</span><span class="s1">'pressed'</span><span class="p">,</span> <span class="s1">'!disabled'</span><span class="p">,</span> <span class="s1">'black'</span><span class="p">),</span> <span class="p">(</span><span class="s1">'active'</span><span class="p">,</span> <span class="s1">'white'</span><span class="p">)]</span>
|
||
<span class="p">)</span>
|
||
|
||
<span class="n">colored_btn</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Button</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="s2">"Test"</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="s2">"C.TButton"</span><span class="p">)</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>
|
||
|
||
<span class="n">root</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-411">Note that the order of the (states, value) sequences for an option does matter, if the order is changed to <code class="docutils literal"><span class="pre">[('active',</span> <span class="pre">'blue'),</span> <span class="pre">('pressed',</span> <span class="pre">'red')]</span></code> in the foreground option, for example, the result would be a blue foreground when the widget were in active or pressed states.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Style.lookup"><span class="yiyi-st" id="yiyi-412"> <code class="descname">lookup</code><span class="sig-paren">(</span><em>style</em>, <em>option</em>, <em>state=None</em>, <em>default=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-413">Returns the value specified for <em>option</em> in <em>style</em>.</span></p><p><span class="yiyi-st" id="yiyi-414">If <em>state</em> is specified, it is expected to be a sequence of one or more states. </span><span class="yiyi-st" id="yiyi-415">If the <em>default</em> argument is set, it is used as a fallback value in case no specification for option is found.</span></p><p><span class="yiyi-st" id="yiyi-416">To check what font a Button uses by default:</span></p><pre><code class="language-python"><span></span><span class="kn">from</span> <span class="nn">tkinter</span> <span class="k">import</span> <span class="n">ttk</span>
|
||
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">ttk</span><span class="o">.</span><span class="n">Style</span><span class="p">()</span><span class="o">.</span><span class="n">lookup</span><span class="p">(</span><span class="s2">"TButton"</span><span class="p">,</span> <span class="s2">"font"</span><span class="p">))</span>
|
||
</code></pre></dd></dl><dl class="method"><dt id="tkinter.ttk.Style.layout"><span class="yiyi-st" id="yiyi-417"> <code class="descname">layout</code><span class="sig-paren">(</span><em>style</em>, <em>layoutspec=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-418">Define the widget layout for given <em>style</em>. </span><span class="yiyi-st" id="yiyi-419">If <em>layoutspec</em> is omitted, return the layout specification for given style.</span></p><p><span class="yiyi-st" id="yiyi-420"><em>layoutspec</em>, if specified, is expected to be a list or some other sequence type (excluding strings), where each item should be a tuple and the first item is the layout name and the second item should have the format described in <a class="reference internal" href="#layouts">Layouts</a>.</span></p><p><span class="yiyi-st" id="yiyi-421">To understand the format, see the following example (it is not intended to do anything useful):</span></p><pre><code class="language-python"><span></span><span class="kn">from</span> <span class="nn">tkinter</span> <span class="k">import</span> <span class="n">ttk</span>
|
||
<span class="kn">import</span> <span class="nn">tkinter</span>
|
||
|
||
<span class="n">root</span> <span class="o">=</span> <span class="n">tkinter</span><span class="o">.</span><span class="n">Tk</span><span class="p">()</span>
|
||
|
||
<span class="n">style</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Style</span><span class="p">()</span>
|
||
<span class="n">style</span><span class="o">.</span><span class="n">layout</span><span class="p">(</span><span class="s2">"TMenubutton"</span><span class="p">,</span> <span class="p">[</span>
|
||
<span class="p">(</span><span class="s2">"Menubutton.background"</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
|
||
<span class="p">(</span><span class="s2">"Menubutton.button"</span><span class="p">,</span> <span class="p">{</span><span class="s2">"children"</span><span class="p">:</span>
|
||
<span class="p">[(</span><span class="s2">"Menubutton.focus"</span><span class="p">,</span> <span class="p">{</span><span class="s2">"children"</span><span class="p">:</span>
|
||
<span class="p">[(</span><span class="s2">"Menubutton.padding"</span><span class="p">,</span> <span class="p">{</span><span class="s2">"children"</span><span class="p">:</span>
|
||
<span class="p">[(</span><span class="s2">"Menubutton.label"</span><span class="p">,</span> <span class="p">{</span><span class="s2">"side"</span><span class="p">:</span> <span class="s2">"left"</span><span class="p">,</span> <span class="s2">"expand"</span><span class="p">:</span> <span class="mi">1</span><span class="p">})]</span>
|
||
<span class="p">})]</span>
|
||
<span class="p">})]</span>
|
||
<span class="p">}),</span>
|
||
<span class="p">])</span>
|
||
|
||
<span class="n">mbtn</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Menubutton</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="s1">'Text'</span><span class="p">)</span>
|
||
<span class="n">mbtn</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>
|
||
<span class="n">root</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>
|
||
</code></pre></dd></dl><dl class="method"><dt id="tkinter.ttk.Style.element_create"><span class="yiyi-st" id="yiyi-422"> <code class="descname">element_create</code><span class="sig-paren">(</span><em>elementname</em>, <em>etype</em>, <em>*args</em>, <em>**kw</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-423">Create a new element in the current theme, of the given <em>etype</em> which is expected to be either “image”, “from” or “vsapi”. </span><span class="yiyi-st" id="yiyi-424">The latter is only available in Tk 8.6a for Windows XP and Vista and is not described here.</span></p><p><span class="yiyi-st" id="yiyi-425">If “image” is used, <em>args</em> should contain the default image name followed by statespec/value pairs (this is the imagespec), and <em>kw</em> may have the following options:</span></p><span class="yiyi-st" id="yiyi-430"> <blockquote><div><ul><li><dl class="first docutils"><dt>border=padding</dt> <dd><p class="first last">填充是一个最多四个整数的列表,分别指定左边、顶部、右边和底部边界。</p></dd></dl></li> <li><dl class="first docutils"><dt>height=height</dt> <dd><p class="first last">指定元素的最小高度。如果小于零,则使用基本图像的高度作为默认值。</p></dd></dl></li> <li><dl class="first docutils"><dt>padding=padding</dt> <dd><p class="first last">指定元素的内部填充。如果没有指定边界的值,则为默认值。</p></dd></dl></li> <li><dl class="first docutils"><dt>sticky=spec</dt> <dd><p class="first last">指定图像如何放置在最后一个包裹内。spec包含0或更多的字符“n”、“s”、“w”或“e”。</p></dd></dl></li> <li><dl class="first docutils"><dt>width=width</dt> <dd><p class="first last">指定元素的最小宽度。如果小于零,则使用基本图像的宽度作为默认值。</p></dd></dl></li></ul></div></blockquote></span><p><span class="yiyi-st" id="yiyi-426">If “from” is used as the value of <em>etype</em>, <a class="reference internal" href="#tkinter.ttk.Style.element_create" title="tkinter.ttk.Style.element_create"><code class="xref py py-meth docutils literal"><span class="pre">element_create()</span></code></a> will clone an existing element. </span><span class="yiyi-st" id="yiyi-427"><em>args</em> is expected to contain a themename, from which the element will be cloned, and optionally an element to clone from. </span><span class="yiyi-st" id="yiyi-428">If this element to clone from is not specified, an empty element will be used. </span><span class="yiyi-st" id="yiyi-429"><em>kw</em> is discarded.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Style.element_names"><span class="yiyi-st" id="yiyi-431"> <code class="descname">element_names</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-432">Returns the list of elements defined in the current theme.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Style.element_options"><span class="yiyi-st" id="yiyi-433"> <code class="descname">element_options</code><span class="sig-paren">(</span><em>elementname</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-434">Returns the list of <em>elementname</em>‘s options.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Style.theme_create"><span class="yiyi-st" id="yiyi-435"> <code class="descname">theme_create</code><span class="sig-paren">(</span><em>themename</em>, <em>parent=None</em>, <em>settings=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-436">Create a new theme.</span></p><p><span class="yiyi-st" id="yiyi-437">It is an error if <em>themename</em> already exists. </span><span class="yiyi-st" id="yiyi-438">If <em>parent</em> is specified, the new theme will inherit styles, elements and layouts from the parent theme. </span><span class="yiyi-st" id="yiyi-439">If <em>settings</em> are present they are expected to have the same syntax used for <a class="reference internal" href="#tkinter.ttk.Style.theme_settings" title="tkinter.ttk.Style.theme_settings"><code class="xref py py-meth docutils literal"><span class="pre">theme_settings()</span></code></a>.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Style.theme_settings"><span class="yiyi-st" id="yiyi-440"> <code class="descname">theme_settings</code><span class="sig-paren">(</span><em>themename</em>, <em>settings</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-441">Temporarily sets the current theme to <em>themename</em>, apply specified <em>settings</em> and then restore the previous theme.</span></p><p><span class="yiyi-st" id="yiyi-442">Each key in <em>settings</em> is a style and each value may contain the keys ‘configure’, ‘map’, ‘layout’ and ‘element create’ and they are expected to have the same format as specified by the methods <a class="reference internal" href="#tkinter.ttk.Style.configure" title="tkinter.ttk.Style.configure"><code class="xref py py-meth docutils literal"><span class="pre">Style.configure()</span></code></a>, <a class="reference internal" href="#tkinter.ttk.Style.map" title="tkinter.ttk.Style.map"><code class="xref py py-meth docutils literal"><span class="pre">Style.map()</span></code></a>, <a class="reference internal" href="#tkinter.ttk.Style.layout" title="tkinter.ttk.Style.layout"><code class="xref py py-meth docutils literal"><span class="pre">Style.layout()</span></code></a> and <a class="reference internal" href="#tkinter.ttk.Style.element_create" title="tkinter.ttk.Style.element_create"><code class="xref py py-meth docutils literal"><span class="pre">Style.element_create()</span></code></a> respectively.</span></p><p><span class="yiyi-st" id="yiyi-443">As an example, let’s change the Combobox for the default theme a bit:</span></p><pre><code class="language-python"><span></span><span class="kn">from</span> <span class="nn">tkinter</span> <span class="k">import</span> <span class="n">ttk</span>
|
||
<span class="kn">import</span> <span class="nn">tkinter</span>
|
||
|
||
<span class="n">root</span> <span class="o">=</span> <span class="n">tkinter</span><span class="o">.</span><span class="n">Tk</span><span class="p">()</span>
|
||
|
||
<span class="n">style</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Style</span><span class="p">()</span>
|
||
<span class="n">style</span><span class="o">.</span><span class="n">theme_settings</span><span class="p">(</span><span class="s2">"default"</span><span class="p">,</span> <span class="p">{</span>
|
||
<span class="s2">"TCombobox"</span><span class="p">:</span> <span class="p">{</span>
|
||
<span class="s2">"configure"</span><span class="p">:</span> <span class="p">{</span><span class="s2">"padding"</span><span class="p">:</span> <span class="mi">5</span><span class="p">},</span>
|
||
<span class="s2">"map"</span><span class="p">:</span> <span class="p">{</span>
|
||
<span class="s2">"background"</span><span class="p">:</span> <span class="p">[(</span><span class="s2">"active"</span><span class="p">,</span> <span class="s2">"green2"</span><span class="p">),</span>
|
||
<span class="p">(</span><span class="s2">"!disabled"</span><span class="p">,</span> <span class="s2">"green4"</span><span class="p">)],</span>
|
||
<span class="s2">"fieldbackground"</span><span class="p">:</span> <span class="p">[(</span><span class="s2">"!disabled"</span><span class="p">,</span> <span class="s2">"green3"</span><span class="p">)],</span>
|
||
<span class="s2">"foreground"</span><span class="p">:</span> <span class="p">[(</span><span class="s2">"focus"</span><span class="p">,</span> <span class="s2">"OliveDrab1"</span><span class="p">),</span>
|
||
<span class="p">(</span><span class="s2">"!disabled"</span><span class="p">,</span> <span class="s2">"OliveDrab2"</span><span class="p">)]</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
<span class="p">})</span>
|
||
|
||
<span class="n">combo</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Combobox</span><span class="p">()</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>
|
||
|
||
<span class="n">root</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>
|
||
</code></pre></dd></dl><dl class="method"><dt id="tkinter.ttk.Style.theme_names"><span class="yiyi-st" id="yiyi-444"> <code class="descname">theme_names</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-445">Returns a list of all known themes.</span></p></dd></dl><dl class="method"><dt id="tkinter.ttk.Style.theme_use"><span class="yiyi-st" id="yiyi-446"> <code class="descname">theme_use</code><span class="sig-paren">(</span><em>themename=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-447">If <em>themename</em> is not given, returns the theme in use. </span><span class="yiyi-st" id="yiyi-448">Otherwise, sets the current theme to <em>themename</em>, refreshes all widgets and emits a <<themechanged>> event.</themechanged></span></p></dd></dl></dd></dl><div class="section" id="layouts"><h3><span class="yiyi-st" id="yiyi-449">25.2.10.1. </span><span class="yiyi-st" id="yiyi-450">Layouts</span></h3><p><span class="yiyi-st" id="yiyi-451">A layout can be just None, if it takes no options, or a dict of options specifying how to arrange the element. </span><span class="yiyi-st" id="yiyi-452">The layout mechanism uses a simplified version of the pack geometry manager: given an initial cavity, each element is allocated a parcel. </span><span class="yiyi-st" id="yiyi-453">Valid options/values are:</span></p><span class="yiyi-st" id="yiyi-454"> <blockquote><div><ul><li><dl class="first docutils"><dt>side: 哪一边</dt> <dd><p class="first last">指定将元素放置在空腔的哪一侧;顶部,右,下或左。如果省略,元素占据整个腔。</p></dd></dl></li> <li><dl class="first docutils"><dt>sticky: nswe</dt> <dd><p class="first last">指定元素在其分配的包内的位置。</p></dd></dl></li> <li><dl class="first docutils"><dt>unit: 0 or 1</dt> <dd><p class="first last">如果设置为1,则将元素及其所有子元素作为单个元素处理,以实现 <a class="reference internal" href="#tkinter.ttk.Widget.identify" title="tkinter.ttk.Widget.identify"><code class="xref py py-meth docutils literal"><span class="pre"> widget .identify()</span></code></a>等,它用于处理像scrollbar这样的手握柄。.</p></dd></dl></li> <li><dl class="first docutils"><dt>children:[sublayout……]</dt> <dd><p class="first last">指定元素的元素列表。每个元素都是一个元组(或其他序列类型),其中第一项是布局名称,另一个是 <a class="reference internal" href="#layouts">Layout</a>。</p></dd></dl></li></ul></div></blockquote></span></div></div></div></div> |