1

enter image description here

In my previous question (actually the answer) I could find a way to create a Gtk.PopoverMenu with a few ModelButtons (normal, and toggle). But I am really struggling with how to create a radiobutton in this way.

Older examples use Gtk.ActionGroups, but they have been deprecated since GTK 3.10. I have no idea how to construct a Gio.ActionGroup (https://lazka.github.io/pgi-docs/#Gio-2.0/interfaces/ActionGroup.html#Gio.ActionGroup):

action_group = Gio.ActionGroup()
NotImplementedError: ActionGroup can not be constructed

and

action_group = Gio.ActionGroup.new()
AttributeError: type object 'ActionGroup' has no attribute 'new'

The same is true for the Gio.ActionMap (https://lazka.github.io/pgi-docs/#Gio-2.0/interfaces/ActionMap.html#Gio.ActionMap):

actionmap = Gio.ActionMap()
NotImplementedError: ActionMap can not be constructed

and:

actionmap = Gio.ActionMap.new()
AttributeError: type object 'ActionMap' has no attribute 'new'

I am not sure why this works:

    actionmap = Gio.ActionMap.add_action(self, action_yes)
    actionmap = Gio.ActionMap.add_action(self, action_no)

Hopefully someone can help me out, to get this sorted in my head:

from gi.repository import Gio, Gtk, GLib
import sys


class MainApplication(Gtk.Application):

    def __init__(self):
        Gtk.Application.__init__(self,
                                 application_id="needs.dot",
                                 flags=Gio.ApplicationFlags.FLAGS_NONE)
        self.connect("activate", self.activate_window)

    def activate_window(self, app):
        """
        The activate signal of Gtk.Application passes the MainApplication class
        to the window. The window is then set as a window of that class.
        """
        self.window = Gtk.ApplicationWindow()
        self.window.set_default_size(500, 400)

        self.hb = Gtk.HeaderBar()
        self.hb.set_show_close_button(True)
        self.hb.props.title = "HeaderBar & PopOverMenu"
        self.window.set_titlebar(self.hb)

        button_settings = Gtk.MenuButton()
        icon = Gio.ThemedIcon(name="format-justify-fill-symbolic")
        image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON)
        button_settings.add(image)
        self.hb.pack_end(button_settings)

        self.builder = Gtk.Builder()
        self.builder.add_from_file("actionmap_layout.xml")
        pom_options = self.builder.get_object("pom_options")
        button_settings.set_popover(pom_options)
        #self.builder.connect_signals(self) #Not needed because of using actions?

        app.add_window(self.window)

        action_print = Gio.SimpleAction.new("print", None)
        action_print.connect("activate", self.print_something)
        app.add_action(action_print)

        action_toggle = Gio.SimpleAction.new_stateful("toggle", None, GLib.Variant.new_boolean(False))
        action_toggle.connect("change-state", self.toggle_toggled)
        app.add_action(action_toggle)

        action_yes = Gio.SimpleAction.new("yes", None)
        action_yes.connect("activate", self.print_something)
        app.add_action(action_yes)

        action_no = Gio.SimpleAction.new("no", None)
        action_no.connect("activate", self.print_something)
        app.add_action(action_no)
        actionmap = Gio.ActionMap.add_action(self, action_yes)
        actionmap = Gio.ActionMap.add_action(self, action_no)
        #action_group = Gio.ActionGroup.new()

        btn = Gtk.Button("Button")
        self.window.add(btn)
        self.window.show_all()

    def print_something(self, action, variable):
        print("something")

    def toggle_toggled(self, action, state):
        action.set_state(state)
        Gtk.Settings.get_default().set_property("gtk-application-prefer-dark-theme", state)

    def on_action_quit_activated(self, action):
        self.app.quit()


if __name__ == "__main__":
    """
    Creates an instance of the MainApplication class that inherits from
    Gtk.Application.
    """
    app = MainApplication()
    app.run(sys.argv)

XML-Interface:

<interface>
    <object class="GtkPopoverMenu" id ="pom_options">
      <child>
        <object class="GtkBox">
          <property name="visible">True</property>
          <property name="margin">10</property>
          <property name="orientation">vertical</property>
          <child>
            <object class="GtkModelButton" id="mb_print">
              <property name="visible">True</property>
              <property name="action-name">app.print</property>
              <property name="can_focus">True</property>
              <property name="receives_default">True</property>
              <property name="label" translatable="yes">Print</property>
            </object>
          </child>
          <child>
            <object class="GtkModelButton" id="mp_toggle">
              <property name="visible">True</property>
              <property name="action-name">app.toggle</property>
              <property name="can_focus">True</property>
              <property name="receives_default">True</property>
              <property name="label" translatable="yes">Night Mode</property>
            </object>
          </child>
          <child>
            <object class="GtkModelButton" id="mp_yes">
              <property name="visible">True</property>
              <property name="action-name">app.yes</property>
              <property name="can_focus">True</property>
              <property name="receives_default">True</property>
              <property name="label" translatable="yes">Yes</property>
            </object>
          </child>
          <child>
            <object class="GtkModelButton" id="mp_no">
              <property name="visible">True</property>
              <property name="action-name">app.no</property>
              <property name="can_focus">True</property>
              <property name="receives_default">True</property>
              <property name="label" translatable="yes">No</property>
            </object>
          </child>
        </object>
      </child>
    </object>
</interface>

There is another example here. But it also doesn't show how to properly do radios. Toggles seem to be straightforward as seen my example above.

Community
  • 1
  • 1
tobias47n9e
  • 2,233
  • 3
  • 28
  • 54
  • 1
    Why don't you use GtkRadioButtons? – elya5 Jul 01 '15 at 17:50
  • GtkRadioButtons are not highlighted properly in the PopoverMenu. And the docs say "GtkModelButton will adapt its appearance [...] and appear either as a plain, check or radio button. So it shouldn't be too difficult to get it working. (https://developer.gnome.org/gtk3/stable/GtkModelButton.html) – tobias47n9e Jul 01 '15 at 18:44
  • I wonder what kind of dark magic Nautilus uses to make their radiobuttons link to the same "view.sort" action: https://github.com/GNOME/nautilus/blob/master/src/nautilus-toolbar-view-menu.xml#L103 – tobias47n9e Jul 01 '15 at 20:34
  • 1
    For future reference, you can't instantiate `Gio.ActionGroup` and `Gio.ActionMap` because they are interfaces, not classes. (This concept doesn't map particularly well to Python, but think of them as classes without constructors.) See this documentation, for example: http://lazka.github.io/pgi-docs/Gio-2.0/interfaces/ActionGroup.html — it shows that both interfaces are implemented by `Gio.SimpleActionGroup` and `Gio.Application`, which you _can_ construct. – ptomato Jul 04 '15 at 23:44
  • @ptomato: Thanks. I had trouble understanding that. The Gio.Actions stuff is slowly making sense to me. – tobias47n9e Jul 05 '15 at 10:06

