0

I want to create a suggestions drop down for a SearchEntry object in Gtk4 using Python.

In Gtk3 I can do the following to achieve my desired result:

import gi

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

urls = [
    "http://www.google.com",
    "http://www.google.com/android",
    "http://www.greatstuff.com",
    "http://www.facebook.com",
]
liststore = gtk.ListStore(str)
for s in urls:
    liststore.append([s])

completion = gtk.EntryCompletion()
completion.set_model(liststore)
completion.set_text_column(0)
completion.set_minimum_key_length(0)


def match_anywhere(completion, entrystr, iter, data):
    modelstr = completion.get_model()[iter][0]
    return entrystr in modelstr


completion.set_match_func(match_anywhere, None)

entry = gtk.SearchEntry()
entry.set_completion(completion)


# Show completion suggestions when the entry is focused
def on_focus_in(widget, event):
    widget.emit("changed")


entry.connect("focus-in-event", on_focus_in)


header = gtk.HeaderBar()
header.set_show_close_button(True)
header.set_title("Search Entry")
header.set_custom_title(entry)

# boilerplate
window = gtk.Window()
window.set_titlebar(header)

window.connect("destroy", lambda w: gtk.main_quit())
window.show_all()
gtk.main()

However, this does not work in Gtk4, with the following error:

AttributeError: 'SearchEntry' object has no attribute 'set_completion'

Is there another way I could achieve the same result in Gtk4?

Tobias P. G.
  • 827
  • 8
  • 15

0 Answers0