Paepoi » GTK4(Python) Tips » GTK4(Python) Tips | Adw
GTK4(Python) Tips | Adw
# 最終更新日 2024.11.04
以下のように AdwStyleManager を設定するだけで適用されるようになります。
又ダークテーマへの追従もこれで指定できます。

一般的な形にするには AdwToolbarView を上に載せ AdwHeaderBar を配置。
後は AdwToolbarView にコンテンツを配置していくという形になっています。
GtkBox を使って同じようにもできますが本格的に作るなら従ったほうが無難。

Adw.ToastOverlay の内部に Adw.Torst を作り add_toast で表示させます。
Adwaita スタイルにしないと上手く動作しませんので注意。

統一された色分けが簡単になったり上に部品を載せるプロパティがあったりで高機能です。
今後メッセージ関連はこちらを使うと幸せになれると思います。

gnome-text-editor や nautilus で採用されている About です。
GtkAboutDialog 同様にプロパティを埋めていくだけで作れます。
オーバーレイ表示になったので呼び出しするウインドウは大きくしましょう。

Adwaita Style
GTK4 で作成されたアプリは Adwaita の統一されたスタイルになっています。以下のように AdwStyleManager を設定するだけで適用されるようになります。
又ダークテーマへの追従もこれで指定できます。
#!/usr/bin/env python3
import gi, sys
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw
class Win(Gtk.ApplicationWindow):
'''
PREFER_LIGHT PREFER_DARK は意味が無い
'''
def __init__(self, a):
Gtk.ApplicationWindow.__init__(self, application=a)
# Set Adwaita Style
manager = Adw.StyleManager.get_default()
# ダークテーマを OS の設定に追従させる
#manager.set_color_scheme(Adw.ColorScheme.DEFAULT)
# 常にダークテーマ
manager.set_color_scheme(Adw.ColorScheme.FORCE_DARK)
# 常にライトテーマ
#manager.set_color_scheme(Adw.ColorScheme.FORCE_LIGHT)
#
label = Gtk.Label(label='ダークテーマ')
self.set_child(label)
app = Gtk.Application()
app.connect('activate', lambda a: Win(a).present())
app.run()

Window
AdwWindow には GtkWindow にあるタイトルバーがありません。一般的な形にするには AdwToolbarView を上に載せ AdwHeaderBar を配置。
後は AdwToolbarView にコンテンツを配置していくという形になっています。
GtkBox を使って同じようにもできますが本格的に作るなら従ったほうが無難。
#!/usr/bin/env python3
import gi, sys
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw
class Win(Adw.ApplicationWindow):
'''
set_child ではなく set_content なことに注意
'''
def __init__(self, a):
# Set Adwaita Style
manager = Adw.StyleManager.get_default()
manager.set_color_scheme(Adw.ColorScheme.DEFAULT)
# init
Gtk.ApplicationWindow.__init__(self, application=a)
# view
title = Adw.WindowTitle(title='Title', subtitle='sub title')
header = Adw.HeaderBar(title_widget=title)
mein_view = Adw.ToolbarView()
mein_view.add_top_bar(header)
# content
button = Gtk.Label(label='Main Content')
mein_view.set_content(button)
# self
self.set_content(mein_view)
self.set_default_size(600, 300)
app = Adw.Application()
app.connect('activate', lambda a: Win(a).present())
app.run()

Toast
昔ながらのトースターで食パンが跳ね上がるようにメッセージを出すウイジェット。Adw.ToastOverlay の内部に Adw.Torst を作り add_toast で表示させます。
Adwaita スタイルにしないと上手く動作しませんので注意。
#!/usr/bin/env python3
import gi, sys
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw
class Win(Gtk.ApplicationWindow):
'''
GTK4: No Decorated Window
'''
def __init__(self, a):
# Set Adwaita Style
manager = Adw.StyleManager.get_default()
manager.set_color_scheme(Adw.ColorScheme.DEFAULT)
# init
Gtk.ApplicationWindow.__init__(self, application=a)
# Button
button = Gtk.Button(label='Show Toast', halign=Gtk.Align.CENTER, valign=Gtk.Align.CENTER)
button.connect('clicked', self.on_button_clicked)
# Toast
self.toast = Adw.ToastOverlay(child=button)
self.set_child(self.toast)
self.set_default_size(200, 200)
def on_button_clicked(self, button):
toast = Adw.Toast(title='Hello World')
self.toast.add_toast(toast)
app = Gtk.Application()
app.connect('activate', lambda a: Win(a).present())
app.run()

