<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>8. Button Widgets &mdash; Python GTK+ 3 Tutorial 3.4 documentation</title>
  

  
  
  
  

  

  
  
    

  

  
  

  
    <link rel="stylesheet" href="https://media.readthedocs.org/css/sphinx_rtd_theme.css" type="text/css" />
  

  
        <link rel="index" title="Index"
              href="genindex.html"/>
        <link rel="search" title="Search" href="search.html"/>
    <link rel="top" title="Python GTK+ 3 Tutorial 3.4 documentation" href="index.html"/>
        <link rel="next" title="9. ProgressBar" href="progressbar.html"/>
        <link rel="prev" title="7. Entry" href="entry.html"/> 

  
  <script src="_static/js/modernizr.min.js"></script>


<!-- RTD Extra Head -->

<!-- 
Always link to the latest version, as canonical.
http://docs.readthedocs.org/en/latest/canonical.html
-->
<link rel="canonical" href="http://python-gtk-3-tutorial.readthedocs.io/en/latest/button_widgets.html" />

<link rel="stylesheet" href="https://media.readthedocs.org/css/readthedocs-doc-embed.css" type="text/css" />

<script type="text/javascript" src="_static/readthedocs-data.js"></script>

<!-- Add page-specific data, which must exist in the page js, not global -->
<script type="text/javascript">
READTHEDOCS_DATA['page'] = 'button_widgets' 		
READTHEDOCS_DATA['source_suffix'] = '.txt'
</script>

<script type="text/javascript" src="_static/readthedocs-dynamic-include.js"></script>

