Widgets#
Adw.AboutWindow#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.AboutWindow"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.AboutWindow',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button_model.append(
label='About',
detailed_action='app.about',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
label = Gtk.Label.new()
label.set_label(str='Access the menu and click on the about button')
label.set_vexpand(expand=True)
vbox.append(child=label)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
self.create_action('about', self.on_about_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def on_about_action(self, action, param):
dialog = Adw.AboutWindow.new()
dialog.set_transient_for(parent=self.get_active_window())
dialog.set_application_name('Python e GTK 4')
dialog.set_version('0.0.1')
dialog.set_developer_name('Renato Cruz (natorsc)')
dialog.set_license_type(Gtk.License(Gtk.License.MIT_X11))
dialog.set_comments(
'Creating graphical interfaces with the Python programming '
'language (PyGObject) and the GTK graphics toolkit'
)
dialog.set_website('https://gtk.justcode.com.br')
dialog.set_issue_url(
"https://github.com/natorsc/gui-python-pygobject-gtk4/issues")
dialog.add_credit_section('Contributors', ['Name-01', 'Name-02'])
dialog.set_translator_credits('Renato Cruz')
dialog.set_copyright('© 2022 Renato Cruz (natorsc)')
dialog.set_developers(['natorsc https://github.com/natorsc'])
dialog.set_application_icon('help-about-symbolic')
dialog.present()
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.AboutWindow."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
UI = BASE_DIR.joinpath('MainWindow.ui')
Adw.init()
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
self.create_action('about', self.on_about_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def on_about_action(self, action, param):
dialog = Adw.AboutWindow.new()
dialog.set_transient_for(parent=self.get_active_window())
dialog.set_application_name('Python e GTK')
dialog.set_version('0.0.1')
dialog.set_developer_name('Renato Cruz (natorsc)')
dialog.set_license_type(Gtk.License(Gtk.License.MIT_X11))
dialog.set_comments(
'Creating graphical interfaces with the Python programming '
'language (PyGObject) and the GTK graphics toolkit'
)
dialog.set_website('https://gtk.justcode.com.br')
dialog.set_issue_url(
"https://github.com/natorsc/gui-python-pygobject-gtk/issues")
dialog.add_credit_section('Contributors', ['Name-01', 'Name-02'])
dialog.set_translator_credits('Renato Cruz')
dialog.set_copyright('© 2022 Renato Cruz (natorsc)')
dialog.set_developers(['natorsc https://github.com/natorsc'])
dialog.set_application_icon('help-about-symbolic')
dialog.present()
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.AboutWindow</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="GtkLabel">
<property name="label">Access the menu and click on the about button</property>
<property name="vexpand">true</property>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
<item>
<attribute name="label" translatable="true">About</attribute>
<attribute name="action">app.about</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.AboutWindow';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
Gtk.Label {
label: 'Access the menu and click on the about button';
vexpand: true;
}
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
item {
label: _('About');
action: 'app.about';
}
}
}
Adw.ActionRow#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject Adw.ActionRow"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
items = ['Item 01', 'Item 02', 'Item 03', 'Item 04', 'Item 05']
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject Adw.ActionRow',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
self.listbox = Gtk.ListBox.new()
self.listbox.set_selection_mode(mode=Gtk.SelectionMode.NONE)
self.listbox.add_css_class(css_class='boxed-list')
self.listbox.set_margin_start(margin=12)
self.listbox.set_margin_end(margin=12)
vbox.append(child=self.listbox)
for item in self.items:
icon = Gtk.Image.new_from_icon_name(
icon_name='accessories-text-editor-symbolic',
)
switch = Gtk.Switch.new()
switch.set_valign(align=Gtk.Align.CENTER)
switch.connect('notify::active', self.on_switch_button_clicked)
adw_action_row = Adw.ActionRow.new()
adw_action_row.set_title(title=item)
adw_action_row.set_subtitle(subtitle='Adw.ActionRow')
adw_action_row.add_prefix(widget=icon)
adw_action_row.add_suffix(widget=switch)
self.listbox.append(child=adw_action_row)
def on_switch_button_clicked(self, switch, GParamBoolean):
if switch.get_active():
print('Button checked')
else:
print('Button unchecked')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject Gtk.ListBox() Adw.ActionRow."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
UI = BASE_DIR.joinpath('MainWindow.ui')
Adw.init()
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
@Gtk.Template.Callback()
def on_switch_button_clicked(self, switch, GParamBoolean):
if switch.get_active():
print('Button checked')
else:
print('Button unchecked')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject Adw.ActionRow</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="GtkListBox">
<property name="selection-mode">0</property>
<style>
<class name="boxed-list"/>
</style>
<child>
<object class="AdwActionRow">
<property name="title">Item 01</property>
<property name="subtitle">Adw.ActionRow()</property>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
<child type="suffix">
<object class="GtkSwitch">
<property name="valign">3</property>
<signal name="notify::active" handler="on_switch_button_clicked"/>
</object>
</child>
</object>
</child>
<child>
<object class="AdwActionRow">
<property name="title">Item 02</property>
<property name="subtitle">Adw.ActionRow()</property>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
<child type="suffix">
<object class="GtkSwitch">
<property name="valign">3</property>
<signal name="notify::active" handler="on_switch_button_clicked"/>
</object>
</child>
</object>
</child>
<child>
<object class="AdwActionRow">
<property name="title">Item 03</property>
<property name="subtitle">Adw.ActionRow()</property>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
<child type="suffix">
<object class="GtkSwitch">
<property name="valign">3</property>
<signal name="notify::active" handler="on_switch_button_clicked"/>
</object>
</child>
</object>
</child>
<child>
<object class="AdwActionRow">
<property name="title">Item 04</property>
<property name="subtitle">Adw.ActionRow()</property>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
<child type="suffix">
<object class="GtkSwitch">
<property name="valign">3</property>
<signal name="notify::active" handler="on_switch_button_clicked"/>
</object>
</child>
</object>
</child>
<child>
<object class="AdwActionRow">
<property name="title">Item 05</property>
<property name="subtitle">Adw.ActionRow()</property>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
<child type="suffix">
<object class="GtkSwitch">
<property name="valign">3</property>
<signal name="notify::active" handler="on_switch_button_clicked"/>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject Adw.ActionRow';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
Gtk.ListBox {
selection-mode: none;
styles [
'boxed-list',
]
Adw.ActionRow {
title: 'Item 01';
subtitle: 'Adw.ActionRow()';
[prefix]
Gtk.Image {
icon-name: 'accessories-text-editor-symbolic';
}
[suffix]
Gtk.Switch {
valign: center;
notify::active => $on_switch_button_clicked();
}
}
Adw.ActionRow {
title: 'Item 02';
subtitle: 'Adw.ActionRow()';
[prefix]
Gtk.Image {
icon-name: 'accessories-text-editor-symbolic';
}
[suffix]
Gtk.Switch {
valign: center;
notify::active => $on_switch_button_clicked();
}
}
Adw.ActionRow {
title: 'Item 03';
subtitle: 'Adw.ActionRow()';
[prefix]
Gtk.Image {
icon-name: 'accessories-text-editor-symbolic';
}
[suffix]
Gtk.Switch {
valign: center;
notify::active => $on_switch_button_clicked();
}
}
Adw.ActionRow {
title: 'Item 04';
subtitle: 'Adw.ActionRow()';
[prefix]
Gtk.Image {
icon-name: 'accessories-text-editor-symbolic';
}
[suffix]
Gtk.Switch {
valign: center;
notify::active => $on_switch_button_clicked();
}
}
Adw.ActionRow {
title: 'Item 05';
subtitle: 'Adw.ActionRow()';
[prefix]
Gtk.Image {
icon-name: 'accessories-text-editor-symbolic';
}
[suffix]
Gtk.Switch {
valign: center;
notify::active => $on_switch_button_clicked();
}
}
}
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.Application#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.Application"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.Application',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.Application."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
UI = BASE_DIR.joinpath('MainWindow.ui')
Adw.init()
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.Application</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.Application';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.Avatar#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.Avatar"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.Avatar',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_homogeneous(homogeneous=True)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
hbox = Gtk.Box.new(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
hbox.set_homogeneous(homogeneous=True)
vbox.append(child=hbox)
avatar_01 = Adw.Avatar.new(
size=100,
text='Renato Cruz',
show_initials=False,
)
hbox.append(child=avatar_01)
avatar_02 = Adw.Avatar.new(
size=100,
text='Renato Cruz',
show_initials=True,
)
hbox.append(child=avatar_02)
avatar_03 = Adw.Avatar.new(
size=100,
text='Renato Cruz',
show_initials=False,
)
avatar_03.set_icon_name('contact-new-symbolic')
hbox.append(child=avatar_03)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.Avatar."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
UI = BASE_DIR.joinpath('MainWindow.ui')
Adw.init()
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.Avatar</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">0</property>
<property name="homogeneous">true</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="AdwAvatar">
<property name="size">100</property>
<property name="text">Renato Cruz</property>
<property name="show-initials">false</property>
</object>
</child>
<child>
<object class="AdwAvatar">
<property name="size">100</property>
<property name="text">Renato Cruz</property>
<property name="show-initials">true</property>
</object>
</child>
<child>
<object class="AdwAvatar">
<property name="size">100</property>
<property name="text">Renato Cruz</property>
<property name="show-initials">false</property>
<property name="icon-name">contact-new-symbolic</property>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.Avatar';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: horizontal;
homogeneous: true;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
Adw.Avatar {
size: 100;
text: 'Renato Cruz';
show-initials: false;
}
Adw.Avatar {
size: 100;
text: 'Renato Cruz';
show-initials: true;
}
Adw.Avatar {
size: 100;
text: 'Renato Cruz';
show-initials: false;
icon-name: 'contact-new-symbolic';
}
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.Carousel#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.Carousel"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.Carousel()',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
label = Gtk.Label.new(
str='Drag or use the mouse scroller to change pages.'
)
vbox.append(child=label)
separator = Gtk.Separator.new(orientation=Gtk.Orientation.HORIZONTAL)
vbox.append(child=separator)
adw_carousel = Adw.Carousel.new()
adw_carousel.set_vexpand(True)
adw_carousel.set_hexpand(True)
adw_carousel.set_spacing(spacing=24)
adw_carousel.connect('page-changed', self.on_carousel_page_changed)
vbox.append(child=adw_carousel)
for n in range(1, 11):
page = Gtk.Box.new(
orientation=Gtk.Orientation.VERTICAL, spacing=12)
adw_carousel.insert(child=page, position=n)
label = Gtk.Label.new(str=f'Page {n}')
page.append(child=label)
def on_carousel_page_changed(self, carousel, index):
print(f'Position: {carousel.get_position()}')
print(f'índice: {index}')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.Carousel."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
UI = BASE_DIR.joinpath('MainWindow.ui')
Adw.init()
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
@Gtk.Template.Callback()
def on_carousel_page_changed(self, carousel, index):
print(f'Position: {carousel.get_position()}')
print(f'índice: {index}')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.Carousel</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="GtkLabel">
<property name="label">Drag or use the mouse scroller to change pages.</property>
</object>
</child>
<child>
<object class="GtkSeparator">
<property name="orientation">0</property>
</object>
</child>
<child>
<object class="AdwCarousel">
<property name="vexpand">true</property>
<property name="hexpand">true</property>
<property name="spacing">24</property>
<signal name="page-changed" handler="on_carousel_page_changed"/>
<child>
<object class="GtkBox">
<property name="orientation">1</property>
<child>
<object class="GtkLabel">
<property name="label">Page 01.</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="orientation">1</property>
<child>
<object class="GtkLabel">
<property name="label">Page 02.</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="orientation">1</property>
<child>
<object class="GtkLabel">
<property name="label">Page 03.</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.Carousel';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
Gtk.Label {
label: 'Drag or use the mouse scroller to change pages.';
}
Separator {
orientation: horizontal;
}
Adw.Carousel {
vexpand: true;
hexpand: true;
spacing: 24;
page-changed => $on_carousel_page_changed();
Gtk.Box {
orientation: vertical;
Gtk.Label {
label: 'Page 01.';
}
}
Gtk.Box {
orientation: vertical;
Gtk.Label {
label: 'Page 02.';
}
}
Gtk.Box {
orientation: vertical;
Gtk.Label {
label: 'Page 03.';
}
}
}
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.CarouselIndicatorDots#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.CarouselIndicatorDots"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.CarouselIndicatorDots()',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
label = Gtk.Label.new(
str='Drag or use the mouse scroller to change pages.'
)
vbox.append(child=label)
separator = Gtk.Separator.new(orientation=Gtk.Orientation.HORIZONTAL)
vbox.append(child=separator)
adw_carousel = Adw.Carousel.new()
adw_carousel.set_vexpand(True)
adw_carousel.set_hexpand(True)
adw_carousel.set_spacing(spacing=24)
adw_carousel.connect('page-changed', self.on_carousel_page_changed)
vbox.append(child=adw_carousel)
for n in range(1, 11):
page = Gtk.Box.new(
orientation=Gtk.Orientation.VERTICAL, spacing=12)
adw_carousel.insert(child=page, position=n)
label = Gtk.Label.new(str=f'Page {n}')
page.append(child=label)
adw_carousel_indicator_dots = Adw.CarouselIndicatorDots.new()
adw_carousel_indicator_dots.set_carousel(carousel=adw_carousel)
vbox.append(child=adw_carousel_indicator_dots)
def on_carousel_page_changed(self, carousel, index):
print(f'Position: {carousel.get_position()}')
print(f'índice: {index}')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.CarouselIndicatorDots."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
UI = BASE_DIR.joinpath('MainWindow.ui')
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
@Gtk.Template.Callback()
def on_carousel_page_changed(self, carousel, index):
print(f'Position: {carousel.get_position()}')
print(f'índice: {index}')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.CarouselIndicatorDots</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="GtkLabel">
<property name="label">Drag or use the mouse scroller to change pages.</property>
</object>
</child>
<child>
<object class="GtkSeparator">
<property name="orientation">0</property>
</object>
</child>
<child>
<object class="AdwCarousel" id="carousel">
<property name="vexpand">true</property>
<property name="hexpand">true</property>
<property name="spacing">24</property>
<signal name="page-changed" handler="on_carousel_page_changed"/>
<child>
<object class="GtkBox">
<property name="orientation">1</property>
<child>
<object class="GtkLabel">
<property name="label">Page 01.</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="orientation">1</property>
<child>
<object class="GtkLabel">
<property name="label">Page 02.</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="orientation">1</property>
<child>
<object class="GtkLabel">
<property name="label">Page 03.</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="AdwCarouselIndicatorDots">
<property name="carousel">carousel</property>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.CarouselIndicatorDots';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
Gtk.Label {
label: 'Drag or use the mouse scroller to change pages.';
}
Separator {
orientation: horizontal;
}
Adw.Carousel carousel {
vexpand: true;
hexpand: true;
spacing: 24;
page-changed => $on_carousel_page_changed();
Gtk.Box {
orientation: vertical;
Gtk.Label {
label: 'Page 01.';
}
}
Gtk.Box {
orientation: vertical;
Gtk.Label {
label: 'Page 02.';
}
}
Gtk.Box {
orientation: vertical;
Gtk.Label {
label: 'Page 03.';
}
}
}
Adw.CarouselIndicatorDots {
carousel: carousel;
}
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.CarouselIndicatorLines#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.CarouselIndicatorLines"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.CarouselIndicatorLines()',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
label = Gtk.Label.new(
str='Drag or use the mouse scroller to change pages.'
)
vbox.append(child=label)
separator = Gtk.Separator.new(orientation=Gtk.Orientation.HORIZONTAL)
vbox.append(child=separator)
adw_carousel = Adw.Carousel.new()
adw_carousel.set_vexpand(True)
adw_carousel.set_hexpand(True)
adw_carousel.set_spacing(spacing=24)
adw_carousel.connect('page-changed', self.on_carousel_page_changed)
vbox.append(child=adw_carousel)
# Loop de repetição para criar os widgets.
for n in range(1, 11):
page = Gtk.Box.new(
orientation=Gtk.Orientation.VERTICAL, spacing=12)
adw_carousel.insert(child=page, position=n)
label = Gtk.Label.new(str=f'Page {n}')
page.append(child=label)
adw_carousel_indicator_dots = Adw.CarouselIndicatorLines.new()
adw_carousel_indicator_dots.set_carousel(carousel=adw_carousel)
vbox.append(child=adw_carousel_indicator_dots)
def on_carousel_page_changed(self, carousel, index):
print(f'Position: {carousel.get_position()}')
print(f'índice: {index}')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.CarouselIndicatorLines."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
UI = BASE_DIR.joinpath('MainWindow.ui')
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
@Gtk.Template.Callback()
def on_carousel_page_changed(self, carousel, index):
print(f'Position: {carousel.get_position()}')
print(f'índice: {index}')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.CarouselIndicatorLines</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="GtkLabel">
<property name="label">Drag or use the mouse scroller to change pages.</property>
</object>
</child>
<child>
<object class="GtkSeparator">
<property name="orientation">0</property>
</object>
</child>
<child>
<object class="AdwCarousel" id="carousel">
<property name="vexpand">true</property>
<property name="hexpand">true</property>
<property name="spacing">24</property>
<signal name="page-changed" handler="on_carousel_page_changed"/>
<child>
<object class="GtkBox">
<property name="orientation">1</property>
<child>
<object class="GtkLabel">
<property name="label">Page 01.</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="orientation">1</property>
<child>
<object class="GtkLabel">
<property name="label">Page 02.</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="orientation">1</property>
<child>
<object class="GtkLabel">
<property name="label">Page 03.</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="AdwCarouselIndicatorLines">
<property name="carousel">carousel</property>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.CarouselIndicatorLines';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
Gtk.Label {
label: 'Drag or use the mouse scroller to change pages.';
}
Separator {
orientation: horizontal;
}
Adw.Carousel carousel {
vexpand: true;
hexpand: true;
spacing: 24;
page-changed => $on_carousel_page_changed();
Gtk.Box {
orientation: vertical;
Gtk.Label {
label: 'Page 01.';
}
}
Gtk.Box {
orientation: vertical;
Gtk.Label {
label: 'Page 02.';
}
}
Gtk.Box {
orientation: vertical;
Gtk.Label {
label: 'Page 03.';
}
}
}
Adw.CarouselIndicatorLines {
carousel: carousel;
}
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.Clamp#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.Clamp"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(title='Python and GTK: PyGObject libadwaita Adw.Clamp')
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
adw_clamp = Adw.Clamp.new()
adw_toolbar_view.set_content(content=adw_clamp)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
adw_clamp.set_child(child=vbox)
list_box = Gtk.ListBox.new()
list_box.set_selection_mode(mode=Gtk.SelectionMode.NONE)
list_box.add_css_class(css_class='boxed-list')
vbox.append(child=list_box)
for n in range(1, 4):
adw_action_row = Adw.ActionRow.new()
adw_action_row.set_title(title=f'Item {n}')
list_box.append(child=adw_action_row)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.Clamp."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
UI = BASE_DIR.joinpath('MainWindow.ui')
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.Clamp</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="AdwClamp">
<property name="child">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<child>
<object class="GtkListBox">
<property name="selection-mode">0</property>
<style>
<class name="boxed-list"/>
</style>
<child>
<object class="AdwActionRow">
<property name="title">Item 01</property>
</object>
</child>
<child>
<object class="AdwActionRow">
<property name="title">Item 02</property>
</object>
</child>
<child>
<object class="AdwActionRow">
<property name="title">Item 03</property>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.Clamp';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Adw.Clamp {
child: Gtk.Box {
orientation: vertical;
spacing: 12;
ListBox {
selection-mode: none;
styles [
'boxed-list',
]
Adw.ActionRow {
title: 'Item 01';
}
Adw.ActionRow {
title: 'Item 02';
}
Adw.ActionRow {
title: 'Item 03';
}
}
};
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.ComboRow#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject Adw.ComboRow"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
items = ['Item 01', 'Item 02', 'Item 03', 'Item 04', 'Item 05']
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject Adw.ComboRow',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
self.listbox = Gtk.ListBox.new()
self.listbox.set_selection_mode(mode=Gtk.SelectionMode.NONE)
self.listbox.add_css_class(css_class='boxed-list')
vbox.append(child=self.listbox)
model = Gtk.StringList.new(strings=self.items)
for n in range(1, 5):
icon = Gtk.Image.new_from_icon_name(
icon_name='accessories-text-editor-symbolic'
)
adw_combo_row = Adw.ComboRow.new()
adw_combo_row.set_title(title=f'Title {n}')
adw_combo_row.set_subtitle(subtitle='Adw.ComboRow')
adw_combo_row.add_prefix(widget=icon)
adw_combo_row.set_model(model=model)
adw_combo_row.connect(
'notify::selected',
self.on_adw_combo_row_selected,
)
adw_combo_row.connect(
'notify::selected-item',
self.on_adw_combo_row_selected_item,
)
self.listbox.append(child=adw_combo_row)
def on_adw_combo_row_selected(self, comborow, GParamUInt):
print(f'Position of the selected item {comborow.get_selected()}')
selected_item = comborow.get_selected_item()
print(f'Text of the selected item {selected_item.get_string()}')
def on_adw_combo_row_selected_item(self, comborow, GParamObject):
print(f'Position of the selected item {comborow.get_selected()}')
selected_item = comborow.get_selected_item()
print(f'Text of the selected item {selected_item.get_string()}')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject Gtk.ListBox() Adw.ComboRow().
blueprint-compiler: `error: unsupported XML tag: <items>`.
"""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
UI = BASE_DIR.joinpath('MainWindow.ui')
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
@Gtk.Template.Callback()
def on_adw_combo_row_selected(self, comborow, GParamUInt):
print(f'Position of the selected item {comborow.get_selected()}')
selected_item = comborow.get_selected_item()
print(f'Text of the selected item {selected_item.get_string()}')
@Gtk.Template.Callback()
def on_adw_combo_row_selected_item(self, comborow, GParamObject):
print(f'Position of the selected item {comborow.get_selected()}')
selected_item = comborow.get_selected_item()
print(f'Text of the selected item {selected_item.get_string()}')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject Adw.ComboRow</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="GtkListBox">
<property name="selection-mode">0</property>
<style>
<class name="boxed-list"/>
</style>
<child>
<object class="AdwComboRow">
<property name="title">Title 01</property>
<property name="model">model</property>
<signal name="notify::selected" handler="on_adw_combo_row_selected"/>
<signal name="notify::selected-item" handler="on_adw_combo_row_selected_item"/>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwComboRow">
<property name="title">Title 02</property>
<property name="model">model</property>
<signal name="notify::selected" handler="on_adw_combo_row_selected"/>
<signal name="notify::selected-item" handler="on_adw_combo_row_selected_item"/>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwComboRow">
<property name="title">Title 03</property>
<property name="model">model</property>
<signal name="notify::selected" handler="on_adw_combo_row_selected"/>
<signal name="notify::selected-item" handler="on_adw_combo_row_selected_item"/>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwComboRow">
<property name="title">Title 04</property>
<property name="model">model</property>
<signal name="notify::selected" handler="on_adw_combo_row_selected"/>
<signal name="notify::selected-item" handler="on_adw_combo_row_selected_item"/>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwComboRow">
<property name="title">Title 05</property>
<property name="model">model</property>
<signal name="notify::selected" handler="on_adw_combo_row_selected"/>
<signal name="notify::selected-item" handler="on_adw_combo_row_selected_item"/>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<object class="GtkStringList" id="model">
<items>
<item>Item 01</item>
<item>Item 02</item>
<item>Item 03</item>
<item>Item 04</item>
<item>Item 05</item>
</items>
</object>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject Adw.ComboRow';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
Gtk.ListBox {
selection-mode: none;
styles [
'boxed-list',
]
Adw.ComboRow {
title: 'Title 01';
model: model;
notify::selected => $on_adw_combo_row_selected();
notify::selected-item => $on_adw_combo_row_selected_item();
[prefix]
Gtk.Image {
icon-name: 'accessories-text-editor-symbolic';
}
}
Adw.ComboRow {
title: 'Title 02';
model: model;
notify::selected => $on_adw_combo_row_selected();
notify::selected-item => $on_adw_combo_row_selected_item();
[prefix]
Gtk.Image {
icon-name: 'accessories-text-editor-symbolic';
}
}
Adw.ComboRow {
title: 'Title 03';
model: model;
notify::selected => $on_adw_combo_row_selected();
notify::selected-item => $on_adw_combo_row_selected_item();
[prefix]
Image {
icon-name: 'accessories-text-editor-symbolic';
}
}
Adw.ComboRow {
title: 'Title 04';
model: model;
notify::selected => $on_adw_combo_row_selected();
notify::selected-item => $on_adw_combo_row_selected_item();
[prefix]
Gtk.Image {
icon-name: 'accessories-text-editor-symbolic';
}
}
Adw.ComboRow {
title: 'Title 05';
model: model;
notify::selected => $on_adw_combo_row_selected();
notify::selected-item => $on_adw_combo_row_selected_item();
[prefix]
Image {
icon-name: 'accessories-text-editor-symbolic';
}
}
}
};
};
};
}
Gtk.StringList model {
strings ['Item 01', 'Item 02', 'Item 03', 'Item 04', 'Item 05']
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.EntryRow#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject Adw.EntryRow"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
items = ['Title 01', 'Title 02', 'Title 03', 'Title 04', 'Title 05']
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject Adw.EntryRow()',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
self.list_box = Gtk.ListBox.new()
self.list_box.set_selection_mode(mode=Gtk.SelectionMode.NONE)
self.list_box.add_css_class(css_class='boxed-list')
vbox.append(child=self.list_box)
for item in self.items:
icon = Gtk.Image.new_from_icon_name(
icon_name='accessories-text-editor-symbolic'
)
adw_entry_row = Adw.EntryRow.new()
adw_entry_row.set_title(title=item)
adw_entry_row.add_prefix(widget=icon)
adw_entry_row.set_show_apply_button(show_apply_button=True)
adw_entry_row.set_activates_default(activates=True)
adw_entry_row.connect('apply', self.on_apply_button_pressed)
self.list_box.append(child=adw_entry_row)
def on_apply_button_pressed(self, entry_row):
print(f'Entry value = {entry_row.get_text()}')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject Gtk.ListBox() Adw.EntryRow"""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
UI = BASE_DIR.joinpath('MainWindow.ui')
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
@Gtk.Template.Callback()
def on_apply_button_pressed(self, entry_row):
print(f'Entry value = {entry_row.get_text()}')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject Adw.EntryRow</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="GtkListBox">
<property name="selection-mode">0</property>
<style>
<class name="boxed-list"/>
</style>
<child>
<object class="AdwEntryRow">
<property name="title">Title 01</property>
<property name="activates-default">true</property>
<property name="show-apply-button">true</property>
<signal name="apply" handler="on_apply_button_pressed"/>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwEntryRow">
<property name="title">Title 02</property>
<property name="activates-default">true</property>
<property name="show-apply-button">true</property>
<signal name="apply" handler="on_apply_button_pressed"/>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwEntryRow">
<property name="title">Title 03</property>
<property name="activates-default">true</property>
<property name="show-apply-button">true</property>
<signal name="apply" handler="on_apply_button_pressed"/>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwEntryRow">
<property name="title">Title 04</property>
<property name="activates-default">true</property>
<property name="show-apply-button">true</property>
<signal name="apply" handler="on_apply_button_pressed"/>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwEntryRow">
<property name="title">Title 05</property>
<property name="activates-default">true</property>
<property name="show-apply-button">true</property>
<signal name="apply" handler="on_apply_button_pressed"/>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject Adw.EntryRow';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
ListBox {
selection-mode: none;
styles [
'boxed-list',
]
Adw.EntryRow {
title: 'Title 01';
activates-default: true;
show-apply-button: true;
apply => $on_apply_button_pressed();
[prefix]
Image {
icon-name: 'accessories-text-editor-symbolic';
}
}
Adw.EntryRow {
title: 'Title 02';
activates-default: true;
show-apply-button: true;
apply => $on_apply_button_pressed();
[prefix]
Image {
icon-name: 'accessories-text-editor-symbolic';
}
}
Adw.EntryRow {
title: 'Title 03';
activates-default: true;
show-apply-button: true;
apply => $on_apply_button_pressed();
[prefix]
Image {
icon-name: 'accessories-text-editor-symbolic';
}
}
Adw.EntryRow {
title: 'Title 04';
activates-default: true;
show-apply-button: true;
apply => $on_apply_button_pressed();
[prefix]
Image {
icon-name: 'accessories-text-editor-symbolic';
}
}
Adw.EntryRow {
title: 'Title 05';
activates-default: true;
show-apply-button: true;
apply => $on_apply_button_pressed();
[prefix]
Image {
icon-name: 'accessories-text-editor-symbolic';
}
}
}
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.ExpanderRow#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject Adw.ExpanderRow"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
text = '<big>Lorem ipsum</big>\n\nLorem ipsum dolor sit amet, consectetur...'
class ExampleWindow(Adw.ApplicationWindow):
items = ['Item 01', 'Item 02', 'Item 03', 'Item 04']
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject Gtk.ListBox() Adw.ExpanderRow()',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
self.list_box = Gtk.ListBox.new()
self.list_box.set_selection_mode(mode=Gtk.SelectionMode.NONE)
self.list_box.add_css_class(css_class='boxed-list')
vbox.append(child=self.list_box)
for item in self.items:
icon = Gtk.Image.new_from_icon_name(
icon_name='accessories-text-editor-symbolic'
)
label = Gtk.Label.new()
label.set_markup(str=text)
label.set_wrap(wrap=True)
adw_expander_row = Adw.ExpanderRow.new()
adw_expander_row.add_prefix(widget=icon)
adw_expander_row.set_title(title=item)
adw_expander_row.set_subtitle(subtitle='Adw.ExpanderRow')
adw_expander_row.add_row(child=label)
self.list_box.append(child=adw_expander_row)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject Adw.ExpanderRow"""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
UI = BASE_DIR.joinpath('MainWindow.ui')
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject Adw.ExpanderRow</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="GtkListBox">
<property name="selection-mode">0</property>
<style>
<class name="boxed-list"/>
</style>
<child>
<object class="AdwExpanderRow">
<property name="title">Item 01</property>
<property name="subtitle">Adw.ExpanderRow()</property>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label"><big>Lorem ipsum</big>
Lorem ipsum dolor sit amet, consectetur...</property>
<property name="use-markup">true</property>
<property name="wrap">true</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwExpanderRow">
<property name="title">Item 02</property>
<property name="subtitle">Adw.ExpanderRow()</property>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label"><big>Lorem ipsum</big>
Lorem ipsum dolor sit amet, consectetur...</property>
<property name="use-markup">true</property>
<property name="wrap">true</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwExpanderRow">
<property name="title">Item 03</property>
<property name="subtitle">Adw.ExpanderRow()</property>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label"><big>Lorem ipsum</big>
Lorem ipsum dolor sit amet, consectetur...</property>
<property name="use-markup">true</property>
<property name="wrap">true</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwExpanderRow">
<property name="title">Item 04</property>
<property name="subtitle">Adw.ExpanderRow()</property>
<child type="prefix">
<object class="GtkImage">
<property name="icon-name">accessories-text-editor-symbolic</property>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label"><big>Lorem ipsum</big>
Lorem ipsum dolor sit amet, consectetur...</property>
<property name="use-markup">true</property>
<property name="wrap">true</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject Adw.ExpanderRow';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
ListBox {
selection-mode: none;
styles [
'boxed-list',
]
Adw.ExpanderRow {
title: 'Item 01';
subtitle: 'Adw.ExpanderRow()';
[prefix]
Image {
icon-name: 'accessories-text-editor-symbolic';
}
Gtk.Label {
label: '<big>Lorem ipsum</big>\n\nLorem ipsum dolor sit amet, consectetur...';
use-markup: true;
wrap: true;
}
}
Adw.ExpanderRow {
title: 'Item 02';
subtitle: 'Adw.ExpanderRow()';
[prefix]
Image {
icon-name: 'accessories-text-editor-symbolic';
}
Gtk.Label {
label: '<big>Lorem ipsum</big>\n\nLorem ipsum dolor sit amet, consectetur...';
use-markup: true;
wrap: true;
}
}
Adw.ExpanderRow {
title: 'Item 03';
subtitle: 'Adw.ExpanderRow()';
[prefix]
Image {
icon-name: 'accessories-text-editor-symbolic';
}
Gtk.Label {
label: '<big>Lorem ipsum</big>\n\nLorem ipsum dolor sit amet, consectetur...';
use-markup: true;
wrap: true;
}
}
Adw.ExpanderRow {
title: 'Item 04';
subtitle: 'Adw.ExpanderRow()';
[prefix]
Gtk.Image {
icon-name: 'accessories-text-editor-symbolic';
}
Gtk.Label {
label: '<big>Lorem ipsum</big>\n\nLorem ipsum dolor sit amet, consectetur...';
use-markup: true;
wrap: true;
}
}
}
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.MessageDialog#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.MessageDialog"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class Dialog(Adw.MessageDialog):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_heading(heading='Dialog header')
self.set_body(body='Body of the dialog window, you can use markup.')
self.add_response(Gtk.ResponseType.CANCEL.value_nick, 'Cancelar')
self.set_response_appearance(
response=Gtk.ResponseType.CANCEL.value_nick,
appearance=Adw.ResponseAppearance.DESTRUCTIVE,
)
self.add_response(Gtk.ResponseType.OK.value_nick, 'OK')
self.set_response_appearance(
response=Gtk.ResponseType.OK.value_nick,
appearance=Adw.ResponseAppearance.SUGGESTED,
)
self.connect('response', self.dialog_response)
def dialog_response(self, dialog, response):
if response == Gtk.ResponseType.OK.value_nick:
print('OK button pressed')
elif response == Gtk.ResponseType.CANCEL.value_nick:
print('CANCEL button pressed')
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.MessageDialog()',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
button = Gtk.Button.new_with_label(label='Click here')
button.set_valign(align=Gtk.Align.CENTER)
button.set_vexpand(expand=True)
button.connect('clicked', self.on_button_clicked)
vbox.append(child=button)
def on_button_clicked(self, button):
# Using a class.
# dialog = Dialog(transient_for=self)
# dialog.present()
dialog = Adw.MessageDialog.new(parent=self)
dialog.set_heading(heading='Dialog header')
dialog.set_body(
body='Body of the dialog window, you can use markup.',
)
dialog.add_response(Gtk.ResponseType.CANCEL.value_nick, 'Cancelar')
dialog.set_response_appearance(
response=Gtk.ResponseType.CANCEL.value_nick,
appearance=Adw.ResponseAppearance.DESTRUCTIVE,
)
dialog.add_response(Gtk.ResponseType.OK.value_nick, 'OK')
dialog.set_response_appearance(
response=Gtk.ResponseType.OK.value_nick,
appearance=Adw.ResponseAppearance.SUGGESTED,
)
dialog.connect('response', self.dialog_response)
dialog.present()
def dialog_response(self, dialog, response):
if response == Gtk.ResponseType.OK.value_nick:
print('OK button pressed')
elif response == Gtk.ResponseType.CANCEL.value_nick:
print('CANCEL button pressed')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.MessageDialog."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
UI = BASE_DIR.joinpath('MainWindow.ui')
DIALOG = str(BASE_DIR.joinpath('AdwMessageDialog.ui'))
@Gtk.Template(filename=DIALOG)
class MessageDialog(Adw.MessageDialog):
__gtype_name__ = 'MessageDialog'
def __init__(self, **kwargs):
super(MessageDialog, self).__init__(**kwargs)
@Gtk.Template.Callback()
def dialog_response(self, dialog, response):
if response == Gtk.ResponseType.OK.value_nick:
print('OK button pressed')
elif response == Gtk.ResponseType.CANCEL.value_nick:
print('CANCEL button pressed')
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super(ExampleWindow, self).__init__(**kwargs)
@Gtk.Template.Callback()
def on_button_clicked(self, button):
dialog = MessageDialog(transient_for=self)
dialog.present()
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.MessageDialog</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="GtkButton">
<property name="label">Click here</property>
<property name="valign">3</property>
<property name="vexpand">true</property>
<signal name="clicked" handler="on_button_clicked"/>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
Adw.PasswordEntryRow#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject Gtk.ListBox() Adw.PasswordEntryRow"""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
UI = BASE_DIR.joinpath('MainWindow.ui')
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.PasswordEntryRow</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="AdwPasswordEntryRow"></object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.PasswordEntryRow';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
Adw.PasswordEntryRow {
}
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.PreferencesPage#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.PreferencesPage"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.AdwPreferencesPage',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
adw_preferences_page = Adw.PreferencesPage.new()
vbox.append(child=adw_preferences_page)
button_flat = Gtk.Button.new_with_label(label='Suffix')
button_flat.set_icon_name(icon_name='list-add-symbolic')
button_flat.add_css_class(css_class='flat')
button_flat.connect('clicked', self.on_button_clicked)
adw_preferences_group = Adw.PreferencesGroup.new()
adw_preferences_group.set_title(title='AdwPreferencesPage')
adw_preferences_group.set_description(
description='AdwPreferencesGroup')
adw_preferences_group.set_header_suffix(suffix=button_flat)
adw_preferences_page.add(group=adw_preferences_group)
switch_01 = Gtk.Switch.new()
switch_01.set_valign(align=Gtk.Align.CENTER)
switch_01.connect('notify::active', self.on_switch_button_clicked)
adw_action_row_01 = Adw.ActionRow.new()
adw_action_row_01.add_prefix(
widget=Gtk.Image.new_from_icon_name(
icon_name='edit-find-symbolic'),
)
adw_action_row_01.set_title(title='Libadwaita')
adw_action_row_01.set_subtitle(subtitle='Adw.ActionRow')
adw_action_row_01.add_suffix(widget=switch_01)
adw_preferences_group.add(child=adw_action_row_01)
switch_02 = Gtk.Switch.new()
switch_02.set_valign(align=Gtk.Align.CENTER)
switch_02.connect('notify::active', self.on_switch_button_clicked)
adw_action_row_02 = Adw.ActionRow.new()
adw_action_row_02.add_prefix(
widget=Gtk.Image.new_from_icon_name(
icon_name='edit-find-symbolic'),
)
adw_action_row_02.set_title(
title='Libadwaita - Clicking on the widget line toggles it on and off'
)
adw_action_row_02.set_subtitle(subtitle='Adw.ActionRow')
adw_action_row_02.add_suffix(widget=switch_02)
adw_action_row_02.set_activatable_widget(widget=switch_02)
adw_preferences_group.add(child=adw_action_row_02)
def on_button_clicked(self, button):
print('Button pressed.')
def on_switch_button_clicked(self, switch, GParamBoolean):
if switch.get_active():
print('Button checked')
else:
print('Button unchecked')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.PreferencesPage."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
UI = BASE_DIR.joinpath('MainWindow.ui')
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
@Gtk.Template.Callback()
def on_button_clicked(self, button):
print('Button pressed.')
@Gtk.Template.Callback()
def on_switch_button_clicked(self, switch, GParamBoolean):
if switch.get_active():
print('Button checked')
else:
print('Button unchecked')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels')_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.PreferencesPage</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="AdwPreferencesPage">
<child>
<object class="AdwPreferencesGroup">
<property name="title">AdwPreferencesPage</property>
<property name="description">AdwPreferencesGroup</property>
<property name="header-suffix">header_suffix</property>
<child>
<object class="AdwActionRow">
<property name="icon-name">edit-find-symbolic</property>
<property name="title">Libadwaita</property>
<property name="subtitle">Adw.ActionRow()</property>
<child>
<object class="GtkSwitch">
<property name="valign">3</property>
<signal name="notify::active" handler="on_switch_button_clicked"/>
</object>
</child>
</object>
</child>
<child>
<object class="AdwActionRow">
<property name="icon-name">edit-find-symbolic</property>
<property name="title">Libadwaita - Clicking on the widget line toggles it on and off
</property>
<property name="subtitle">Adw.ActionRow()</property>
<property name="activatable-widget">switch</property>
<child>
<object class="GtkSwitch" id="switch">
<property name="valign">3</property>
<signal name="notify::active" handler="on_switch_button_clicked"/>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<object class="GtkButton" id="header_suffix">
<property name="icon-name">list-add-symbolic</property>
<signal name="clicked" handler="on_button_clicked"/>
<style>
<class name="flat"/>
</style>
</object>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.PreferencesPage';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Gtk.Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
Adw.PreferencesPage {
Adw.PreferencesGroup {
title: 'AdwPreferencesPage';
description: 'AdwPreferencesGroup';
header-suffix: header_suffix;
Adw.ActionRow {
icon-name: 'edit-find-symbolic';
title: 'Libadwaita';
subtitle: 'Adw.ActionRow()';
Switch {
valign: center;
notify::active => $on_switch_button_clicked();
}
}
Adw.ActionRow {
icon-name: 'edit-find-symbolic';
title: 'Libadwaita - Clicking on the widget line toggles it on and off\n';
subtitle: 'Adw.ActionRow()';
activatable-widget: switch;
Switch switch {
valign: center;
notify::active => $on_switch_button_clicked();
}
}
}
}
};
};
};
}
Button header_suffix {
icon-name: 'list-add-symbolic';
clicked => $on_button_clicked();
styles [
'flat',
]
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.PreferencesWindow#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.PreferencesWindow."""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class AdwPreferencesWindow(Adw.PreferencesWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.PreferencesWindow',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
# adw_toolbar_view.set_content(content=vbox)
adw_preferences_page = Adw.PreferencesPage.new()
adw_toolbar_view.set_content(content=adw_preferences_page)
# self.add(page=adw_preferences_page)
adw_preferences_group = Adw.PreferencesGroup.new()
adw_preferences_group.set_title(title='AdwPreferencesPage')
adw_preferences_group.set_description(
description='AdwPreferencesGroup')
adw_preferences_page.add(group=adw_preferences_group)
adw_action_row_01 = Adw.ActionRow.new()
adw_action_row_01.set_title(title='Libadwaita')
adw_action_row_01.set_subtitle(subtitle='Adw.ActionRow')
adw_preferences_group.add(child=adw_action_row_01)
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.PreferencesWindow',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
button = Gtk.Button.new_with_label(label='Click here')
button.set_vexpand(expand=True)
button.set_valign(align=Gtk.Align.END)
button.connect('clicked', self.on_button_clicked)
vbox.append(child=button)
def on_button_clicked(self, button):
adw_preferences_window = AdwPreferencesWindow(transient_for=self)
adw_preferences_window.present()
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.PreferencesWindow."""
import sys
import pathlib
from AdwPreferencesWindow import AdwPreferencesWindow
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
UI = BASE_DIR.joinpath('MainWindow.ui')
Adw.init()
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
@Gtk.Template.Callback()
def on_button_clicked(self, button):
adw_preferences_window = AdwPreferencesWindow(transient_for=self)
adw_preferences_window.present()
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.PreferencesWindow</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<property name="spacing">12</property>
<child>
<object class="GtkButton">
<property name="label">Click here</property>
<property name="vexpand">true</property>
<property name="valign">2</property>
<signal name="clicked" handler="on_button_clicked"/>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.PreferencesWindow';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Box {
orientation: vertical;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
spacing: 12;
Button {
label: 'Click here';
vexpand: true;
valign: end;
clicked => $on_button_clicked();
}
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.StatusPage#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.StatusPage"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.StatusPage',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
status_page = Adw.StatusPage.new()
status_page.set_description(description='Descrição do status page.')
status_page.set_icon_name(icon_name='face-smile-big-symbolic')
status_page.set_title(title='Título do status page.')
vbox.append(child=status_page)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.StatusPage."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
UI = BASE_DIR.joinpath('MainWindow.ui')
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.StatusPage</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="AdwStatusPage">
<property name="description">Status page description.</property>
<property name="icon-name">face-smile-big-symbolic</property>
<property name="title">Status page title.</property>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.StatusPage';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
Adw.StatusPage {
description: 'Status page description.';
icon-name: 'face-smile-big-symbolic';
title: 'Status page title.';
}
};
};
};
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.Toast#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.Toast"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(title='Python and GTK: PyGObject libadwaita Adw.Toast')
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
self.adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=self.adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
self.adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
vbox.set_margin_top(margin=12)
vbox.set_margin_end(margin=12)
vbox.set_margin_bottom(margin=12)
vbox.set_margin_start(margin=12)
adw_toolbar_view.set_content(content=vbox)
self.button = Gtk.Button.new_with_label(label='Click here')
self.button.set_vexpand(expand=True)
self.button.set_valign(align=Gtk.Align.END)
self.button.connect('clicked', self.on_button_clicked)
vbox.append(child=self.button)
self.toast = Adw.Toast.new(title='Lorem Ipsum')
self.toast.set_button_label(button_label='Undo')
self.toast.set_action_name(action_name='app.toast')
self.toast.connect('dismissed', self.on_toast_dismissed)
self.toast.connect('button-clicked', self.on_toast_button_clicked)
def on_button_clicked(self, button):
button.set_sensitive(sensitive=False)
self.adw_toast_overlay.add_toast(self.toast)
def on_toast_dismissed(self, toast):
"""Emitted when the toast has been dismissed."""
print('[!] dismissed [!]')
print('Emitted when the toast has been dismissed')
self.button.set_sensitive(sensitive=True)
def on_toast_button_clicked(self, toast):
"""Emitted after the button has been clicked."""
print('[!] button-clicked [!]')
print('Emitted after the button has been clicked.')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
self.create_action('toast', self.on_toast_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def on_toast_action(self, action, param):
"""It will be activated when clicking the button."""
print('[!] action-name [!]')
print('Action `app.toast` was active.')
print('It will be activated when clicking the button')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.Toast."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
UI = BASE_DIR.joinpath('MainWindow.ui')
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
button = Gtk.Template.Child(name='button')
toast = Gtk.Template.Child(name='toast')
adw_toast_overlay = Gtk.Template.Child(name='adw_toast_overlay')
def __init__(self, **kwargs):
super().__init__(**kwargs)
@Gtk.Template.Callback()
def on_button_clicked(self, button):
button.set_sensitive(sensitive=False)
self.adw_toast_overlay.add_toast(self.toast)
@Gtk.Template.Callback()
def on_toast_dismissed(self, toast):
"""Emitted when the toast has been dismissed."""
print('[!] dismissed [!]')
print('Emitted when the toast has been dismissed')
self.button.set_sensitive(sensitive=True)
@Gtk.Template.Callback()
def on_toast_button_clicked(self, toast):
"""Emitted after the button has been clicked."""
print('[!] button-clicked [!]')
print('Emitted after the button has been clicked.')
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
self.create_action('toast', self.on_toast_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def on_toast_action(self, action, param):
"""It will be activated when clicking the button."""
print('[!] action-name [!]')
print('Action `app.toast` was active.')
print('It will be activated when clicking the button')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.Toast</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="content">
<object class="AdwToastOverlay" id="adw_toast_overlay">
<property name="child">
<object class="AdwToolbarView">
<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>
</object>
</child>
</object>
</child>
<property name="content">
<object class="AdwToastOverlay" id="toast_overlay">
<property name="child">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">12</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<child>
<object class="GtkButton" id="button">
<property name="label">Click here</property>
<property name="vexpand">true</property>
<property name="valign">2</property>
<signal name="clicked" handler="on_button_clicked"/>
</object>
</child>
</object>
</property>
</object>
</property>
</object>
</property>
</object>
</property>
</template>
<object class="AdwToast" id="toast">
<property name="title">Lorem Ipsum</property>
<property name="button-label">Undo</property>
<property name="action-name">app.toast</property>
<signal name="dismissed" handler="on_toast_dismissed"/>
<signal name="button-clicked" handler="on_toast_button_clicked"/>
</object>
<menu id="primary_menu">
<section>
<item>
<attribute name="label" translatable="true">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
</menu>
</interface>
using Gtk 4.0;
using Adw 1;
template $ExampleWindow : Adw.ApplicationWindow {
title: 'Python and GTK: PyGObject libadwaita Adw.Toast';
default-width: 683;
default-height: 384;
content: Adw.ToastOverlay adw_toast_overlay {
child: Adw.ToolbarView {
[top]
Adw.HeaderBar header_bar {
[end]
MenuButton {
icon-name: 'open-menu-symbolic';
menu-model: primary_menu;
}
}
content: Adw.ToastOverlay toast_overlay {
child: Box {
orientation: vertical;
spacing: 12;
margin-top: 12;
margin-end: 12;
margin-bottom: 12;
margin-start: 12;
Button button {
label: 'Click here';
vexpand: true;
valign: end;
clicked => $on_button_clicked();
}
};
};
};
};
}
Adw.Toast toast {
title: 'Lorem Ipsum';
button-label: 'Undo';
action-name: 'app.toast';
dismissed => $on_toast_dismissed();
button-clicked => $on_toast_button_clicked();
}
menu primary_menu {
section {
item {
label: _('Preferences');
action: 'app.preferences';
}
}
}
Adw.ToolbarView#
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.ToolbarView"""
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
Adw.init()
class ExampleWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(
title='Python and GTK: PyGObject libadwaita Adw.ToolbarView',
)
self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
self.set_size_request(width=int(1366 / 3), height=int(768 / 3))
adw_toast_overlay = Adw.ToastOverlay.new()
self.set_content(content=adw_toast_overlay)
adw_toolbar_view = Adw.ToolbarView.new()
adw_toast_overlay.set_child(child=adw_toolbar_view)
adw_header_bar = Adw.HeaderBar.new()
adw_toolbar_view.add_top_bar(widget=adw_header_bar)
menu_button_model = Gio.Menu()
menu_button_model.append(
label='Preferences',
detailed_action='app.preferences',
)
menu_button = Gtk.MenuButton.new()
menu_button.set_icon_name(icon_name='open-menu-symbolic')
menu_button.set_menu_model(menu_model=menu_button_model)
adw_header_bar.pack_end(child=menu_button)
adw_preferences_page = Adw.PreferencesPage.new()
adw_toolbar_view.set_content(content=adw_preferences_page)
button_flat = Gtk.Button.new_with_label(label='Suffix')
button_flat.set_icon_name(icon_name='list-add-symbolic')
button_flat.add_css_class(css_class='flat')
adw_preferences_group = Adw.PreferencesGroup.new()
adw_preferences_group.set_title(title='AdwPreferencesPage')
adw_preferences_group.set_description(
description='AdwPreferencesGroup')
adw_preferences_group.set_header_suffix(suffix=button_flat)
adw_preferences_page.add(group=adw_preferences_group)
switch_01 = Gtk.Switch.new()
switch_01.set_valign(align=Gtk.Align.CENTER)
adw_action_row_01 = Adw.ActionRow.new()
adw_action_row_01.add_prefix(
widget=Gtk.Image.new_from_icon_name(
icon_name='edit-find-symbolic'),
)
adw_action_row_01.set_title(title='Libadwaita')
adw_action_row_01.set_subtitle(subtitle='Adw.ActionRow')
adw_action_row_01.add_suffix(widget=switch_01)
adw_preferences_group.add(child=adw_action_row_01)
switch_02 = Gtk.Switch.new()
switch_02.set_valign(align=Gtk.Align.CENTER)
adw_action_row_02 = Adw.ActionRow.new()
adw_action_row_02.add_prefix(
widget=Gtk.Image.new_from_icon_name(
icon_name='edit-find-symbolic'),
)
adw_action_row_02.set_title(title='Libadwaita')
adw_action_row_02.set_subtitle(subtitle='Adw.ActionRow')
adw_action_row_02.add_suffix(widget=switch_02)
adw_action_row_02.set_activatable_widget(widget=switch_02)
adw_preferences_group.add(child=adw_action_row_02)
# Barra inferior.
hbox_bottom_bar = Gtk.Box.new(orientation=Gtk.Orientation.HORIZONTAL, spacing=12)
hbox_bottom_bar.set_margin_top(margin=12)
hbox_bottom_bar.set_margin_end(margin=12)
hbox_bottom_bar.set_margin_bottom(margin=12)
hbox_bottom_bar.set_margin_start(margin=12)
adw_toolbar_view.add_bottom_bar(widget=hbox_bottom_bar)
label = Gtk.Label.new()
label.set_text(str='Adw.ToolbarView - Barra inferior')
hbox_bottom_bar.append(child=label)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)
Adw.ViewStack#
Aviso
Criar código com Blueprint
# -*- coding: utf-8 -*-
"""Python and GTK: PyGObject libadwaita Adw.ViewStack."""
import sys
import pathlib
import gi
gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')
from gi.repository import Adw, Gio, Gtk
UI = BASE_DIR.joinpath('MainWindow.ui')
Adw.init()
@Gtk.Template(filename=str(UI))
class ExampleWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ExampleWindow'
def __init__(self, **kwargs):
super().__init__(**kwargs)
class ExampleApplication(Adw.Application):
def __init__(self):
super().__init__(application_id='br.com.justcode.PyGObject',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.exit_app, ['<primary>q'])
self.create_action('preferences', self.on_preferences_action)
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def do_shutdown(self):
Gtk.Application.do_shutdown(self)
def on_preferences_action(self, action, param):
print('Action `app.preferences` was active.')
def exit_app(self, action, param):
self.quit()
def create_action(self, name, callback, shortcuts=None):
action = Gio.SimpleAction.new(name=name, parameter_type=None)
action.connect('activate', callback)
self.add_action(action=action)
if shortcuts:
self.set_accels_for_action(
detailed_action_name=f'app.{name}',
accels=shortcuts,
)
if __name__ == '__main__':
app = ExampleApplication()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="ExampleWindow" parent="AdwApplicationWindow">
<property name="title">Python and GTK: PyGObject libadwaita Adw.ViewStack</property>
<property name="default-width">683</property>
<property name="default-height">384</property>
<property name="width-request">500</property>
<property name="height-request">300</property>
<child>
<object class="AdwBreakpoint">
<condition>max-width: 550</condition>
<setter object="switcher_bar" property="reveal">true</setter>
<setter object="header_bar" property="title-widget">title_widget</setter>
</object>
</child>
<property name="content">
<object class="AdwToastOverlay">
<property name="child">
<object class="AdwToolbarView">
<child type="top">
<object class="AdwHeaderBar" id="header_bar">
<property name="title-widget">
<object class="AdwViewSwitcher">
<property name="stack">stack</property>
<property name="policy">1</property>
</object>
</property>
</object>
</child>
<property name="content">
<object class="AdwViewStack" id="stack">
<child>
<object class="AdwViewStackPage">
<property name="name">page01</property>
<property name="title">Page 01</property>
<property name="child">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<property name="spacing">12</property>
<child>
<object class="GtkLabel">
<property name="label">Page 01</property>
</object>
</child>
</object>
</property>
</object>
</child>
<child>
<object class="AdwViewStackPage">
<property name="name">page02</property>
<property name="title">Page 02</property>
<property name="child">
<object class="GtkBox">
<property name="orientation">1</property>
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<property name="spacing">12</property>
<child>
<object class="GtkLabel">
<property name="label">Page 02</property>
</object>
</child>
</object>
</property>
</object>
</child>
</object>
</property>
<child type="bottom">
<object class="AdwViewSwitcherBar" id="switcher_bar">
<property name="stack">stack</property>
</object>
</child>
</object>
</property>
</object>
</property>
</template>
<object class="GtkLabel" id="title_widget">
<property name="label">Python and GTK: PyGObject libadwaita Adw.ViewStack</property>
</object>
</interface>