AlertDialog
AdwMessageDialog は Adw 1.6 より非推奨になりました。統一された色分けが簡単になったり上に部品を載せるプロパティがあったりで高機能です。
今後メッセージ関連はこちらを使うと幸せになれると思います。
#!/usr/bin/env python3
import gi, sys
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw
class Win(Adw.ApplicationWindow):
'''
Gtk.ApplicationWindow is Gtk-CRITICAL
Widget of type “AdwAlertDialog” already has an accessible role of type “GTK_ACCESSIBLE_ROLE_GENERIC”
'''
def __init__(self, a):
# Set Adwaita Style
manager = Adw.StyleManager.get_default()
manager.set_color_scheme(Adw.ColorScheme.DEFAULT)
# init
Adw.ApplicationWindow.__init__(self, application=a)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
# Header
title = Adw.WindowTitle(title='test')
header = Adw.HeaderBar(title_widget=title)
vbox.append(header)
for s in ('Simple', 'Yes No', 'Vertical'):
# Button
button = Gtk.Button(label=s)
button.connect('clicked', self.on_button_clicked, s)
vbox.append(button)
# Toast
self.set_content(vbox)
self.set_default_size(600, 500)
def on_button_clicked(self, button, text):
match text:
case 'Simple':
alert = Adw.AlertDialog(body='Same for ESC')
alert.add_response('close', 'OK')
alert.present(self)
case 'Yes No':
alert = Adw.AlertDialog(body='Close to the Edge')
# add_responces is no...
alert.add_response('close', 'No')
alert.add_response('yes', 'Yes')
alert.set_response_appearance('yes', Adw.ResponseAppearance.SUGGESTED)
alert.choose(self, None, self.on_dialog_responce)
case 'Vertical':
alert = Adw.AlertDialog(heading='Head', body='body', prefer_wide_layout=True)
alert.add_response('close', 'Cancel')
alert.add_response('no', 'Destroy this document')
alert.add_response('ok', 'Save this document')
alert.set_response_appearance('ok', Adw.ResponseAppearance.SUGGESTED)
alert.set_response_appearance('no', Adw.ResponseAppearance.DESTRUCTIVE)
alert.choose(self, None, self.on_dialog_responce)
case _:
alert = Adw.AlertDialog(body='???')
alert.add_response('close', 'ok')
alert.present(self)
def on_dialog_responce(self, alert, res):
text = alert.choose_finish(res)
match text:
case 'ok':
print('Saved')
case 'no':
print('Discarded')
case 'yes':
print("You're a fan of Yes")
case 'close':
print('Oh my good!')
app = Gtk.Application()
app.connect('activate', lambda a: Win(a).present())
app.run()

AboutDialog
AdwAboutWindow は Adw 1.6 より非推奨になりましたので変更を。gnome-text-editor や nautilus で採用されている About です。
GtkAboutDialog 同様にプロパティを埋めていくだけで作れます。
オーバーレイ表示になったので呼び出しするウインドウは大きくしましょう。
#!/usr/bin/env python3
import gi, sys
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw
class Win(Adw.ApplicationWindow):
'''
このダイアログは Adw.ApplicationWindow から呼び出さないとエラー
'''
def __init__(self, a):
# Set Adwaita Style
manager = Adw.StyleManager.get_default()
manager.set_color_scheme(Adw.ColorScheme.DEFAULT)
# init
Gtk.ApplicationWindow.__init__(self, application=a)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
# Header
title = Adw.WindowTitle(title='about')
header = Adw.HeaderBar(title_widget=title)
vbox.append(header)
# Button
button = Gtk.Button(label='Show About', halign=Gtk.Align.CENTER, valign=Gtk.Align.CENTER)
button.connect('clicked', self.on_button_clicked)
# self
vbox.append(button)
self.set_content(vbox)
self.set_default_size(600, 600)
def on_button_clicked(self, button):
d = Adw.AboutDialog(
developer_name='sasakima-nao',
developers=['sasakima-nao'],
copyright='© 2023 sasakima-nao',
license_type=Gtk.License.GPL_2_0,
#application_icon='org.sasakima.Comipoli',
application_name='Comipoli',
version='0.0.1',
website='http://palepoli.skr.jp/')
d.present(self)
app = Gtk.Application()
app.connect('activate', lambda a: Win(a).present())
app.run()

Copyright(C) sasakima-nao All rights reserved 2002 --- 2025.