1
votes

Below are 4 files:

Case #1: example1.py using example1.glade.

Case #2: example2.py using example2.glade.

To see the output in both cases, the steps are the same for each case. Just click on the multiline label or on the link button after opening one of these programs, and the output will be shown in the terminal.

Screenshot, the same for both cases

Screenshot for both cases

1st case

example1.py

# coding=utf-8

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


def box_clicked(*args):
    print(args)
    return True


b = Gtk.Builder()
b.add_from_file("example1.glade")
w = b.get_object("window1")
lb = b.get_object("linkButton")
eb = b.get_object("eventbox1")
eb.connect("button-release-event", box_clicked, lb)

w.connect("delete-event", Gtk.main_quit)
w.show_all()
Gtk.main()

example1.glade

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkAspectFrame">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label_xalign">0</property>
        <child>
          <object class="GtkEventBox" id="eventbox1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="above_child">True</property>
            <child>
              <object class="GtkBox" id="box1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="orientation">vertical</property>
                <child>
                  <object class="GtkLinkButton" id="linkButton">
                    <property name="label" translatable="yes">button</property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="receives_default">True</property>
                    <property name="relief">none</property>
                    <property name="uri">http://www.google.com</property>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <object class="GtkLabel" id="multilineLabel">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="label" translatable="yes">this
is
a
multiline
label</property>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">1</property>
                  </packing>
                </child>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

The signal and the handler were connected manually from code.

Example of arguments passed to the signal handler, printed with print(args):

(<Gtk.EventBox object at 0x02bc4b6c (GtkEventBox at 0x02b9e290)>, <Gdk.EventButt
on object at 0x02bc4c34 (void at 0x02e691c8)>, <Gtk.LinkButton object at 0x02668
914 (GtkLinkButton at 0x02e37120)>)

Pretty print:

  1. Gtk.EventBox
  2. Gtk.EventButton
  3. Gtk.LinkButton

2nd case

example2.py

# coding=utf-8

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


class Builder(Gtk.Builder):
    def __init__(self):
        Gtk.Builder.__init__(self)
        self.add_from_file("example2.glade")
        self.connect_signals(self)
        self.win = self.get_object("window1")

    def box_clicked(self, *args):
        print(args)
        return True


win = Builder().win
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

example2.glade

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkAspectFrame">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label_xalign">0</property>
        <child>
          <object class="GtkEventBox" id="eventbox1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="above_child">True</property>
            <signal name="button-release-event" handler="box_clicked" object="linkButton" swapped="no"/>
            <child>
              <object class="GtkBox" id="box1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="orientation">vertical</property>
                <child>
                  <object class="GtkLinkButton" id="linkButton">
                    <property name="label" translatable="yes">button</property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="receives_default">True</property>
                    <property name="relief">none</property>
                    <property name="uri">http://www.google.com</property>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <object class="GtkLabel" id="multilineLabel">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="label" translatable="yes">this
is
a
multiline
label</property>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">1</property>
                  </packing>
                </child>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

The event handler was connected to the signal in Glade.

Example of arguments passed to the signal handler, printed with print(args):

(<Gtk.LinkButton object at 0x02c68554 (GtkLinkButton at 0x02e96118)>, <Gdk.Event
Button object at 0x02c6b5f4 (void at 0x02d56a60)>)

Pretty print

  1. Gtk.LinkButton
  2. Gdk.EventButton

Pretty print comparison

Arguments for the 1st case:

  1. Gtk.EventBox
  2. Gtk.EventButton
  3. Gtk.LinkButton

Arguments for the 2nd case:

  1. Gtk.LinkButton
  2. Gdk.EventButton

I want to comprehend the reasons behind the differences between the output in these 2 cases.

I use Python 2.7.13 and GTK+ 3.22 on MSYS2 on Windows 10. Both MSYS2 and Windows have all the available updates installed.

1

1 Answers

2
votes

The second glade file has an object attribute to the signal element, which is the equivalent of using g_signal_connect_object() instead of g_signal_connect() in C. It passes the given object (the link button) as the first parameter to the signal handler.