Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Afacanc38/76ce9b3260307bea64ebf3506b485147 to your computer and use it in GitHub Desktop.
Save Afacanc38/76ce9b3260307bea64ebf3506b485147 to your computer and use it in GitHub Desktop.
Gtk4 SearchBar Example Python 2022
# Thanks to @keremgokcek for helping to make the search action.
import gi, sys
gi.require_version (
'Gtk',
'4.0'
)
gi.require_version (
'Adw',
'1'
)
from gi.repository import Gtk, Adw, GLib, Gio, Gdk
class MainWindow (Gtk.ApplicationWindow):
def __init__ (self, *args, **kwargs):
super ().__init__ (*args, **kwargs)
GLib.set_prgname ("GTK Search Bar denemesi.")
GLib.set_application_name ("GTK Search Bar denemesi.")
self.box_main = Gtk.Box (orientation = Gtk.Orientation.VERTICAL)
self.set_child (self.box_main)
self.hb = Gtk.HeaderBar ()
self.set_titlebar (self.hb)
self.tgl_search = Gtk.ToggleButton (
icon_name = 'system-search-symbolic'
)
self.tgl_search.connect (
'toggled',
self.on_tgl_search_toggle
)
self.hb.pack_start (self.tgl_search)
self.ent_sb = Gtk.SearchEntry (
placeholder_text = "Ne aramıştınız?"
)
self.sb = Gtk.SearchBar (
halign = Gtk.Align.FILL,
hexpand = True,
valign = Gtk.Align.START,
show_close_button = True
)
self.sb.connect_entry (self.ent_sb)
self.sb.set_child (self.ent_sb)
self.box_main.append (self.sb)
self.sb.set_key_capture_widget (
self.ent_sb
)
self.controller = Gtk.EventControllerKey()
self.controller.connect('key-released', self.on_key_released)
self.add_controller(self.controller)
self.listbox = Gtk.ListBox (
margin_top = 10,
margin_bottom = 10,
margin_start = 10,
margin_end = 10
)
self.listbox.get_style_context ().add_class ('boxed-list')
self.box_main.append (self.listbox)
self.row1 = Adw.ActionRow (
title = "Yardım"
)
self.listbox.append(self.row1)
self.row2 = Adw.ActionRow (
title = "Oyun"
)
self.listbox.append(self.row2)
self.row3 = Adw.ActionRow (
title = "Hakkında"
)
self.listbox.append(self.row3)
self.row4 = Adw.ActionRow (
title = "Hıyar"
)
self.listbox.append(self.row4)
self.ent_sb.connect('changed', self.on_sb_change)
def on_key_released(self, widget, keyval, keycode, state):
keyname = Gdk.keyval_name(keyval)
if Gdk.ModifierType.CONTROL_MASK & state and keyname == 'f':
if self.sb.get_search_mode():
self.sb.set_search_mode(False)
else:
self.sb.set_search_mode(True)
def sort_func(self, row1: Adw.ActionRow, row2: Adw.ActionRow):
if row1.get_title() < row2.get_title():
return -1
elif row1.get_title() > row2.get_title():
return 1
else:
return 0
def filter_func(self, row):
return row.get_title().lower().startswith(self.ent_sb.get_text().lower())
def on_sb_change(self, widget):
self.listbox.invalidate_filter()
self.listbox.set_filter_func(self.filter_func)
self.listbox.invalidate_sort()
self.listbox.set_sort_func(sort_func=self.sort_func)
def on_tgl_search_toggle (self, widget):
if self.tgl_search.get_active ():
self.sb.set_search_mode (True)
else:
self.sb.set_search_mode (False)
class MyApp (Adw.Application):
def __init__ (self, **kwargs):
super ().__init__ (**kwargs)
self.connect (
'activate',
self.on_activate
)
def on_activate (self, app):
win = MainWindow (
application = app
)
win.present ()
app = MyApp (
application_id = 'io.github.napim'
)
app.run (sys.argv)
@iman-salmani
Copy link

hi
thanks for this code
you don't have to set sort function after call invalidate_sort()

also filter function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment