3

I am trying to get entry completion to work with Gtk on Python 3, but I seem to miss some points when using Glade to construct the interface.

When using a layout based on a Glade file, I get a list of suggestions, but cannot read them since the suggestion texts do not show up. Clicking on one entry to select it works and it gets displayed as entry text. Constructing the interface from scratch without Glade does not show this behaviour.

This is the Python code I use:

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


def glade_based():
    builder = Gtk.Builder()
    builder.add_from_file("test.glade")

    liststore = builder.get_object("liststore1")
    for i in range(10):
        liststore.append(["a" * i])

    window = builder.get_object("window1")
    window.connect("destroy", lambda w: Gtk.main_quit())
    window.show_all()
    Gtk.main()


def from_scratch():
    liststore = Gtk.ListStore(str)
    for i in range(10):
        liststore.append(["a" * i])

    completion = Gtk.EntryCompletion()
    completion.set_model(liststore)
    completion.set_text_column(0)

    entry = Gtk.Entry()
    entry.set_completion(completion)

    window = Gtk.Window()
    window.add(entry)
    window.connect("destroy", lambda w: Gtk.main_quit())
    window.show_all()
    Gtk.main()


if __name__ == "__main__":
    import sys
    if len(sys.argv) > 1:
        from_scratch()
    else:
        glade_based()

If there is no command line argument, the layout gets loaded from the following Glade file:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkListStore" id="liststore1">
    <columns>
      <!-- column-name column1 -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkEntryCompletion" id="entrycompletion1">
    <property name="model">liststore1</property>
    <property name="text_column">0</property>
  </object>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkEntry" id="entry1">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="invisible_char">●</property>
        <property name="completion">entrycompletion1</property>
      </object>
    </child>
  </object>
</interface>

If there are additional command line parameters, the code creates the same layout from scratch.

How to get the Glade version to work as expected?

epR8GaYuh
  • 311
  • 9
  • 21

1 Answers1

4

You need to right click the EntryCompletion and add a cellrenderer to your completion object. I can't tell you why it works in Python. Here is the proper Glade file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.2"/>
  <object class="GtkListStore" id="liststore1">
    <columns>
      <!-- column-name column1 -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkEntryCompletion" id="entrycompletion1">
    <property name="model">liststore1</property>
    <property name="text_column">0</property>
    <child>
      <object class="GtkCellRendererText" id="cellrenderertext1"/>
      <attributes>
        <attribute name="text">0</attribute>
      </attributes>
    </child>
  </object>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkEntry" id="entry1">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="invisible_char">●</property>
        <property name="completion">entrycompletion1</property>
      </object>
    </child>
  </object>
</interface>
theGtknerd
  • 3,647
  • 1
  • 13
  • 34