PythonGTK3盒不';隐藏

PythonGTK3盒不';隐藏,python,gtk3,Python,Gtk3,我正在写申请书。它有一个侧边栏,我想在启动时关闭它。我尝试过使用self.sidebarbox.hide()。这似乎只有当我把它放在一个链接到按钮的函数中时才起作用。当我按下按钮时,它显示/隐藏。我怎样才能解决这个问题 这是我的代码(它是用python3编写的,但将在python2中运行): 您正在调用show_all(),它将所有包含的小部件的状态更改为可见,包括侧边栏 如果您仍然喜欢使用它(毕竟这很方便),一种方法是您自己的方法,它会在显示所有内容后隐藏侧边栏,例如: 从gi.reposit

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

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


您正在调用
show_all()
,它将所有包含的小部件的状态更改为可见,包括侧边栏

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

从gi.repository导入Gtk,Gio
从gi.repository导入WebKit
高度=500
宽度=800
类主窗口(Gtk.Window):
定义初始化(自):
Gtk.Window.\uuuu init\uuuuu(self,title=“解析”)
self.set_边框_宽度(0)
设置默认大小(宽度、高度)
hb=Gtk.HeaderBar()
hb.props.show\u close\u按钮=真
hb.props.title=“决议”
hb.props.subtitle=“数字数学修订指南”
自置标题栏(hb)
button=Gtk.button()
图标=Gio.MEDICON(名称=“标志系统符号”)
image=Gtk.image.new_from_gicon(图标,1)
按钮。添加(图像)
按钮连接(“单击”,self.sidebarShowHide)
按钮。在单击时设置焦点(错误)
hb.pack_启动(按钮)
self.sidebarbox=Gtk.Box(方向=Gtk.orientation.VERTICAL,间距=0)
toplevelbox=Gtk.Box(方向=Gtk.orientation.HORIZONTAL,间距=0)
自我添加(toplevelbox)
toplevelbox.pack_start(self.sidebarbox,False,False,0)
self.searchentry=Gtk.searchentry()
self.searchentry.connect(“搜索已更改”,self.search\u已更改)
self.sidebarbox.pack_start(self.searchentry,False,False,0)
标签=Gtk.label(“内容选择器”)
self.sidebarbox.pack_start(标签,True,True,0)
scroller=Gtk.ScrolledWindow()
content=WebKit.WebView()
滚动条添加(内容)
toplevelbox.pack_start(滚动条,真,真,0)
content.open(“/home/oliver/resolution/placeholder.html”)
定义初始显示(自):
赢,全力以赴
self.sidebarbox.hide();
#这很有效。侧边栏不显示/隐藏。
def侧边栏显示隐藏(自身,按钮):
如果self.sidebarbox.get_可见():
self.sidebarbox.hide()
其他:
self.sidebarbox.show()文件
def search_已更改(self,searchentry):
通过
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
win=主窗口()
win.connect(“删除事件”,Gtk.main_退出)
win.inital_show()
Gtk.main()

请注意
initial\u show()
方法,并从主要部分调用它。

谢谢,这让我非常恼火:)
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()