我正在写一个应用程序。它有一个侧边栏,我想在启动时关闭它。我尝试过使用 self.sidebarbox.hide(). 。这似乎只有当我将其放入链接到按钮的函数中时才起作用。当我按下按钮时,它会显示/隐藏。我怎样才能解决这个问题?

这是我的代码(它是用 python3 编写的,但将在 python2 中运行。):

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"
        hb.props.subtitle = "Digital Maths Revision Guide"
        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)  

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

        self.add(toplevelbox)

        toplevelbox.pack_start(self.sidebarbox, False, False, 0)

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

        label = Gtk.Label("Contents Selector")
        self.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")

        #This should make the sidebar hide.        
        self.sidebarbox.hide()

    #This works. The sidebar does show/hide.
    def sidebarShowHide(self, button):
        if self.sidebarbox.get_visible():
            self.sidebarbox.hide()
        else:
            self.sidebarbox.show()

    def search_changed(self, searchentry):
        pass




win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
有帮助吗?

解决方案

你正在呼唤 show_all() 它将所有包含的小部件的状态更改为可见,包括侧边栏。

如果您仍然喜欢使用它(毕竟很方便),一种方法是您自己的方法,它将在显示所有内容后隐藏侧边栏,例如:

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"
        hb.props.subtitle = "Digital Maths Revision Guide"
        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)

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

        self.add(toplevelbox)

        toplevelbox.pack_start(self.sidebarbox, False, False, 0)

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

        label = Gtk.Label("Contents Selector")
        self.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 inital_show(self):
        win.show_all()
        self.sidebarbox.hide();

    #This works. The sidebar does show/hide.
    def sidebarShowHide(self, button):
        if self.sidebarbox.get_visible():
            self.sidebarbox.hide()
        else:
            self.sidebarbox.show()

    def search_changed(self, searchentry):
        pass

if __name__ == '__main__':
    win = MainWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.inital_show()
    Gtk.main()

请注意 initial_show() 方法,并从主部分调用它。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top