<!-- end RTD <extrahead> --></head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="index.html" class="icon icon-home"> Python GTK+ 3 Tutorial
          

          
          </a>

          
            
            
            
              <div class="version">
                latest
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="install.html">1. Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="introduction.html">2. Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="basics.html">3. Basics</a></li>
<li class="toctree-l1"><a class="reference internal" href="unicode.html">4. How to Deal With Strings</a></li>
<li class="toctree-l1"><a class="reference internal" href="layout.html">5. Layout Containers</a></li>
<li class="toctree-l1"><a class="reference internal" href="label.html">6. Label</a></li>
<li class="toctree-l1"><a class="reference internal" href="entry.html">7. Entry</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">8. Button Widgets</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#button">8.1. Button</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#example">8.1.1. Example</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#togglebutton">8.2. ToggleButton</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#id1">8.2.1. Example</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#checkbutton">8.3. CheckButton</a></li>
<li class="toctree-l2"><a class="reference internal" href="#radiobutton">8.4. RadioButton</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#id2">8.4.1. Example</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#linkbutton">8.5. LinkButton</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#id3">8.5.1. Example</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#spinbutton">8.6. SpinButton</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#id4">8.6.1. Example</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#switch">8.7. Switch</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#id5">8.7.1. Example</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="progressbar.html">9. ProgressBar</a></li>
<li class="toctree-l1"><a class="reference internal" href="spinner.html">10. Spinner</a></li>
<li class="toctree-l1"><a class="reference internal" href="treeview.html">11. Tree and List Widgets</a></li>
<li class="toctree-l1"><a class="reference internal" href="cellrenderers.html">12. CellRenderers</a></li>
<li class="toctree-l1"><a class="reference internal" href="combobox.html">13. ComboBox</a></li>
<li class="toctree-l1"><a class="reference internal" href="iconview.html">14. IconView</a></li>
<li class="toctree-l1"><a class="reference internal" href="textview.html">15. Multiline Text Editor</a></li>
<li class="toctree-l1"><a class="reference internal" href="dialogs.html">16. Dialogs</a></li>
<li class="toctree-l1"><a class="reference internal" href="clipboard.html">17. Clipboard</a></li>
<li class="toctree-l1"><a class="reference internal" href="drag_and_drop.html">18. Drag and Drop</a></li>
<li class="toctree-l1"><a class="reference internal" href="builder.html">19. Glade and Gtk.Builder</a></li>
<li class="toctree-l1"><a class="reference internal" href="objects.html">20. Objects</a></li>
<li class="toctree-l1"><a class="reference internal" href="application.html">21. Application</a></li>
</ul>
<p class="caption"><span class="caption-text">Deprecated</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="menus.html">Menus</a></li>
<li class="toctree-l1"><a class="reference internal" href="layout-table.html">Table</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">Python GTK+ 3 Tutorial</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="index.html">Docs</a> &raquo;</li>
        
      <li>8. Button Widgets</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            
              <a href="https://github.com/sebp/PyGObject-Tutorial/blob/master/source/button_widgets.txt" class="fa fa-github"> Edit on GitHub</a>
            
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="button-widgets">
<h1>8. Button Widgets<a class="headerlink" href="#button-widgets" title="Permalink to this headline">¶</a></h1>
<div class="section" id="button">
<h2>8.1. Button<a class="headerlink" href="#button" title="Permalink to this headline">¶</a></h2>
<p>The Button widget is another commonly used widget. It is generally used to
attach a function that is called when the button is pressed.</p>
<p>The <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Button.html#Gtk.Button" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.Button</span></code></a> widget can hold any valid child widget. That is it can
hold most any other standard <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Widget.html#Gtk.Widget" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.Widget</span></code></a>. The most commonly used child
is the <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Label.html#Gtk.Label" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.Label</span></code></a>.</p>
<p>Usually, you want to connect to the button&#8217;s &#8220;clicked&#8221; signal which is emitted
when the button has been pressed and released.</p>
<div class="section" id="example">
<h3>8.1.1. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3>
<img alt="_images/button_example.png" src="_images/button_example.png" />
<div class="highlight-default"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gi</span>
<span class="n">gi</span><span class="o">.</span><span class="n">require_version</span><span class="p">(</span><span class="s1">&#39;Gtk&#39;</span><span class="p">,</span> <span class="s1">&#39;3.0&#39;</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">gi.repository</span> <span class="k">import</span> <span class="n">Gtk</span>

<span class="k">class</span> <span class="nc">ButtonWindow</span><span class="p">(</span><span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s2">&quot;Button Demo&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_border_width</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>

        <span class="n">hbox</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Box</span><span class="p">(</span><span class="n">spacing</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">hbox</span><span class="p">)</span>

        <span class="n">button</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Button</span><span class="o">.</span><span class="n">new_with_label</span><span class="p">(</span><span class="s2">&quot;Click Me&quot;</span><span class="p">)</span>
        <span class="n">button</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;clicked&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_click_me_clicked</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">button</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

        <span class="n">button</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Button</span><span class="o">.</span><span class="n">new_with_mnemonic</span><span class="p">(</span><span class="s2">&quot;_Open&quot;</span><span class="p">)</span>
        <span class="n">button</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;clicked&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_open_clicked</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">button</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

        <span class="n">button</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Button</span><span class="o">.</span><span class="n">new_with_mnemonic</span><span class="p">(</span><span class="s2">&quot;_Close&quot;</span><span class="p">)</span>
        <span class="n">button</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;clicked&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_close_clicked</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">button</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">on_click_me_clicked</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">button</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\&quot;</span><span class="s2">Click me</span><span class="se">\&quot;</span><span class="s2"> button was clicked&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">on_open_clicked</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">button</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\&quot;</span><span class="s2">Open</span><span class="se">\&quot;</span><span class="s2"> button was clicked&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">on_close_clicked</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">button</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Closing application&quot;</span><span class="p">)</span>
        <span class="n">Gtk</span><span class="o">.</span><span class="n">main_quit</span><span class="p">()</span>

<span class="n">win</span> <span class="o">=</span> <span class="n">ButtonWindow</span><span class="p">()</span>
<span class="n">win</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;delete-event&quot;</span><span class="p">,</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">main_quit</span><span class="p">)</span>
<span class="n">win</span><span class="o">.</span><span class="n">show_all</span><span class="p">()</span>
<span class="n">Gtk</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</td></tr></table></div>
</div>
</div>
<div class="section" id="togglebutton">
<h2>8.2. ToggleButton<a class="headerlink" href="#togglebutton" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ToggleButton.html#Gtk.ToggleButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.ToggleButton</span></code></a> is very similar to a normal <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Button.html#Gtk.Button" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.Button</span></code></a>,
but when clicked they remain activated, or pressed, until clicked again.
When the state of the button is changed, the &#8220;toggled&#8221; signal is emitted.</p>
<p>To retrieve the state of the <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ToggleButton.html#Gtk.ToggleButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.ToggleButton</span></code></a>, you can use the
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ToggleButton.html#Gtk.ToggleButton.get_active" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.ToggleButton.get_active()</span></code></a> method. This returns <code class="docutils literal"><span class="pre">True</span></code> if the button
is &#8220;down&#8221;. You can also set the toggle button&#8217;s state, with
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ToggleButton.html#Gtk.ToggleButton.set_active" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.ToggleButton.set_active()</span></code></a>. Note that, if you do this, and the state
actually changes, it causes the &#8220;toggled&#8221; signal to be emitted.</p>
<div class="section" id="id1">
<h3>8.2.1. Example<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<img alt="_images/togglebutton_example.png" src="_images/togglebutton_example.png" />
<div class="highlight-default"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gi</span>
<span class="n">gi</span><span class="o">.</span><span class="n">require_version</span><span class="p">(</span><span class="s1">&#39;Gtk&#39;</span><span class="p">,</span> <span class="s1">&#39;3.0&#39;</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">gi.repository</span> <span class="k">import</span> <span class="n">Gtk</span>

<span class="k">class</span> <span class="nc">ToggleButtonWindow</span><span class="p">(</span><span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s2">&quot;ToggleButton Demo&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_border_width</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>

        <span class="n">hbox</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Box</span><span class="p">(</span><span class="n">spacing</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">hbox</span><span class="p">)</span>

        <span class="n">button</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">ToggleButton</span><span class="p">(</span><span class="s2">&quot;Button 1&quot;</span><span class="p">)</span>
        <span class="n">button</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;toggled&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_button_toggled</span><span class="p">,</span> <span class="s2">&quot;1&quot;</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">button</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

        <span class="n">button</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">ToggleButton</span><span class="p">(</span><span class="s2">&quot;B_utton 2&quot;</span><span class="p">,</span> <span class="n">use_underline</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">button</span><span class="o">.</span><span class="n">set_active</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">button</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;toggled&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_button_toggled</span><span class="p">,</span> <span class="s2">&quot;2&quot;</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">button</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">on_button_toggled</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">button</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">button</span><span class="o">.</span><span class="n">get_active</span><span class="p">():</span>
            <span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;on&quot;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;off&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Button&quot;</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="s2">&quot;was turned&quot;</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span>

<span class="n">win</span> <span class="o">=</span> <span class="n">ToggleButtonWindow</span><span class="p">()</span>
<span class="n">win</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;delete-event&quot;</span><span class="p">,</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">main_quit</span><span class="p">)</span>
<span class="n">win</span><span class="o">.</span><span class="n">show_all</span><span class="p">()</span>
<span class="n">Gtk</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</td></tr></table></div>
</div>
</div>
<div class="section" id="checkbutton">
<h2>8.3. CheckButton<a class="headerlink" href="#checkbutton" title="Permalink to this headline">¶</a></h2>
<p><a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/CheckButton.html#Gtk.CheckButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.CheckButton</span></code></a> inherits from <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ToggleButton.html#Gtk.ToggleButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.ToggleButton</span></code></a>. The only real
difference between the two is <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/CheckButton.html#Gtk.CheckButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.CheckButton</span></code></a>&#8216;s appearance.
A <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/CheckButton.html#Gtk.CheckButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.CheckButton</span></code></a> places a discrete <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ToggleButton.html#Gtk.ToggleButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.ToggleButton</span></code></a> next to
a widget, (usually a <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Label.html#Gtk.Label" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.Label</span></code></a>).
The &#8220;toggled&#8221; signal, <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ToggleButton.html#Gtk.ToggleButton.set_active" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.ToggleButton.set_active()</span></code></a> and
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ToggleButton.html#Gtk.ToggleButton.get_active" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.ToggleButton.get_active()</span></code></a> are inherited.</p>
</div>
<div class="section" id="radiobutton">
<h2>8.4. RadioButton<a class="headerlink" href="#radiobutton" title="Permalink to this headline">¶</a></h2>
<p>Like checkboxes, radio buttons also inherit from <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ToggleButton.html#Gtk.ToggleButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.ToggleButton</span></code></a>,
but these work in groups, and only one <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/RadioButton.html#Gtk.RadioButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.RadioButton</span></code></a> in a group can
be selected at any one time. Therefore, a <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/RadioButton.html#Gtk.RadioButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.RadioButton</span></code></a> is one way
of giving the user a choice from many options.</p>
<p>Radio buttons can be created with one of the static methods
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/RadioButton.html#Gtk.RadioButton.new_from_widget" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.RadioButton.new_from_widget()</span></code></a>,
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/RadioButton.html#Gtk.RadioButton.new_with_label_from_widget" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.RadioButton.new_with_label_from_widget()</span></code></a> or
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/RadioButton.html#Gtk.RadioButton.new_with_mnemonic_from_widget" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.RadioButton.new_with_mnemonic_from_widget()</span></code></a>.
The first radio button in a group will be created passing <code class="docutils literal"><span class="pre">None</span></code> as the
<em>group</em> argument. In subsequent calls, the group you
wish to add this button to should be passed as an argument.</p>
<p>When first run, the first radio button in the group will be active.
This can be changed by calling <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ToggleButton.html#Gtk.ToggleButton.set_active" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.ToggleButton.set_active()</span></code></a> with <code class="docutils literal"><span class="pre">True</span></code>
as first argument.</p>
<p>Changing a <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/RadioButton.html#Gtk.RadioButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.RadioButton</span></code></a>&#8216;s widget group after its creation can be
achieved by calling <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/RadioButton.html#Gtk.RadioButton.join_group" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.RadioButton.join_group()</span></code></a>.</p>
<div class="section" id="id2">
<h3>8.4.1. Example<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<img alt="_images/radiobutton_example.png" src="_images/radiobutton_example.png" />
<div class="highlight-default"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gi</span>
<span class="n">gi</span><span class="o">.</span><span class="n">require_version</span><span class="p">(</span><span class="s1">&#39;Gtk&#39;</span><span class="p">,</span> <span class="s1">&#39;3.0&#39;</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">gi.repository</span> <span class="k">import</span> <span class="n">Gtk</span>

<span class="k">class</span> <span class="nc">RadioButtonWindow</span><span class="p">(</span><span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s2">&quot;RadioButton Demo&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_border_width</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>

        <span class="n">hbox</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Box</span><span class="p">(</span><span class="n">spacing</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">hbox</span><span class="p">)</span>

        <span class="n">button1</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">RadioButton</span><span class="o">.</span><span class="n">new_with_label_from_widget</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="s2">&quot;Button 1&quot;</span><span class="p">)</span>
        <span class="n">button1</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;toggled&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_button_toggled</span><span class="p">,</span> <span class="s2">&quot;1&quot;</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">button1</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

        <span class="n">button2</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">RadioButton</span><span class="o">.</span><span class="n">new_from_widget</span><span class="p">(</span><span class="n">button1</span><span class="p">)</span>
        <span class="n">button2</span><span class="o">.</span><span class="n">set_label</span><span class="p">(</span><span class="s2">&quot;Button 2&quot;</span><span class="p">)</span>
        <span class="n">button2</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;toggled&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_button_toggled</span><span class="p">,</span> <span class="s2">&quot;2&quot;</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">button2</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

        <span class="n">button3</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">RadioButton</span><span class="o">.</span><span class="n">new_with_mnemonic_from_widget</span><span class="p">(</span><span class="n">button1</span><span class="p">,</span>
            <span class="s2">&quot;B_utton 3&quot;</span><span class="p">)</span>
        <span class="n">button3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;toggled&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_button_toggled</span><span class="p">,</span> <span class="s2">&quot;3&quot;</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">button3</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">on_button_toggled</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">button</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">button</span><span class="o">.</span><span class="n">get_active</span><span class="p">():</span>
            <span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;on&quot;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;off&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Button&quot;</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="s2">&quot;was turned&quot;</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span>

<span class="n">win</span> <span class="o">=</span> <span class="n">RadioButtonWindow</span><span class="p">()</span>
<span class="n">win</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;delete-event&quot;</span><span class="p">,</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">main_quit</span><span class="p">)</span>
<span class="n">win</span><span class="o">.</span><span class="n">show_all</span><span class="p">()</span>
<span class="n">Gtk</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</td></tr></table></div>
</div>
</div>
<div class="section" id="linkbutton">
<h2>8.5. LinkButton<a class="headerlink" href="#linkbutton" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/LinkButton.html#Gtk.LinkButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.LinkButton</span></code></a> is a <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Button.html#Gtk.Button" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.Button</span></code></a> with a hyperlink, similar
to the one used by web browsers, which triggers an action when clicked. It is
useful to show quick links to resources.</p>
<p>The URI bound to a <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/LinkButton.html#Gtk.LinkButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.LinkButton</span></code></a> can be set specifically using
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/LinkButton.html#Gtk.LinkButton.set_uri" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.LinkButton.set_uri()</span></code></a>, and retrieved using <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/LinkButton.html#Gtk.LinkButton.get_uri" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.LinkButton.get_uri()</span></code></a>.</p>
<div class="section" id="id3">
<h3>8.5.1. Example<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3>
<img alt="_images/linkbutton_example.png" src="_images/linkbutton_example.png" />
<div class="highlight-default"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gi</span>
<span class="n">gi</span><span class="o">.</span><span class="n">require_version</span><span class="p">(</span><span class="s1">&#39;Gtk&#39;</span><span class="p">,</span> <span class="s1">&#39;3.0&#39;</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">gi.repository</span> <span class="k">import</span> <span class="n">Gtk</span>

<span class="k">class</span> <span class="nc">LinkButtonWindow</span><span class="p">(</span><span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s2">&quot;LinkButton Demo&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_border_width</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>

        <span class="n">button</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">LinkButton</span><span class="p">(</span><span class="s2">&quot;http://www.gtk.org&quot;</span><span class="p">,</span> <span class="s2">&quot;Visit GTK+ Homepage&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">button</span><span class="p">)</span>

<span class="n">win</span> <span class="o">=</span> <span class="n">LinkButtonWindow</span><span class="p">()</span>
<span class="n">win</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;delete-event&quot;</span><span class="p">,</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">main_quit</span><span class="p">)</span>
<span class="n">win</span><span class="o">.</span><span class="n">show_all</span><span class="p">()</span>
<span class="n">Gtk</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</td></tr></table></div>
</div>
</div>
<div class="section" id="spinbutton">
<h2>8.6. SpinButton<a class="headerlink" href="#spinbutton" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.SpinButton</span></code></a> is an ideal way to allow the user to set the value of
some attribute. Rather than having to directly type a number into a
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Entry.html#Gtk.Entry" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.Entry</span></code></a>, <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.SpinButton</span></code></a> allows the user to click on one of
two arrows to increment or decrement the displayed value. A value can still be
typed in, with the bonus that it can be checked to ensure it is in a given range.
The main properties of a <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.SpinButton</span></code></a> are set through
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Adjustment.html#Gtk.Adjustment" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.Adjustment</span></code></a>.</p>
<p>To change the value that <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.SpinButton</span></code></a> is showing, use
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton.set_value" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.SpinButton.set_value()</span></code></a>. The value entered can either be an integer or
float, depending on your requirements, use <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton.get_value" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.SpinButton.get_value()</span></code></a> or
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton.get_value_as_int" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.SpinButton.get_value_as_int()</span></code></a>, respectively.</p>
<p>When you allow the displaying of float values in the spin button, you may wish
to adjust the number of decimal spaces displayed by calling
<a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton.set_digits" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.SpinButton.set_digits()</span></code></a>.</p>
<p>By default, <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.SpinButton</span></code></a> accepts textual data. If you wish to limit
this to numerical values only, call <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton.set_numeric" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.SpinButton.set_numeric()</span></code></a> with <code class="docutils literal"><span class="pre">True</span></code>
as argument.</p>
<p>We can also adjust the update policy of <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.SpinButton</span></code></a>. There are two
options here; by default the spin button updates the value even if the data
entered is invalid. Alternatively, we can set the policy to only update when the
value entered is valid by calling <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/SpinButton.html#Gtk.SpinButton.set_update_policy" title="(in Gtk 3.0 v3.0)"><code class="xref py py-meth docutils literal"><span class="pre">Gtk.SpinButton.set_update_policy()</span></code></a>.</p>
<div class="section" id="id4">
<h3>8.6.1. Example<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h3>
<img alt="_images/spinbutton_example.png" src="_images/spinbutton_example.png" />
<div class="highlight-default"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gi</span>
<span class="n">gi</span><span class="o">.</span><span class="n">require_version</span><span class="p">(</span><span class="s1">&#39;Gtk&#39;</span><span class="p">,</span> <span class="s1">&#39;3.0&#39;</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">gi.repository</span> <span class="k">import</span> <span class="n">Gtk</span>

<span class="k">class</span> <span class="nc">SpinButtonWindow</span><span class="p">(</span><span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s2">&quot;SpinButton Demo&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_border_width</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>

        <span class="n">hbox</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Box</span><span class="p">(</span><span class="n">spacing</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">hbox</span><span class="p">)</span>

        <span class="n">adjustment</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Adjustment</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spinbutton</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">SpinButton</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spinbutton</span><span class="o">.</span><span class="n">set_adjustment</span><span class="p">(</span><span class="n">adjustment</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spinbutton</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

        <span class="n">check_numeric</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">CheckButton</span><span class="p">(</span><span class="s2">&quot;Numeric&quot;</span><span class="p">)</span>
        <span class="n">check_numeric</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;toggled&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_numeric_toggled</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">check_numeric</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

        <span class="n">check_ifvalid</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">CheckButton</span><span class="p">(</span><span class="s2">&quot;If Valid&quot;</span><span class="p">)</span>
        <span class="n">check_ifvalid</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;toggled&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_ifvalid_toggled</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">check_ifvalid</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">on_numeric_toggled</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">button</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spinbutton</span><span class="o">.</span><span class="n">set_numeric</span><span class="p">(</span><span class="n">button</span><span class="o">.</span><span class="n">get_active</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">on_ifvalid_toggled</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">button</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">button</span><span class="o">.</span><span class="n">get_active</span><span class="p">():</span>
            <span class="n">policy</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">SpinButtonUpdatePolicy</span><span class="o">.</span><span class="n">IF_VALID</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">policy</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">SpinButtonUpdatePolicy</span><span class="o">.</span><span class="n">ALWAYS</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spinbutton</span><span class="o">.</span><span class="n">set_update_policy</span><span class="p">(</span><span class="n">policy</span><span class="p">)</span>

<span class="n">win</span> <span class="o">=</span> <span class="n">SpinButtonWindow</span><span class="p">()</span>
<span class="n">win</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;delete-event&quot;</span><span class="p">,</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">main_quit</span><span class="p">)</span>
<span class="n">win</span><span class="o">.</span><span class="n">show_all</span><span class="p">()</span>
<span class="n">Gtk</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</td></tr></table></div>
</div>
</div>
<div class="section" id="switch">
<h2>8.7. Switch<a class="headerlink" href="#switch" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference external" href="https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Switch.html#Gtk.Switch" title="(in Gtk 3.0 v3.0)"><code class="xref py py-class docutils literal"><span class="pre">Gtk.Switch</span></code></a> is a widget that has two states: on or off. The user can
control which state should be active by clicking the empty area, or by dragging
the handle.</p>
<p>You shouldn&#8217;t use the &#8220;activate&#8221; signal on the Gtk.Switch which is an action
signal and emitting it causes the switch to animate. Applications should never
connect to this signal, but use the &#8220;notify::active&#8221; signal, see the example
here below.</p>
<div class="section" id="id5">
<h3>8.7.1. Example<a class="headerlink" href="#id5" title="Permalink to this headline">¶</a></h3>
<img alt="_images/switch_example.png" src="_images/switch_example.png" />
<div class="highlight-default"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gi</span>
<span class="n">gi</span><span class="o">.</span><span class="n">require_version</span><span class="p">(</span><span class="s1">&#39;Gtk&#39;</span><span class="p">,</span> <span class="s1">&#39;3.0&#39;</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">gi.repository</span> <span class="k">import</span> <span class="n">Gtk</span>

<span class="k">class</span> <span class="nc">SwitcherWindow</span><span class="p">(</span><span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s2">&quot;Switch Demo&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_border_width</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>

        <span class="n">hbox</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Box</span><span class="p">(</span><span class="n">spacing</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">hbox</span><span class="p">)</span>

        <span class="n">switch</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Switch</span><span class="p">()</span>
        <span class="n">switch</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;notify::active&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_switch_activated</span><span class="p">)</span>
        <span class="n">switch</span><span class="o">.</span><span class="n">set_active</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">switch</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

        <span class="n">switch</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Switch</span><span class="p">()</span>
        <span class="n">switch</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;notify::active&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_switch_activated</span><span class="p">)</span>
        <span class="n">switch</span><span class="o">.</span><span class="n">set_active</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">hbox</span><span class="o">.</span><span class="n">pack_start</span><span class="p">(</span><span class="n">switch</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">on_switch_activated</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">switch</span><span class="p">,</span> <span class="n">gparam</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">switch</span><span class="o">.</span><span class="n">get_active</span><span class="p">():</span>
            <span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;on&quot;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">state</span> <span class="o">=</span> <span class="s2">&quot;off&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Switch was turned&quot;</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span>

<span class="n">win</span> <span class="o">=</span> <span class="n">SwitcherWindow</span><span class="p">()</span>
<span class="n">win</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;delete-event&quot;</span><span class="p">,</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">main_quit</span><span class="p">)</span>
<span class="n">win</span><span class="o">.</span><span class="n">show_all</span><span class="p">()</span>
<span class="n">Gtk</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>

</pre></div>
</td></tr></table></div>
</div>
</div>
</div>


           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="progressbar.html" class="btn btn-neutral float-right" title="9. ProgressBar" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="entry.html" class="btn btn-neutral" title="7. Entry" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright GNU Free Documentation License 1.3.
      
        <span class="commit">
          Revision <code>a65390fa</code>.
        </span>
      

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <div class="rst-versions" data-toggle="rst-versions" role="note" aria-label="versions">
    <span class="rst-current-version" data-toggle="rst-current-version">
      <span class="fa fa-book"> Read the Docs</span>
      v: latest
      <span class="fa fa-caret-down"></span>
    </span>
    <div class="rst-other-versions">
      <dl>
        <dt>Versions</dt>
        
          <dd><a href="/en/latest/">latest</a></dd>
        
      </dl>
      <dl>
        <dt>Downloads</dt>
        
          <dd><a href="//readthedocs.org/projects/python-gtk-3-tutorial/downloads/pdf/latest/">pdf</a></dd>
        
          <dd><a href="//readthedocs.org/projects/python-gtk-3-tutorial/downloads/htmlzip/latest/">htmlzip</a></dd>
        
          <dd><a href="//readthedocs.org/projects/python-gtk-3-tutorial/downloads/epub/latest/">epub</a></dd>
        
      </dl>
      <dl>
        <dt>On Read the Docs</dt>
          <dd>
            <a href="//readthedocs.org/projects/python-gtk-3-tutorial/?fromdocs=python-gtk-3-tutorial">Project Home</a>
          </dd>
          <dd>
            <a href="//readthedocs.org/builds/python-gtk-3-tutorial/?fromdocs=python-gtk-3-tutorial">Builds</a>
          </dd>
      </dl>
      <hr/>
      Free document hosting provided by <a href="http://www.readthedocs.org">Read the Docs</a>.

    </div>
  </div>



  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'./',
            VERSION:'3.4',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="https://media.readthedocs.org/javascript/jquery/jquery-2.0.3.min.js"></script>
      <script type="text/javascript" src="https://media.readthedocs.org/javascript/jquery/jquery-migrate-1.2.1.min.js"></script>
      <script type="text/javascript" src="https://media.readthedocs.org/javascript/underscore.js"></script>
      <script type="text/javascript" src="https://media.readthedocs.org/javascript/doctools.js"></script>
      <script type="text/javascript" src="https://media.readthedocs.org/javascript/readthedocs-doc-embed.js"></script>

  

  
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>