2 Answers2

3

I recently talked to mgedmin, who has a working example of a radioaction-group (https://github.com/gtimelog/gtimelog/blob/da38b1ac9fc8b81a2b92884a755f311a99e22d0e/mockup.py). I adapted my example from the question accordingly. Please note the different structure of the XML-interface.

Python:

import gi
gi.require_version("Gtk", "3.0")

from gi.repository import Gio, Gtk, GLib
import sys


class MainApplication(Gtk.Application):

    def __init__(self):
        Gtk.Application.__init__(self,
                                 application_id="needs.dots",
                                 flags=Gio.ApplicationFlags.FLAGS_NONE)
        # https://developer.gnome.org/gio/unstable/GApplication.html#g-application-id-is-valid
        self.connect("activate", self.activate_window)

    def activate_window(self, app):
        """
        The activate signal of Gtk.Application passes the
        MainApplication class
        to the window. The window is then set as a window
        of that class.
        """
        self.window = Gtk.ApplicationWindow()
        self.window.set_default_size(500, 400)
        app.add_window(self.window)

        self.hb = Gtk.HeaderBar()
        self.hb.set_show_close_button(True)
        self.hb.props.title = "HeaderBar & PopOverMenu"
        self.window.set_titlebar(self.hb)

        button_settings = Gtk.MenuButton()
        icon = Gio.ThemedIcon(name="format-justify-fill-symbolic")
        image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON)
        button_settings.add(image)
        self.hb.pack_end(button_settings)

        self.builder = Gtk.Builder()
        self.builder.add_from_file("popovermenu_layout.xml")
        button_settings.set_menu_model(\
                        self.builder.get_object('options-menu'))

        # Add radiobutton group that includes all actions that
        # are named ***.radiogroup, 'radio-two' is the default
        # choice.
        detail_level = Gio.SimpleAction.new_stateful("radiogroup", \
                           GLib.VariantType.new("s"), \
                           GLib.Variant("s", "radio-two"))
        detail_level.connect("activate", self.radio_response)
        self.window.add_action(detail_level)

        # Connects to the action. The first part of the XML name
        # is left away:
        # <property name="action-name">app.print</property>
        # becomes simply "print".
        action_print = Gio.SimpleAction.new("print", None)
        action_print.connect("activate", self.print_something)
        app.add_action(action_print)

        # app.toggle becomes -> toggle
        action_toggle = Gio.SimpleAction.new_stateful("toggle", \
                            None, GLib.Variant.new_boolean(False))
        action_toggle.connect("change-state", self.toggle_toggled)
        app.add_action(action_toggle)

        btn = Gtk.Button("Button")
        self.window.add(btn)
        self.window.show_all()

    def print_something(self, action, variable):
        print("something")

    def toggle_toggled(self, action, state):
        action.set_state(state)
        Gtk.Settings.get_default().set_property( \
                "gtk-application-prefer-dark-theme", state)

    def radio_response(self, act_obj, act_lbl):
        # Not sure if this is the correct way of doing this.
        # but it seems to work.
        act_obj.set_state(act_lbl)

    def on_action_quit_activated(self, action):
        self.app.quit()

