tobias47n9e tobias47n9e - 2 months ago 32
Python Question

Create a radio action in a Gtk.PopoverMenu

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.

Answer

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