SearchEntry sizing inside a header bar

Hi,

Not vital for my application but I would like the Gtk.SearchEntry() widget to fill / expand. I tried several setting but without any success.

Below is the code I am using:

    def set_header_bar(self):
        self.header_bar = Gtk.HeaderBar()
        self.header_bar.set_show_title_buttons(True)

        self.button_open = Gtk.Button.new_from_icon_name("document-open-symbolic")
        self.button_open.connect("clicked", self.on_button_open_clicked)
        self.header_bar.pack_start(self.button_open)

        self.search_entry = Gtk.SearchEntry()
        self.search_entry.connect("changed", self.app_column_view.on_search_entry_changed)
        self.header_bar.pack_start(self.search_entry)

        self.popover = Gtk.Popover()
        self.menu_button = Gtk.MenuButton()
        self.menu_button.set_icon_name("view-more-symbolic")
        self.menu_button.set_popover(self.popover)
        self.header_bar.pack_end(self.menu_button)

        self.button_refresh = Gtk.Button.new_from_icon_name("view-refresh-symbolic")
        self.button_refresh.connect("clicked", self.on_button_refresh_clicked)
        self.header_bar.pack_end(self.button_refresh)

        self.spinner = Gtk.Spinner()
        self.header_bar.pack_end(self.spinner)

        self.set_titlebar(self.header_bar)

Kind regards,
Vincent

I’m not that familiar with GTK3 (which your app seems to use) but in GTK4, doing something like

self.search_entry = Gtk.SearchEntry(hexpand=True)

would do the trick. Try it. Maybe, it will work in GTK3 too.

Hi @mazharhussain,

Actually I am using Gtk4. I tried your suggested but it has no effect.

Regards

Then maybe you need to do something like

title_widget = self.header_bar.get_title_widget()
title_widget.set_hexpand(False)

Along with my previous solution.

@mazharhussain,

Well, it’s not better with your suggestion. The title widget is not expanded but neither the entry widget. I tried with a Gtk.CenterBox() and it’s working as expected. However, with this widget I don’t have the native title buttons and the ability to move the window.

Some related topic but quite old: python - Gtk HeaderBar doesn't expand children - Stack Overflow

Code extract:

    def set_header_bar(self):
        header_bar = Gtk.HeaderBar()
        header_bar.set_show_title_buttons(True)

        title_widget = Gtk.Label()
        title_widget.set_markup(f"<b>{Config.PROGRAM_NAME}</b>")
        title_widget.set_hexpand(False)
        header_bar.set_title_widget(title_widget)

        # button_open = Gtk.Button.new_from_icon_name("document-open-symbolic")
        # button_open.connect("clicked", self.on_button_open_clicked)
        # header_bar.pack_start(button_open)

        search_entry = Gtk.SearchEntry()
        search_entry.set_hexpand(True)
        search_entry.connect("changed", self.app_column_view.on_search_entry_changed)
        header_bar.pack_start(search_entry)

        # popover = Gtk.Popover()
        # menu_button = Gtk.MenuButton()
        # menu_button.set_icon_name("view-more-symbolic")
        # menu_button.set_popover(popover)
        # header_bar.pack_end(menu_button)

        # button_refresh = Gtk.Button.new_from_icon_name("view-refresh-symbolic")
        # button_refresh.connect("clicked", self.on_button_refresh_clicked)
        # header_bar.pack_end(button_refresh)

        # self.spinner = Gtk.Spinner()
        # header_bar.pack_end(self.spinner)

        self.set_titlebar(header_bar)

Kind regards,
Vincent

Make CenterBox a child of Gtk.WindowHandle to regain the ability to move the window

And in order to regain title buttons, addGtk.WindowControls(side='start') as first child of the start wigdet of the CenterBox and Gtk.WindowControls(side='end') as last child of the end widget of CenterBox.

Note: Even though we added WindowControls widget on both sides, it will only show up on one side according to the user’s preferences.

Note: The user preference may be to have controls on both sides

1 Like

Hi @mazharhussain,

Thanks a lot for helping. I gave a try of your suggestion and it’s working fine.
However, I think I will stick to the native HeaderBar widget and let the SearchEntry not expanded.

Kind regards,
Vincent

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.