Skip to content

Commit 88ce2af

Browse files
committed
Merge branch 'cjmayo-template'
2 parents a561932 + b2fc789 commit 88ce2af

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

examples/builder_example.glade

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
<interface>
33
<!-- interface-requires gtk+ 3.0 -->
44
<object class="GtkWindow" id="window1">
5-
<property name="can_focus">False</property>
5+
<property name="can-focus">False</property>
66
<signal name="destroy" handler="onDestroy" swapped="no"/>
77
<child>
88
<object class="GtkButton" id="button1">
99
<property name="label" translatable="yes">button</property>
10-
<property name="use_action_appearance">False</property>
10+
<property name="use-action-appearance">False</property>
1111
<property name="visible">True</property>
12-
<property name="can_focus">True</property>
13-
<property name="receives_default">True</property>
14-
<property name="use_action_appearance">False</property>
12+
<property name="can-focus">True</property>
13+
<property name="receives-default">True</property>
14+
<property name="use-action-appearance">False</property>
1515
<signal name="pressed" handler="onButtonPressed" swapped="no"/>
1616
</object>
1717
</child>

examples/template_example.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import gi
2+
3+
gi.require_version("Gtk", "3.0")
4+
from gi.repository import Gtk
5+
6+
7+
@Gtk.Template(filename="template_example.ui")
8+
class Window1(Gtk.Window):
9+
__gtype_name__ = "window1"
10+
11+
@Gtk.Template.Callback()
12+
def onDestroy(self, *args):
13+
Gtk.main_quit()
14+
15+
@Gtk.Template.Callback()
16+
def onButtonPressed(self, button):
17+
print("Hello World!")
18+
19+
20+
window = Window1()
21+
window.show()
22+
23+
Gtk.main()

examples/template_example.ui

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<interface>
3+
<!-- interface-requires gtk+ 3.0 -->
4+
<template class="window1" parent="GtkWindow">
5+
<signal name="destroy" handler="onDestroy" swapped="no"/>
6+
<child>
7+
<object class="GtkButton" id="button1">
8+
<property name="label" translatable="yes">button</property>
9+
<property name="use-action-appearance">False</property>
10+
<property name="visible">True</property>
11+
<property name="can-focus">True</property>
12+
<property name="receives-default">True</property>
13+
<property name="use-action-appearance">False</property>
14+
<signal name="pressed" handler="onButtonPressed" swapped="no"/>
15+
</object>
16+
</child>
17+
</template>
18+
</interface>

source/builder.txt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Glade and Gtk.Builder
22
=====================
33
The :class:`Gtk.Builder` class offers you the opportunity to design user interfaces without writing a single line of code.
4-
This is possible through describing the interface by an XML file and then loading the XML description at runtime and create the objects automatically, which the Builder class does for you.
5-
For the purpose of not needing to write the XML manually the `Glade <https://glade.gnome.org/>`_ application lets you create the user interface in a WYSIWYG (what you see is what you get) manner
4+
This is achieved by defining the interface in an XML file and then loading that XML UI definition at runtime using the Builder class which creates the objects automatically.
5+
To avoid writing the XML manually use the `Glade <https://glade.gnome.org/>`_ application which lets you create the user interface in a WYSIWYG (what you see is what you get) manner
66

77
This method has several advantages:
88

@@ -29,10 +29,10 @@ The resulting XML file should look like this.
2929
<child>
3030
<object class="GtkButton" id="button1">
3131
<property name="label" translatable="yes">button</property>
32-
<property name="use_action_appearance">False</property>
32+
<property name="use-action-appearance">False</property>
3333
<property name="visible">True</property>
34-
<property name="can_focus">True</property>
35-
<property name="receives_default">True</property>
34+
<property name="can-focus">True</property>
35+
<property name="receives-default">True</property>
3636
</object>
3737
</child>
3838
</object>
@@ -120,3 +120,20 @@ The final code of the example
120120

121121
.. literalinclude:: ../examples/builder_example.py
122122
:linenos:
123+
124+
Gtk.Template
125+
------------
126+
:class:`Gtk.WidgetClass` allows UI definition files to be used to extend a widget,
127+
PyGObject provides :doc:`guide/gtk_template` as a way of accessing this from Python.
128+
129+
The UI definition file used in the example needs a small change to include a *<template>* element:
130+
131+
.. literalinclude:: ../examples/template_example.ui
132+
:language: xml
133+
134+
Then it can be used to implement the example with a :class:`Gtk.Window` subclass:
135+
136+
.. literalinclude:: ../examples/template_example.py
137+
:linenos:
138+
139+
More information can be found at the `PyGObject <https://pygobject.readthedocs.io/en/latest/guide/gtk_template.html>`_ website.

0 commit comments

Comments
 (0)