oschoudhury
oschoudhury

Reputation: 1146

Gtk3 Python - permanently visible SearchBar

I am trying to have a small application with a permanently visible searchbar. The current implementation of Gtk.SearchEntry is that the searchbar disappears from the window when you press Esc. I have it kind of working in the following MWA by adding a on_key_release-event handler, but it is nagging me that when you press Esc, the searchbar will blink away for a moment and then re-appear. It will actually stay invisible as long as you keep Esc pressed in.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk


class AppWindow(Gtk.ApplicationWindow):
    def __init__(self):
        super(AppWindow, self).__init__()
        self.grid = Gtk.Grid()
        self.add(self.grid)
        self.searchbar = Gtk.SearchBar()
        self.grid.attach(self.searchbar, 1, 1, 1, 1)
        if not self.searchbar.get_search_mode():
            self.searchbar.set_search_mode(True)

        self.searchentry = Gtk.SearchEntry()
        self.searchbar.connect_entry(self.searchentry)
        self.searchbar.add(self.searchentry)
        self.searchbar.set_show_close_button(False)
        self.connect("key-release-event", self._on_key_release)

    def _on_key_release(self, widget, event):
        keyname = Gdk.keyval_name(event.keyval)
        if keyname == 'Escape':
            self.searchbar.set_visible(True)
            self.searchbar.set_search_mode(True)
        if event.state and Gdk.ModifierType.CONTROL_MASK:
            if keyname == 'f':
                self.searchbar.set_search_mode(True)


class Application(Gtk.Application):
    def __init__(self):
        super(Application, self).__init__()

    def do_activate(self):
        self.win = AppWindow()
        self.win.show()
        self.win.connect("delete-event", Gtk.main_quit)
        self.win.show_all()

        Gtk.main()

if __name__ == '__main__':
    app = Application()
    app.run()

Is it possible that instead of making the whole searchbar blink, I only see the current search-text go away without any blinking?

Upvotes: 3

Views: 689

Answers (1)

theGtknerd
theGtknerd

Reputation: 3745

If you want the search entry visible all the time, stop using a SearchBar. Just add the SearchEntry directly to your container.

class AppWindow(Gtk.ApplicationWindow):
    def __init__(self):
        super(AppWindow, self).__init__()
        self.grid = Gtk.Grid()
        self.add(self.grid)

        self.searchentry = Gtk.SearchEntry()
        self.grid.attach(self.searchentry, 1, 1, 1, 1)
        self.connect("key-release-event", self._on_key_release)}

You will probably have to set some margins or spacing to achieve a look that suits you.

Upvotes: 4

Related Questions