Skip to main content
 首页 » 编程设计

python之Gtk3 和 Python之框不会调整大小以填充窗口

2025年05月04日11zhwl

我正在使用 Gtk3 和 Python 编写一个应用程序。我有一个 reveerer 作为侧边栏来选择内容,还有一个 webkit webview 来显示主要内容。当 reveerer 被隐藏时,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.Revealer 所在的顶层容器必须具有 Gtk.Orientation.VERTICAL.. 如果您将“toplevelbox”方向更改为,它会工作,但它不会是侧边栏。它将来自顶部或底部。如果您将 GtkBox 更改为 GtkGrid,情况也一样。如果我猜的话,这取决于 children 的默认方向。

解决方法是使用小部件隐藏/显示机制(相信我,我检查了你的代码并且它有效)。

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