Re: Using Gtk.Builder to create a menubar.



Dear Ifrit,

thank you for your answer. I am getting closer but there are still
problems.

I updated my StackOverflow question with the current code (see at the
end of this mail): 
https://stackoverflow.com/q/49964906/4865723

What you want is create your Gio.Action's and add them to anything
that implements Gio.ActionMap. Gtk.Application and
Gtk.ApplicationWindow both implement it so you can add the action to
them with the add_action method.

You can see in my new code that the main problem now is how I create a
menubar widget based on the XML-string and the Actions.

I know that Gtk.Application has a add_menubar() method. But I don't
want to have a Gtk.Application instance. Do I really need it?
And what is about Toolbars? Gtk.Application doesn't have a
add_toolbar().

I would prefer to handle the widgets for Menubar and Toolbar by myself.

The gtk3 python tutorial deals with Gtk.Application and Gio.Action
but if you want more in depth docs see
https://wiki.gnome.org/HowDoI/GAction

This is a very interesting page. It started with a lot of C-code. I
overread in the past that there is Python code also. See section
"Adding actions to your GtkApplicationWindow" on that page.
From there I understand some better how to create actions and connect
them to signals. But there is no menubar or a toolbar in that example.
There is only a button.

I need a menubar and a toolbar.

Isn't there a way to create a menubar and toolbar without using XML and
actions? I still see no advantage in that way (part of this question
https://stackoverflow.com/q/49948612/4865723).

And this is much better asked on the general Gtk and Gtk app dev
mailing list as what you are confused about is the Gtk and Gio api
and how to use them, not so much with the python bindings.

Please see
https://pygobject.readthedocs.io/en/latest/contact.html

I used the mailinglist mentioned there.

My current 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]