Search code examples
pythongtkpygobject

Gtk3 and Python - Box doesn't resize to fill window


I'm writing an app using Gtk3 and Python. I have a revealer as a sidebar to select the content and a webkit webview to display the main content. When the revealer is hidden the webview doesn't fill the entire window space and I don't know why. Any help would be appreciated.

from gi.repository import Gtk, Gio
from gi.repository import WebKit

HEIGHT = 500
WIDTH = 800

class MainWindow(Gtk.Window):

    def __init__(self):

        Gtk.Window.__init__(self, title="Resolution")
        self.set_border_width(0)
        self.set_default_size(WIDTH, HEIGHT)

        hb = Gtk.HeaderBar()
        hb.props.show_close_button = True
        hb.props.title = "Resolution"
        self.set_titlebar(hb)

        button = Gtk.Button()   
        icon = Gio.ThemedIcon(name="emblem-system-symbolic")
        image = Gtk.Image.new_from_gicon(icon, 1)
        button.add(image)
        button.connect("clicked", self.sidebarShowHide)
        button.set_focus_on_click(False)
        hb.pack_start(button)  

        sidebarbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
        toplevelbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)

        self.add(toplevelbox)

        self.sidebar = Gtk.Revealer()
        self.sidebar.set_transition_duration(0)
        self.sidebar.set_reveal_child(False)
        toplevelbox.pack_start(self.sidebar, False, False, 0)
        self.sidebar.add(sidebarbox)

        self.searchentry = Gtk.SearchEntry()
        self.searchentry.connect("search-changed", self.search_changed)
        sidebarbox.pack_start(self.searchentry, False, False, 0)

        label = Gtk.Label("Contents Selector")
        sidebarbox.pack_start(label, True, True, 0)

        scroller = Gtk.ScrolledWindow()
        content = WebKit.WebView()
        scroller.add(content)
        toplevelbox.pack_start(scroller, True, True, 0)

        content.open("/home/oliver/resolution/placeholder.html")

    def sidebarShowHide(self, button):
        if self.sidebar.get_reveal_child():
            self.sidebar.set_reveal_child(False)
        else:
            self.sidebar.set_reveal_child(True)

    def search_changed(self, searchentry):
        pass




win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Solution

  • Well, i have done some GtkRevealer few months ago and it works. It drive me nuts to see this piece of code was not.

    I opened my project again and look inside where that part is, and it turn out the toplevel container where the Gtk.Revealer resides, has to has Gtk.Orientation.VERTICAL.. if you change your "toplevelbox" orientation to that, it will work, but it wont be sidebar. It will coming from top or bottom. It goes the same if you change GtkBox with GtkGrid. If I were to guess it depends on the children default orientation.

    Workaround on that, is to use widget hide/show mechanism (believe me, i ransack your code and it works).

    from gi.repository import Gtk, Gio
    from gi.repository import WebKit
    
    HEIGHT = 500
    WIDTH = 800
    
    class MainWindow(Gtk.Window):
    
     def __init__(self):
    
        Gtk.Window.__init__(self, title="Resolution")
        self.set_border_width(0)
        self.set_default_size(WIDTH, HEIGHT)
    
        hb = Gtk.HeaderBar()
        hb.props.show_close_button = True
        hb.props.title = "Resolution"
        self.set_titlebar(hb)
    
        button = Gtk.Button()   
        icon = Gio.ThemedIcon(name="emblem-system-symbolic")
        image = Gtk.Image.new_from_gicon(icon, 1)
        button.add(image)
        button.connect("clicked", self.sidebarShowHide)
        button.set_focus_on_click(False)
        hb.pack_start(button)  
    
        sidebarbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
        toplevelbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
    
        self.add(toplevelbox)
    
        self.sidebar = Gtk.Box()     
        toplevelbox.pack_start(self.sidebar, False, False, 0)
        self.sidebar.add(sidebarbox)
    
        self.searchentry = Gtk.SearchEntry()
        self.searchentry.connect("search-changed", self.search_changed)
        sidebarbox.pack_start(self.searchentry, False, False, 0)
    
        label = Gtk.Label("Contents Selector")
        sidebarbox.pack_start(label, True, True, 0)
    
        scroller = Gtk.ScrolledWindow()
        content = WebKit.WebView()
        scroller.add(content)
        toplevelbox.pack_start(scroller, True, True, 0)
    
        content.open("/home/oliver/resolution/placeholder.html")
    
    def sidebarShowHide(self, button):
        if self.sidebar.get_visible():
            self.sidebar.hide ()
        else:
            self.sidebar.show ()
    
    def search_changed(self, searchentry):
        pass
    
    
    
    
    win = MainWindow()
    win.connect("delete-event", Gtk.main_quit)  
    win.show_all()
    Gtk.main()