Re: Using Gtk.Builder to create a menubar.



Hi Infirit,

thanks for your reply.

On 2018-04-22 15:03 infirit <infirit gmail com> wrote:
Better to include the actual code as text (no attachment)

See at the end of the mail.

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.

[1] https://lazka.github.io/pgi-docs/#Gtk-3.0/classes/Action.html#Gtk.Action

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]