if __name__ == "__main__":
    """
    Creates an instance of the MainApplication class
    that inherits from Gtk.Application.
    """
    app = MainApplication()
    app.run(sys.argv)

UI-file:

<interface>
  <menu id="options-menu">
    <section>
      <item>
    <attribute name="label">Print Message</attribute>
    <attribute name="action">app.print</attribute>
      </item>
    </section>
    <section>
      <item>
    <attribute name="label">Night Mode</attribute>
    <attribute name="action">app.toggle</attribute>
      </item>
    </section>
    <section>
      <item>
    <attribute name="label">Choice 1</attribute>
    <attribute name="action">win.radiogroup</attribute>
    <attribute name="target">radio-one</attribute>
      </item>
      <item>
    <attribute name="label">Choice 2</attribute>
    <attribute name="action">win.radiogroup</attribute>
    <attribute name="target">radio-two</attribute>
      </item>
      <item>
    <attribute name="label">Choice 3</attribute>
    <attribute name="action">win.radiogroup</attribute>
    <attribute name="target">radio-three</attribute>
      </item>
    </section>
  </menu>
</interface>

The resulting interface:

enter image description here

Community
  • 1
  • 1
tobias47n9e
  • 2,233
  • 3
  • 28
  • 54
1

I modified some code I did a couple of weeks ago. This might be of help. It's a little simplistic compared with what you're trying to do, but, on the other hand, it works.

There's a lot of extra code. The RadioMenuButtons are created in the MyPopup function. It's called with the btndef argument, which contains a list of tuples. If the tuple has more than 1 element, then it generates a radiobutton group.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  test_gtk3_popover.py
#  
#  Copyright 2015 John Coppens <john@jcoppens.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#

from gi.repository import Gtk

class MyPopup(Gtk.MenuButton):
    def __init__(self, btndefs):
        super(MyPopup, self).__init__()

        self.menu = Gtk.Menu()
        self.set_popup(self.menu)
        self.set_label(">")
        self.set_direction(Gtk.ArrowType.RIGHT)

        for btndef in btndefs:
            if len(btndef) == 1:                # This is a normal button
                item = Gtk.MenuItem()
                item.set_label(btndef[0])
                item.show()
                self.menu.append(item)
            else:
                group = None
                for radiodef in btndef:         # It's a selection of radiobuttons
                    item = Gtk.RadioMenuItem(label = radiodef)
                    if group == None:
                        group = item
                    else:
                        item.set_property("group", group)
                    item.show()
                    self.menu.append(item)


class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.set_size_request(200, -1)
        self.connect("destroy", lambda x: Gtk.main_quit())

        self.hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL)
        self.entry = Gtk.Entry()

        self.popup = MyPopup( (("String", "String no case", "Hexadecimal"),
                               ("Regexp",)) )

        self.hbox.pack_start(self.entry, True, True, 0)
        self.hbox.pack_start(self.popup, False, True, 0)

        self.add(self.hbox)

        self.show_all()

    def run(self):
        Gtk.main()


def main():
    mw = MainWindow()
    mw.run()
    return 0

if __name__ == '__main__':
    main()
jcoppens
  • 5,306
  • 6
  • 27
  • 47
  • Thank you for posting this. I am still digesting the code and trying to convert it to a PopoverMenu. I think the big advantage of the different `MenuItems` in `Gtk.Menu` is that their widget behavior is automatically set correctly. The `ModelButtons` are constructed empty, and I think your approach of adding them to a group, is probably a good way of triggering their radiobutton behaviour. – tobias47n9e Jul 05 '15 at 10:11