Connect signals from .ui file

This is my .ui code:

<?xml version='1.0' encoding='UTF-8'?>
<!-- Created with Cambalache 0.16.0 -->
<interface>
  <!-- interface-name window.ui -->
  <requires lib="Adw" version="1.0"/>
  <requires lib="gtk" version="4.0"/>
  <requires lib="gio" version="2.0"/>
  <requires lib="libadwaita" version="1.4"/>
  <template class="LilbroWindow" parent="AdwApplicationWindow">
    <property name="content">
      <object class="AdwToolbarView">
        <child>
          <object class="GtkGrid" id="my_grid_1">
            <child>
              <object class="GtkLabel" id="my_label_1">
                <property name="margin-bottom">12</property>
                <property name="margin-end">12</property>
                <property name="margin-start">12</property>
                <property name="margin-top">12</property>
                <layout>
                  <property name="column">0</property>
                  <property name="column-span">1</property>
                  <property name="row">0</property>
                  <property name="row-span">1</property>
                </layout>
              </object>
            </child>
            <child>
              <object class="GtkGrid" id="my_grid_2">
                <child>
                  <object class="GtkEntry" id="my_entry_1">
                    <property name="margin-bottom">12</property>
                    <property name="margin-end">12</property>
                    <property name="margin-start">12</property>
                    <property name="margin-top">12</property>
                    <property name="placeholder-text">Ask LilBro</property>
                    <layout>
                      <property name="column">0</property>
                      <property name="column-span">1</property>
                      <property name="row">0</property>
                      <property name="row-span">1</property>
                    </layout>
                  </object>
                </child>
                <child>
                  <object class="GtkButton" id="my_button_1">
                    <property name="icon-name">go-next-symbolic</property>
                    <property name="margin-bottom">12</property>
                    <property name="margin-end">12</property>
                    <property name="margin-start">12</property>
                    <property name="margin-top">12</property>
                    <signal name="clicked" handler="send_message"/>
                    <layout>
                      <property name="column">1</property>
                      <property name="column-span">1</property>
                      <property name="row">0</property>
                      <property name="row-span">1</property>
                    </layout>
                  </object>
                </child>
                <layout>
                  <property name="column">0</property>
                  <property name="column-span">1</property>
                  <property name="row">1</property>
                  <property name="row-span">1</property>
                </layout>
              </object>
            </child>
          </object>
        </child>
        <child type="top">
          <object class="AdwHeaderBar" id="header_bar">
            <child type="end">
              <object class="GtkMenuButton">
                <property name="icon-name">open-menu-symbolic</property>
                <property name="menu-model">primary_menu</property>
                <property name="primary">True</property>
                <property name="tooltip-text" translatable="yes">Menu</property>
              </object>
            </child>
          </object>
        </child>
      </object>
    </property>
    <property name="default-height">300</property>
    <property name="default-width">300</property>
  </template>
  <menu id="primary_menu">
    <section>
      <item>
        <attribute name="action">app.preferences</attribute>
        <attribute name="label" translatable="yes">_Preferences</attribute>
      </item>
      <item>
        <attribute name="action">win.show-help-overlay</attribute>
        <attribute name="label" translatable="yes">_Keyboard Shortcuts</attribute>
      </item>
      <item>
        <attribute name="action">app.about</attribute>
        <attribute name="label" translatable="yes">_About Lilbro</attribute>
      </item>
    </section>
  </menu>
</interface>

As you can see, the button emits a send_message signal.
But how do i connect and use it with this main code (pretty default):

from gi.repository import Adw
from gi.repository import Gtk

@Gtk.Template(resource_path='/org/gnome/lilbro/window.ui')
class LilbroWindow(Adw.ApplicationWindow):
    __gtype_name__ = 'LilbroWindow'

    label = Gtk.Template.Child()

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

Im a newbie with gtk and python so please answer in a very understandable way :slight_smile:

Here is the documentation for GtK.Template in python: Gtk.Template - PyGObject.

You just need to decorate your method with Gtk.Template.Callback():

from gi.repository import Adw
from gi.repository import Gtk

@Gtk.Template(resource_path='/org/gnome/lilbro/window.ui')
class LilbroWindow(Adw.ApplicationWindow):
    __gtype_name__ = 'LilbroWindow'

    label = Gtk.Template.Child()

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @Gtk.Template.Callback()
    def send_message(self, button):
        pass
1 Like

It works!!! Here is my digital kiss: :kissing_heart:

One more question tho: How can i change a label text and how to access what the user types into a GtkEntry?

Oh found it out by myself

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.