Gtk3 和 Python - Box 不会调整大小以填充窗口



我正在使用Gtk3和Python编写一个应用程序。我有一个揭示器作为侧边栏来选择内容和webkit webview来显示主要内容。当显示器被隐藏时,webview没有填满整个窗口空间,我不知道为什么。如有任何帮助,不胜感激。

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()

好吧,我几个月前做了一些GtkRevealer,它工作。看到这段代码不是这样,我简直要发疯了。

我再次打开我的项目,看看那部分在哪里,它变成了Gtk的顶层容器。启示器驻留,必须有Gtk.Orientation.VERTICAL..如果你改变你的" topplevelbox "的方向,它会工作,但它不会是侧边栏。它会从上到下。如果您将GtkBox更改为GtkGrid,则情况相同。如果我猜它取决于子元素的默认方向。

的解决办法,是使用小部件隐藏/显示机制(相信我,我洗劫你的代码,它的工作)。

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()

闻起来像有bug。

作为一种变通方法,您可以使用
def sidebarShowHide(self, button):
    self.sidebarbox.set_visible(not self.sidebarbox.get_visible())

,但这不会产生任何过渡动画,但会删除其分配的空间,暂时不可见。


实际上,git仓库中提供的C演示会按预期调整大小,所以这实际上可能需要对子部件的首选方向做一些事情。

最新更新