XMLデータにより定義したGtk.MessageDialogの使用
Gtk.MessageDialogは、Gtk4でメッセージを表示するのに利用するWidgetです。Gtk.MessageDialogは、Gtk4.10より非推奨となりましたが、Gtk4の間は使用することが可能です。(代替Widgetは、Gtk.AlertDialogです。)
Gtk.MessageDialogもXMLデータで作成することが可能であり、Gtk.MessageDialogに載せるボタンの作成やその処理を記入する関数の指定もおこなえます。
今回は、Gtk.MessageDialogをXMLデータで作成する方法と作成したGtk.MessageDialogを表示・非表示にする方法について紹介します。
今回紹介する内容
・ Gtk.MessageDialogを作成する
・ Gtk.MessageDialogにボタンを追加する
・ ボタンのイベントを設定する
・ Gtk.MessageDialogを表示する
XMLデータでのGtk.MessageDialogの記入
Gtk.MessageDialogを作成する
XMLデータでGtk.MessageDialogを作成する場合、以下のように記入します。
・ </template>と</interface>の間に記入する。
・ <object class=”GtkMessageDialog” id=”(id名)”>と</object>を書き、
その間にプロパティなどを記入する。
・ プロパティtransient-forの値は、id名で指定します。
</template>
<object class="GtkMessageDialog" id="messagedialog">
<property name="text">テキスト</property>
<property name="secondary-text">第2テキスト</property>
<property name="message-type">info</property>
<property name="transient-for">window</property>
<property name="modal">True</property>
…省略…
</object>
Gtk.MessageDialogにボタンを追加する
Gtk.MessageDialogへのボタンの追加は、
1. <child type=”action”></child>内に、Gtk.Buttonによりボタン内容を記入する。
2. <action-widgets></action-widgets>内に、Gtk.Button(id名で指定)を押した場合の
responseや、デフォルトのボタンにするかを指定します。
<object class="GtkMessageDialog" id="messagedialog">
…省略…
<child type="action">
<object class="GtkButton" id="button_ok">
<property name="label">OK</property>
</object>
</child>
<action-widgets>
<action-widget response="ok" default="true">button_ok</action-widget>
</action-widgets>
</object>
ボタンのイベントを設定する
シグナル’response’とon_dialog_responseとの紐付けをおこない、Gtk.MessageDialogのボタンが押されたらon_dialog_responseを呼ぶように指定します。
<object class="GtkMessageDialog" id="messagedialog">
…省略…
<signal name="response" handler="on_dialog_response"/>
…省略…
</object>
Gtk.MessageDialogのボタンを押すとon_dialog_responseが呼ばれますので、Gtk.MessageDialogで押されたボタンの判定とそのボタンにあった処理を記入します。
最後に、メソッドset_visible()を使用してGtk.MessageDialogを見えなくします。
@Gtk.Template.Callback()
def on_dialog_response(self, dialog, response_id):
if response_id == Gtk.ResponseType.OK:
print('OK を押しました。')
else:
print('Esc を押しました。')
dialog.set_visible(False)
Gtk.MessageDialogを表示する
Gtk.MessageDialogは、メソッドset_visible()を使用して表示します。
@Gtk.Template.Callback()
def on_button_clicked(self, button):
self.messagedialog.set_visible(True)
サンプルプログラム
以下のサンプルプログラムを実行すると左図のようなボタン(MessageDialog表示)付きのWindowが表示されます。ボタンを押すと、右図のようなGtk.MessageDialogが表示されます。
実行方法
下の2つのファイルを同じフォルダに保存して、そのフォルダで以下のコマンドを実行します。
python main.py
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="window" parent="GtkWindow">
<property name="default_width">500</property>
<property name="default_height">70</property>
<property name="title">Template MessageDialog Test</property>
<child>
<object class="GtkButton" id="button">
<property name="label">MessageDialog表示</property>
<property name="margin-top">20</property>
<property name="margin-bottom">20</property>
<property name="margin-start">20</property>
<property name="margin-end">20</property>
<signal name="clicked" handler="on_button_clicked"/>
</object>
</child>
</template>
<object class="GtkMessageDialog" id="messagedialog">
<property name="text">テキスト</property>
<property name="secondary-text">第2テキスト</property>
<property name="message-type">info</property>
<property name="transient-for">window</property>
<property name="modal">True</property>
<signal name="response" handler="on_dialog_response"/>
<child type="action">
<object class="GtkButton" id="button_ok">
<property name="label">OK</property>
</object>
</child>
<action-widgets>
<action-widget response="ok" default="true">button_ok</action-widget>
</action-widgets>
</object>
</interface>
import os
import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk
APPID = 'com.github.taniyoshima.g4_fblogt_messagedialog'
@Gtk.Template(filename=os.path.dirname(__file__) + '/ui_file.ui')
class Gtk4TestTest(Gtk.Window):
__gtype_name__ = "window"
messagedialog = Gtk.Template.Child()
def __init__(self, app):
Gtk.Window.__init__(
self, application=app, title='Template MessageDialog Test')
@Gtk.Template.Callback()
def on_button_clicked(self, button):
self.messagedialog.set_visible(True)
@Gtk.Template.Callback()
def on_dialog_response(self, dialog, response_id):
if response_id == Gtk.ResponseType.OK:
print('OK を押しました。')
else:
print('Esc を押しました。')
dialog.set_visible(False)
class Gtk4TestApp(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self, application_id=APPID)
def do_activate(self):
window = Gtk4TestTest(self)
window.present()
def main():
app = Gtk4TestApp()
app.run()
if __name__ == '__main__':
main()