Ir para o conteúdo

Python e GTK 3: PyGObject Gtk.SearchBar()

Utilizando Gtk.SearchBar() com a linguagem de programação Python (PyGObject).

Exemplo

Python PyGObject Gtk.SearchBar()

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
"""Python e GTK 3: PyGObject Gtk.SearchBar()."""

import gi

gi.require_version(namespace='Gtk', version='3.0')

from gi.repository import Gtk, Gio


class MainWindow(Gtk.ApplicationWindow):
    _current_button_state = False

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.set_title(title='Gtk.SearchBar')
        self.set_default_size(width=1366 / 2, height=768 / 2)
        self.set_position(position=Gtk.WindowPosition.CENTER)
        self.set_default_icon_from_file(filename='../../assets/icons/icon.png')
        # Conectando o evento de tecla pressionada à janela principal.
        self.connect('key-press-event', self.key_press_event)

        header_bar = Gtk.HeaderBar.new()
        header_bar.set_title(title='Gtk.SearchBar')
        header_bar.set_subtitle(subtitle='Gtk.SearchBar')
        header_bar.set_show_close_button(setting=True)
        self.set_titlebar(titlebar=header_bar)

        btn_icon_search = Gtk.Image.new_from_icon_name(
            icon_name='system-search-symbolic',
            size=Gtk.IconSize.BUTTON,
        )

        self.btn_search = Gtk.ToggleButton.new()
        self.btn_search.set_image(image=btn_icon_search)
        self.btn_search.connect('clicked', self.on_btn_search_clicled)
        header_bar.pack_start(child=self.btn_search)

        vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=0)
        self.add(widget=vbox)

        search_entry = Gtk.SearchEntry.new()

        self.search_bar = Gtk.SearchBar.new()
        self.search_bar.connect_entry(entry=search_entry)
        self.search_bar.add(widget=search_entry)
        vbox.pack_start(child=self.search_bar, expand=False, fill=True, padding=0)

        label = Gtk.Label.new(
            str='Pressione <b><tt>Ctrl+f</tt></b> para abrir a pesquisa ou '
                'clique no ícone de pesquisa na barra de título.',
        )
        label.set_use_markup(setting=True)
        vbox.pack_start(child=label, expand=True, fill=True, padding=0)

        self.show_all()

    def _show_hidden_search_bar(self):
        if self.search_bar.get_search_mode():
            self.search_bar.set_search_mode(search_mode=False)
        else:
            self.search_bar.set_search_mode(search_mode=True)

    def _change_button_state(self):
        if self._current_button_state:
            self.btn_search.set_state_flags(flags=Gtk.StateFlags.NORMAL, clear=True)
            self._current_button_state = False
        else:
            self.btn_search.set_state_flags(flags=Gtk.StateFlags.ACTIVE, clear=True)
            self._current_button_state = True

    def on_btn_search_clicled(self, widget):
        self._current_button_state = widget.get_active()
        self._show_hidden_search_bar()

    def key_press_event(self, widget, event):
        shortcut = Gtk.accelerator_get_label(event.keyval, event.state)
        if shortcut in ('Ctrl+F', 'Ctrl+Mod2+F'):
            self._show_hidden_search_bar()
            self._change_button_state()
        if shortcut == 'Mod2+Esc' and self.search_bar.get_search_mode():
            self._show_hidden_search_bar()
            self._change_button_state()
        return True


class Application(Gtk.Application):

    def __init__(self):
        super().__init__(application_id='br.natorsc.Exemplo',
                         flags=Gio.ApplicationFlags.FLAGS_NONE)

    def do_startup(self):
        Gtk.Application.do_startup(self)

    def do_activate(self):
        win = self.props.active_window
        if not win:
            win = MainWindow(application=self)
        win.present()

    def do_shutdown(self):
        Gtk.Application.do_shutdown(self)


if __name__ == '__main__':
    import sys

    app = Application()
    app.run(sys.argv)