title: I need example code for a simple python-gui script with gtk+
body: <p>I'm new to Python and OOP and need a sample script to get an understanding of how the gtk.builder objects and window objects relate. I'm using gnome-builder to get started.</p>
  
  <p>What I'd like is to load the gui definition from xml as produced by builder (or Glade): simple stuff really :</p>
  
  <p>Window to have a button and a label. When button is clicked label is toggled as shown or hidden. However, the label (when shown) should be a continuously changing random letter.</p>
  
  <p>The following code is from the Gnome builder hello world with gui changed to my needs.</p>
  
  <p>main.py :</p>
  
  <pre><code>import sys
  import gi
  
  gi.require_version('Gtk', '3.0')
  
  from gi.repository import Gtk, Gio
  
  from .window import TestWindow
  
  
  class Application(Gtk.Application):
      def __init__(self):
          super().__init__(application_id='test',
                           flags=Gio.ApplicationFlags.FLAGS_NONE)
  
      def do_activate(self):
          win = self.props.active_window
          if not win:
              win = TestWindow(application=self)
          win.present()
  
  
  def main(version):
      app = Application()
      return app.run(sys.argv)
  </code></pre>
  
  <p>window.py:</p>
  
  <pre><code>from gi.repository import Gtk
  
  
  @Gtk.Template(resource_path='/test/window.ui')
  class TestWindow(Gtk.ApplicationWindow):
      __gtype_name__ = 'TestWindow'
  
      label = Gtk.Template.Child()
  
      def __init__(self, **kwargs):
          super().__init__(**kwargs)
  </code></pre>
  
  <p>window.ui:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!-- Generated with glade 3.22.1 --&gt;
  &lt;interface&gt;
    &lt;requires lib="gtk+" version="3.20"/&gt;
    &lt;template class="TestWindow" parent="GtkApplicationWindow"&gt;
      &lt;property name="can_focus"&gt;False&lt;/property&gt;
      &lt;property name="default_width"&gt;600&lt;/property&gt;
      &lt;property name="default_height"&gt;300&lt;/property&gt;
      &lt;child type="titlebar"&gt;
        &lt;placeholder/&gt;
      &lt;/child&gt;
      &lt;child&gt;
        &lt;object class="GtkBox"&gt;
          &lt;property name="visible"&gt;True&lt;/property&gt;
          &lt;property name="can_focus"&gt;False&lt;/property&gt;
          &lt;property name="orientation"&gt;vertical&lt;/property&gt;
          &lt;child&gt;
            &lt;object class="GtkLabel" id="label"&gt;
              &lt;property name="visible"&gt;True&lt;/property&gt;
              &lt;property name="can_focus"&gt;False&lt;/property&gt;
              &lt;property name="label" translatable="yes"&gt;label&lt;/property&gt;
            &lt;/object&gt;
            &lt;packing&gt;
              &lt;property name="expand"&gt;False&lt;/property&gt;
              &lt;property name="fill"&gt;True&lt;/property&gt;
              &lt;property name="position"&gt;0&lt;/property&gt;
            &lt;/packing&gt;
          &lt;/child&gt;
          &lt;child&gt;
            &lt;object class="GtkButton" id="button"&gt;
              &lt;property name="label" translatable="yes"&gt;button&lt;/property&gt;
              &lt;property name="visible"&gt;True&lt;/property&gt;
              &lt;property name="can_focus"&gt;True&lt;/property&gt;
              &lt;property name="receives_default"&gt;True&lt;/property&gt;
            &lt;/object&gt;
            &lt;packing&gt;
              &lt;property name="expand"&gt;False&lt;/property&gt;
              &lt;property name="fill"&gt;True&lt;/property&gt;
              &lt;property name="position"&gt;1&lt;/property&gt;
            &lt;/packing&gt;
          &lt;/child&gt;
          &lt;child&gt;
            &lt;placeholder/&gt;
          &lt;/child&gt;
        &lt;/object&gt;
      &lt;/child&gt;
    &lt;/template&gt;
  &lt;/interface&gt;
  </code></pre>