質問

申請書を書いています。サイドバーがあり、起動時に閉じたいです。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()を呼び出して、サイドバーを含めて表示されているすべてのウィジェットの状態を表示するように変更します。

あなたがまだそれを使うのが好きな場合(まったく便利です)1つの方法はあなた自身の方法です。

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