GTK GUI with PyGObject (7)

Date: 2021/07/19 (initial publish), 2021/07/24 (last update)

Source: en/note-00020.md

Previous Post Top Next Post

TOC

Example: GtkIconView

Let’s go through another notable widget GtkIconView.

Let’s create icon-view.py/icon-view.ui example. It took me a while to make these working since existing examples usually don’t use Glade nor Gtk.Template.

This is very much like GtkTreeView example.

icon-view.py:

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository.GdkPixbuf import Pixbuf

@Gtk.Template(filename="icon-view.ui")
class SimpleWindow(Gtk.Window):

    # corresponding name in XML 'class' attribute for this class
    __gtype_name__ = "icon-views"
    # corresponding name in XML 'id' attribute sets this class member variable
    liststore1 = Gtk.Template.Child() # MODEL
    iconview = Gtk.Template.Child()   # VIEW
    button = Gtk.Template.Child()

    def __init__(self):
        super().__init__()
        renderer_pixbuf = Gtk.CellRendererPixbuf()
        self.iconview.pack_start(renderer_pixbuf, True)
        self.iconview.add_attribute(renderer_pixbuf, "pixbuf", 0)
        renderer_text = Gtk.CellRendererText()
        self.iconview.pack_start(renderer_text, True)
        self.iconview.add_attribute(renderer_text, "text", 1)

    @Gtk.Template.Callback()
    def onDestroy(self, *args):
        Gtk.main_quit()

    @Gtk.Template.Callback()
    def onButtonPressed(self, widget):
        print("=" * 80)

window = SimpleWindow()
window.show_all()
Gtk.main()

icon-view.ui:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkListStore" id="liststore1">
    <columns>
      <!-- column-name GdkPixbuf1 -->
      <column type="GdkPixbuf"/>
      <!-- column-name gchararray1 -->
      <column type="gchararray"/>
    </columns>
    <data>
      <row>
        <col id="0">edit-cut.png</col>
        <col id="1">cut</col>
      </row>
      <row>
        <col id="0">edit-paste.png</col>
        <col id="1">paste</col>
      </row>
      <row>
        <col id="0">edit-copy.png</col>
        <col id="1">copy</col>
      </row>
    </data>
  </object>
  <template class="icon-views" parent="GtkWindow">
    <property name="can-focus">False</property>
    <property name="title" translatable="yes">ICONVIEW EXAMPLES</property>
    <signal name="destroy" handler="onDestroy" swapped="no"/>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="orientation">vertical</property>
        <property name="baseline-position">top</property>
        <child>
          <object class="GtkLabel" id="label_county">
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="label" translatable="yes">Icons</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkIconView" id="iconview">
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="margin">6</property>
            <property name="model">liststore1</property>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button">
            <property name="label" translatable="yes">Print</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <signal name="pressed" handler="onButtonPressed" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </template>
</interface>

Previous Post Top Next Post