Re: Using Gtk.Builder to create a menubar.



Traveling so excuse my brevity and typos below.

Op zo 22 apr. 2018 21:46 schreef 
<c buhtz posteo jp>:
> You may want to work through [2] as it deals with the basic questions
> about Gtk+3 and Python including UIManager and Actions.

My question is about Gtk.Builder. Gtk.UIManager and Gtk.Action are
deprecated as you can see in [1]. I know both links you gave.

Then you missed the part talking about Gtk.Application and how to add a Gio.Action to it. 

You are trying to add Gtk.ActionGroup to a Gtk.Builder which is incorrect. Gtk.ActionGroup only contains Gtk.Action and it is deprecated.

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.

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

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. This list is very quite and there are a lot more people on the those that can help, https://www.gtk.org/mailing-lists.php.

~infirit

The code

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

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_group = Gtk.ActionGroup('my_actions')
        action_bar = Gtk.Action(name='bar')
        action_bar.connect('activate', self.on_menu)
        action_group.add_action(action_bar)
        builder.insert_action_group(action_group)

        menubar = builder.get_widget('TheMenu')
        self.set_menubar(menubar)

        # layout
        self.layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        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]