Using Gtk.Builder to create a menubar.



X-Post: I was redirected from
https://mail.gnome.org/archives/python-hackers-list/2018-April/msg00004.html
to here.

I try to use Gtk.Builder to create a menubar. It is not working
because the documentation is not clear for me. There is no exmple for
a IMO usual thing like a menubar.

To improve documentation and the help other new users I created a
question with example code on StackOverflow.
https://stackoverflow.com/q/49964906/4865723

Currently I can put the XML-string and the Gio.Action together. Not
sure if this is the correct way!? But the next step is the main
problem. I don't know how to create a menubar widget out of it.

btw: I don't want to use Gtk.Application.set_menubar(). Because there
is no Gtk.Application.set_toolbar() and currently I see no advantage on
having a Gtk-based application object.

This is the code

#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio

class Window(Gtk.ApplicationWindow):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(200, 100)

        #
        self.interface_info = """
        <interface>
          <menu id='TheMenu'>
            <section>
              <attribute name='foo'>Foo</attribute>
              <item>
                <attribute name='bar'>Bar</attribute>
              </item>
            </section>
          </menu>
        </interface>
        """

        builder = Gtk.Builder.new_from_string(self.interface_info, -1)

        action_bar = Gio.SimpleAction.new('bar', None)
        action_bar.connect('activate', self.on_menu)
        self.add_action(action_bar)

        menubar = builder.get_object('TheMenu')

        # layout
        self.layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.layout.pack_start(menubar, True, True, 0)
        self.add(self.layout)

        self.connect('destroy', Gtk.main_quit)
        self.show_all()

    def on_menu(self, widget):
        print(widget)

if __name__ == '__main__':
    win = Window()
    Gtk.main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]